Skip to content

fix: prevent IndexError when content is empty after tool recursion (#355)#446

Open
guoyangzhen wants to merge 1 commit into
2FastLabs:mainfrom
guoyangzhen:fix/empty-content-indexerror
Open

fix: prevent IndexError when content is empty after tool recursion (#355)#446
guoyangzhen wants to merge 1 commit into
2FastLabs:mainfrom
guoyangzhen:fix/empty-content-indexerror

Conversation

@guoyangzhen

Copy link
Copy Markdown

Problem

When max_recursions is exhausted during the tool execution loop, the agent returns a ConversationMessage with either:

  • content=None (Python AnthropicAgent)
  • content containing tool_use blocks instead of text (Python/TypeScript BedrockLLMAgent)
  • content=[] after filtering (Python AnthropicAgent streaming)

This causes IndexError: list index out of range when callers access response.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:

while continue_with_tools and max_recursions > 0:
    # ... tool processing ...
    max_recursions -= 1

return ConversationMessage(content=llm_content)  # llm_content is None!

When max_recursions hits 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: Guard llm_content is None after loop
  • AnthropicAgent._handle_streaming: Guard empty content_list after filtering
  • BedrockLLMAgent._handle_single_response_loop: Detect tool_use content in final response
  • BedrockLLMAgent._handle_streaming: Detect None final_response after loop

TypeScript:

  • AnthropicAgent.processRequest: Safe optional chaining for message.content?.[0]?.["text"]
  • BedrockLLMAgent.processRequest: Detect null/tool_use finalMessage after loop

All fallbacks return "Maximum tool recursion limit reached without a final response." instead of crashing.

Fixes #355

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 2FastLabs#355
@cornelcroi

Copy link
Copy Markdown
Collaborator

Hey @guoyangzhen, real bug fixed across all four affected paths (Python and TypeScript, both agents). The if final_response: guard and the null-check after the loop are correct. No API changes, fully backward compatible. Just needs CI to go green before merging.

One small thing: the TypeScript AnthropicAgent path uses optional chaining and ends up with an empty string "" rather than the explicit fallback message the other paths return. Small inconsistency, not a blocker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: AgentResponse.output.content returns empty list causing IndexError in response processing

2 participants