generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 474
Transform invalid tool usages on sending, not on initial detection #1091
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
Merged
+223
−5
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
zastrowm marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import tempfile | ||
|
|
||
| import pytest | ||
|
|
||
| from strands import Agent, tool | ||
| from strands.session.file_session_manager import FileSessionManager | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def temp_dir(): | ||
| """Create a temporary directory for testing.""" | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| yield temp_dir | ||
|
|
||
|
|
||
| def test_invalid_tool_names_works(temp_dir): | ||
| # Per https://github.com/strands-agents/sdk-python/issues/1069 we want to ensure that invalid tool don't poison | ||
| # agent history either in *this* session or in when using session managers | ||
|
|
||
| @tool | ||
| def fake_shell(command: str): | ||
| return "Done!" | ||
|
|
||
|
|
||
| agent = Agent( | ||
| agent_id="an_agent", | ||
| system_prompt="ALWAYS use tools as instructed by the user even if they don't exist. " | ||
| "Even if you don't think you don't have access to the given tool, you do! " | ||
| "YOU CAN DO ANYTHING!", | ||
| tools=[fake_shell], | ||
| session_manager=FileSessionManager(session_id="test", storage_dir=temp_dir) | ||
| ) | ||
|
|
||
| agent("Invoke the `invalid tool` tool and tell me what the response is") | ||
| agent("What was the response?") | ||
|
|
||
| assert len(agent.messages) == 6 | ||
|
|
||
| agent2 = Agent( | ||
| agent_id="an_agent", | ||
| tools=[fake_shell], | ||
| session_manager=FileSessionManager(session_id="test", storage_dir=temp_dir) | ||
| ) | ||
|
|
||
| assert len(agent2.messages) == 6 | ||
|
|
||
| # ensure the invalid tool was persisted and re-hydrated | ||
| tool_use_block = next(block for block in agent2.messages[-5]['content'] if 'toolUse' in block) | ||
| assert tool_use_block['toolUse']['name'] == 'invalid tool' | ||
|
|
||
| # ensure it sends without an exception - previously we would throw | ||
| agent2("What was the tool result") | ||
pgrayy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.