-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add metadata support to deferred tool exceptions #3345
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
cjohnhanson
wants to merge
13
commits into
pydantic:main
Choose a base branch
from
cjohnhanson:feat/deferred-tool-metadata
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.
Open
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
367d860
Add metadata support to deferred tool exceptions
cjohnhanson fbb0457
Reduce complexity of _call_tools function
cjohnhanson 93c24ab
Fix typo in test_reporting.py snapshot
cjohnhanson 42cf5b8
Address Douwe's review feedback on deferred tool metadata
cjohnhanson c7aaca9
Improve deferred tools metadata documentation example
cjohnhanson b23609a
Fix evaluation reporting snapshots for CI (COLUMNS=150)
cjohnhanson d4f1613
Replace ML training example with flight booking example
cjohnhanson 8e6ac24
test: Add test support for deferred_tools_with_metadata example
cjohnhanson 09a38a2
test: Complete deferred tool tests and remove dead branch
cjohnhanson c3c543f
Merge branch 'main' into feat/deferred-tool-metadata
cjohnhanson bb91b35
Merge branch 'main' into feat/deferred-tool-metadata
cjohnhanson e4b86a3
chore: Remove unused ML training test expectations and codespell ignore
cjohnhanson 37f5790
Merge branch 'main' into feat/deferred-tool-metadata
cjohnhanson 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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -147,6 +147,12 @@ class DeferredToolRequests: | |||||
| """Tool calls that require external execution.""" | ||||||
| approvals: list[ToolCallPart] = field(default_factory=list) | ||||||
| """Tool calls that require human-in-the-loop approval.""" | ||||||
| metadata: dict[str, dict[str, Any]] = field(default_factory=dict) | ||||||
| """Metadata for deferred tool calls, keyed by tool_call_id. | ||||||
|
Collaborator
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.
Suggested change
|
||||||
|
|
||||||
| This contains any metadata that was provided when raising [`CallDeferred`][pydantic_ai.exceptions.CallDeferred] | ||||||
| or [`ApprovalRequired`][pydantic_ai.exceptions.ApprovalRequired] exceptions. | ||||||
| """ | ||||||
|
|
||||||
|
|
||||||
| @dataclass(kw_only=True) | ||||||
|
|
||||||
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 |
|---|---|---|
|
|
@@ -4857,9 +4857,13 @@ def call_second(): | |
| else: | ||
| result = agent.run_sync(user_prompt) | ||
|
|
||
| assert result.output == snapshot( | ||
| DeferredToolRequests(approvals=[ToolCallPart(tool_name='requires_approval', tool_call_id=IsStr())]) | ||
| ) | ||
| assert isinstance(result.output, DeferredToolRequests) | ||
| assert len(result.output.approvals) == 1 | ||
| assert result.output.approvals[0].tool_name == 'requires_approval' | ||
| # When no metadata is provided, the tool_call_id should not be in metadata dict | ||
| tool_call_id = result.output.approvals[0].tool_call_id | ||
|
Collaborator
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. I think we can revert this to the snapshot -- the |
||
| assert tool_call_id not in result.output.metadata | ||
| assert result.output.metadata == {} | ||
| assert integer_holder == 2 | ||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
@DouweM I wasn't 100% sure on whether this would benefit from a dedicated example in the docs. I could easily shift this to just be included in the existing examples.
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.
As much as I appreciate the Meltano reference in the flight destinations, I think we can make the existing examples stronger by using
metadataso we should do it there (with one line explanations of themetadataarg, possibly inside one of those# (1)tooltips) instead of in its own section:CallDeferredexample could have scheduling the task return atask_idthat can then be included in metadata, instead of making theTaskResultaware oftool_call_id.ApprovalRequiredexample could havereason: 'protected'or something that can be displayed to the user.