generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 507
Add dummy toolUse message when its missing due to pagination in session management #1274
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
afarntrog
wants to merge
1
commit into
strands-agents:main
Choose a base branch
from
afarntrog:fix_session_missing_toolUse
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 all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """Tests for tool helper functions.""" | ||
|
|
||
| from strands.tools._tool_helpers import ( | ||
| generate_missing_tool_result_content, | ||
| generate_missing_tool_use_content, | ||
| ) | ||
|
|
||
|
|
||
| class TestGenerateMissingToolResultContent: | ||
| """Tests for generate_missing_tool_result_content function.""" | ||
|
|
||
| def test_single_tool_use_id(self): | ||
| """Test generating content for a single tool use ID.""" | ||
| tool_use_ids = ["tool_123"] | ||
| result = generate_missing_tool_result_content(tool_use_ids) | ||
|
|
||
| assert len(result) == 1 | ||
| assert "toolResult" in result[0] | ||
| assert result[0]["toolResult"]["toolUseId"] == "tool_123" | ||
| assert result[0]["toolResult"]["status"] == "error" | ||
| assert result[0]["toolResult"]["content"] == [{"text": "Tool was interrupted."}] | ||
|
|
||
| def test_multiple_tool_use_ids(self): | ||
| """Test generating content for multiple tool use IDs.""" | ||
| tool_use_ids = ["tool_123", "tool_456", "tool_789"] | ||
| result = generate_missing_tool_result_content(tool_use_ids) | ||
|
|
||
| assert len(result) == 3 | ||
| for i, tool_id in enumerate(tool_use_ids): | ||
| assert "toolResult" in result[i] | ||
| assert result[i]["toolResult"]["toolUseId"] == tool_id | ||
| assert result[i]["toolResult"]["status"] == "error" | ||
|
|
||
| def test_empty_list(self): | ||
| """Test generating content for empty list.""" | ||
| result = generate_missing_tool_result_content([]) | ||
| assert result == [] | ||
|
|
||
|
|
||
| class TestGenerateMissingToolUseContent: | ||
| """Tests for generate_missing_tool_use_content function.""" | ||
|
|
||
| def test_single_tool_result_id(self): | ||
| """Test generating content for a single tool result ID.""" | ||
| tool_result_ids = ["tooluse_abc123"] | ||
| result = generate_missing_tool_use_content(tool_result_ids) | ||
|
|
||
| assert len(result) == 1 | ||
| assert "toolUse" in result[0] | ||
| assert result[0]["toolUse"]["toolUseId"] == "tooluse_abc123" | ||
| assert result[0]["toolUse"]["name"] == "unknown_tool" | ||
| assert result[0]["toolUse"]["input"] == {"error": "toolUse is missing. Ignore."} | ||
|
|
||
| def test_multiple_tool_result_ids(self): | ||
| """Test generating content for multiple tool result IDs.""" | ||
| tool_result_ids = ["tooluse_abc123", "tooluse_def456", "tooluse_ghi789"] | ||
| result = generate_missing_tool_use_content(tool_result_ids) | ||
|
|
||
| assert len(result) == 3 | ||
| for i, tool_id in enumerate(tool_result_ids): | ||
| assert "toolUse" in result[i] | ||
| assert result[i]["toolUse"]["toolUseId"] == tool_id | ||
| assert result[i]["toolUse"]["name"] == "unknown_tool" | ||
| assert result[i]["toolUse"]["input"] == {"error": "toolUse is missing. Ignore."} | ||
|
|
||
| def test_empty_list(self): | ||
| """Test generating content for empty list.""" | ||
| result = generate_missing_tool_use_content([]) | ||
| assert result == [] | ||
|
|
||
| def test_realistic_tool_use_id_format(self): | ||
| """Test with realistic tool use ID format (like those from Bedrock).""" | ||
| tool_result_ids = ["tooluse_f09Y0LwyT2yteCYshTzb_Q"] | ||
| result = generate_missing_tool_use_content(tool_result_ids) | ||
|
|
||
| assert len(result) == 1 | ||
| assert result[0]["toolUse"]["toolUseId"] == "tooluse_f09Y0LwyT2yteCYshTzb_Q" |
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.
From the bug report - Isn't the problem the opposite?
It's not that the latest message is broken; it's that the oldest message is broken - that's because we have N messages:
And 48 is a
toolResultwith 47 beingtoolUse, but it's not includedIn this case
messages[0]is 55, whilemessages[-1]== 48And I think the fix in this case is to just not include
48There 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.
So here's a full example:
Let's view the agent_1 messages:
The session manager is replaying the conversation so the
-1message is the newest one (I like pizza) whereas the closer to0the further back we go.Uh oh!
There was an error while loading. Please reload this page.
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.
You're right; I was thinking of it backwards.
Still, can we remove the first message instead creating a fake one? IMHO it's better to trim than to synthesize
(and if you do ^, can we update the comment to indicate "check if the oldest message")