Skip to content

Commit 11b91f4

Browse files
committed
feat: add additional error case where no tool uses were fixed
1 parent 104f6b4 commit 11b91f4

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/strands/agent/conversation_manager/recover_tool_use_on_max_tokens_reached.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ async def recover_tool_use_on_max_tokens_reached(agent: "Agent", exception: MaxT
3838
raise exception
3939

4040
valid_content: list[ContentBlock] = []
41+
has_corrected_content = False
4142
for content in incomplete_message["content"]:
4243
tool_use: ToolUse | None = content.get("toolUse")
4344
if not tool_use:
@@ -57,10 +58,15 @@ async def recover_tool_use_on_max_tokens_reached(agent: "Agent", exception: MaxT
5758
f"to maximum token limits being reached."
5859
}
5960
)
61+
has_corrected_content = True
6062
else:
6163
# ToolUse was invalid for an unknown reason. Cannot correct, return without modifying
6264
raise exception
6365

66+
if not has_corrected_content:
67+
# No ToolUse were modified, meaning this method could not have resolved the root cause
68+
raise exception
69+
6470
valid_message: Message = {"content": valid_content, "role": incomplete_message["role"]}
6571
agent.messages.append(valid_message)
6672
agent.hooks.invoke_callbacks(MessageAddedEvent(agent=agent, message=valid_message))

tests/strands/agent/conversation_manager/test_recover_tool_use_on_max_tokens_reached.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,30 @@ async def test_recover_tool_use_on_max_tokens_reached_with_valid_tool_use():
128128
mock_invoke_callbacks.assert_not_called()
129129

130130

131+
@pytest.mark.parametrize(
132+
"content,description",
133+
[
134+
([], "empty content"),
135+
([{"text": "Just some text with no tools to edit."}], "text-only content"),
136+
],
137+
)
131138
@pytest.mark.asyncio
132-
async def test_recover_tool_use_on_max_tokens_reached_with_empty_content():
139+
async def test_recover_tool_use_on_max_tokens_reached_with_empty_content(content, description):
133140
"""Test that an exception that is raised without recoverability, re-raises exception."""
134141
agent = Agent()
135142
# Mock the hooks.invoke_callbacks method
136143
mock_invoke_callbacks = Mock()
137144
agent.hooks.invoke_callbacks = mock_invoke_callbacks
138145
initial_message_count = len(agent.messages)
139146

140-
incomplete_message: Message = {"role": "assistant", "content": []}
147+
incomplete_message: Message = {"role": "assistant", "content": content}
141148

142149
exception = MaxTokensReachedException(message="Token limit reached", incomplete_message=incomplete_message)
143150

144151
with pytest.raises(MaxTokensReachedException):
145152
await recover_tool_use_on_max_tokens_reached(agent, exception)
146153

147-
# Should not add any message since content is empty
154+
# Should not add any message since there's nothing to recover
148155
assert len(agent.messages) == initial_message_count
149156

150157
# Verify that the MessageAddedEvent callback was NOT invoked

0 commit comments

Comments
 (0)