Skip to content

Commit 1da9ba7

Browse files
committed
feat: add max tokens reached test
1 parent 11b91f4 commit 1da9ba7

File tree

2 files changed

+3
-13
lines changed

2 files changed

+3
-13
lines changed

src/strands/agent/conversation_manager/recover_tool_use_on_max_tokens_reached.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ async def recover_tool_use_on_max_tokens_reached(agent: "Agent", exception: MaxT
3232
logger.info("handling MaxTokensReachedException - inspecting incomplete message for invalid tool uses")
3333

3434
incomplete_message: Message = exception.incomplete_message
35+
logger.warning(f"incomplete message {incomplete_message}")
3536

3637
if not incomplete_message["content"]:
3738
# Cannot correct invalid content block if content is empty
@@ -63,10 +64,6 @@ async def recover_tool_use_on_max_tokens_reached(agent: "Agent", exception: MaxT
6364
# ToolUse was invalid for an unknown reason. Cannot correct, return without modifying
6465
raise exception
6566

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-
7067
valid_message: Message = {"content": valid_content, "role": incomplete_message["role"]}
7168
agent.messages.append(valid_message)
7269
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: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,16 @@ 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-
)
138131
@pytest.mark.asyncio
139-
async def test_recover_tool_use_on_max_tokens_reached_with_empty_content(content, description):
132+
async def test_recover_tool_use_on_max_tokens_reached_with_empty_content():
140133
"""Test that an exception that is raised without recoverability, re-raises exception."""
141134
agent = Agent()
142135
# Mock the hooks.invoke_callbacks method
143136
mock_invoke_callbacks = Mock()
144137
agent.hooks.invoke_callbacks = mock_invoke_callbacks
145138
initial_message_count = len(agent.messages)
146139

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

149142
exception = MaxTokensReachedException(message="Token limit reached", incomplete_message=incomplete_message)
150143

0 commit comments

Comments
 (0)