-
Notifications
You must be signed in to change notification settings - Fork 967
feat: add ToolReturn for customizable tool return #2060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Wh1isper
wants to merge
17
commits into
pydantic:main
Choose a base branch
from
Wh1isper:feat-extend-multi-modal-content
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+272
−2
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3b14091
feat: add MultiModalContent for customizable multi-modal tool present…
Wh1isper 80aff7e
Add test
Wh1isper b7de244
add docs
Wh1isper d4601eb
Merge branch 'main' into feat-extend-multi-modal-content
Wh1isper 8ae0de6
Also skip lint
Wh1isper 4f4f29b
Merge branch 'main' into feat-extend-multi-modal-content
Wh1isper c7eee37
refactor: add extra data and change fields
Wh1isper 8056a9b
fix missing tool return
Wh1isper 4d6cd42
more error process and test
Wh1isper 357550b
fix typehint..
Wh1isper 91a1ad1
fix cov
Wh1isper f47ee1c
Update pydantic_ai_slim/pydantic_ai/_agent_graph.py
Wh1isper da18189
Update pydantic_ai_slim/pydantic_ai/_agent_graph.py
Wh1isper 92f84c5
change to metadata and fix test
Wh1isper c1edce2
Merge branch 'main' into feat-extend-multi-modal-content
Wh1isper b72dd81
Update comment
Wh1isper 5397436
Merge branch 'main' into feat-extend-multi-modal-content
Wh1isper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -306,6 +306,29 @@ def format(self) -> str: | |
|
||
UserContent: TypeAlias = 'str | ImageUrl | AudioUrl | DocumentUrl | VideoUrl | BinaryContent' | ||
|
||
|
||
@dataclass(repr=False) | ||
class ToolReturn: | ||
"""A structured return value for tools that need to provide both a return value and custom content to the model. | ||
This class allows tools to return complex responses that include: | ||
- A return value for actual tool return | ||
- Custom content (including multi-modal content) to be sent to the model as a UserPromptPart | ||
- Optional metadata for application use | ||
""" | ||
|
||
return_value: Any | ||
"""The return value to be used in the tool response.""" | ||
|
||
content: Sequence[UserContent] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make this one optional, in case the user wants to use only a |
||
"""The content sequence to be sent to the model as a UserPromptPart.""" | ||
|
||
metadata: Any = None | ||
"""Additional data that can be accessed programmatically by the application but is not sent to the LLM.""" | ||
|
||
__repr__ = _utils.dataclasses_no_defaults_repr | ||
|
||
|
||
# Ideally this would be a Union of types, but Python 3.9 requires it to be a string, and strings don't work with `isinstance``. | ||
MultiModalContentTypes = (ImageUrl, AudioUrl, DocumentUrl, VideoUrl, BinaryContent) | ||
_document_format_lookup: dict[str, DocumentFormat] = { | ||
|
@@ -396,6 +419,9 @@ class ToolReturnPart: | |
tool_call_id: str | ||
"""The tool call identifier, this is used by some models including OpenAI.""" | ||
|
||
metadata: Any = None | ||
"""Additional data that can be accessed programmatically by the application but is not sent to the LLM.""" | ||
|
||
timestamp: datetime = field(default_factory=_now_utc) | ||
"""The timestamp, when the tool returned.""" | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add
Some other AI frameworks call this feature "artifacts"
, in case someone searches for that.