fix: prevent IndexError when content is empty after tool recursion (#355)#446
Open
guoyangzhen wants to merge 1 commit into2FastLabs:mainfrom
Open
fix: prevent IndexError when content is empty after tool recursion (#355)#446guoyangzhen wants to merge 1 commit into2FastLabs:mainfrom
guoyangzhen wants to merge 1 commit into2FastLabs:mainfrom
Conversation
When max_recursions is exhausted during the tool execution loop, the
agent returns a ConversationMessage with empty or tool_use content,
causing IndexError ('list index out of range') when callers access
response.output.content[0]['text'].
This fix adds fallback handling in all 4 affected code paths:
Python:
- AnthropicAgent._handle_single_response_loop: guard llm_content is None
- AnthropicAgent._handle_streaming: guard empty content_list
- BedrockLLMAgent._handle_single_response_loop: detect tool_use response
- BedrockLLMAgent._handle_streaming: detect None final_response
TypeScript:
- AnthropicAgent.processRequest: safe optional chaining for content access
- BedrockLLMAgent.processRequest: detect null/tool_use finalMessage
Fixes awslabs#355
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When
max_recursionsis exhausted during the tool execution loop, the agent returns aConversationMessagewith either:content=None(Python AnthropicAgent)contentcontaining tool_use blocks instead of text (Python/TypeScript BedrockLLMAgent)content=[]after filtering (Python AnthropicAgent streaming)This causes
IndexError: list index out of rangewhen callers accessresponse.output.content[0]['text'].Issue #355 reports this happening intermittently with the SupervisorAgent, which delegates to lead agents that use tool recursion.
Root Cause
In the tool recursion loop:
When
max_recursionshits 0 inside the loop while processing tool calls,llm_content(or equivalent) never gets assigned a text response.Fix
Added fallback handling in all 4 affected code paths:
Python:
AnthropicAgent._handle_single_response_loop: Guardllm_content is Noneafter loopAnthropicAgent._handle_streaming: Guard emptycontent_listafter filteringBedrockLLMAgent._handle_single_response_loop: Detect tool_use content in final responseBedrockLLMAgent._handle_streaming: DetectNonefinal_response after loopTypeScript:
AnthropicAgent.processRequest: Safe optional chaining formessage.content?.[0]?.["text"]BedrockLLMAgent.processRequest: Detect null/tool_usefinalMessageafter loopAll fallbacks return
"Maximum tool recursion limit reached without a final response."instead of crashing.Fixes #355