AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

ToolStrategy Infinitely Retries Structured-Output Validation Failures Without A Retry Cap

When a model returns invalid structured output, ToolStrategy's default handle_errors=True causes infinite retries without any attempt limit, leading to context window exhaustion or recursion limit errors. A retry budget should be introduced to fail gracefully after a maximum number of attempts.

highConfidence 90%LangchainAffected V1.3.15

Origin Analysis

In langchain.agents.structured_output.ToolStrategy, the _handle_structured_output_error method checks only the handle_errors flag (default True) and never tracks the number of retry attempts. As a result, should_retry remains True indefinitely, causing the agent to repeatedly append AIMessage/ToolMessage pairs for the same validation error.
1. Define a Pydantic model with strict types (e.g., Literal[True] | Literal[False]). 2. Create a custom BaseChatModel that deterministically returns a tool call with a value that violates the schema (e.g., "passed": "true" as a string). 3. Initialize an agent with create_agent(model, [], response_format=ToolStrategy(GraderResponse)). 4. Invoke the agent. Observe that the model is called repeatedly with growing message history until the graph recursion limit is hit or the provider rejects the request due to context length.

Fixing Code Block

Edge Case Audit

The counter is instance-level and not reset between separate agent invocations unless the ToolStrategy is re-instantiated. In concurrent or multi-threaded scenarios, shared instance state may cause premature failure. If max_retries is set too low, legitimate transient model errors might not be corrected; if set too high, context window exhaustion can still occur. Rollback: revert to previous behavior by setting handle_errors=False or removing max_retries, and rely on external context limits or timeouts.

Ecosystem Topology