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

Agent Terminates Early When A Return_direct Tool Call Fails, Preventing Model Retry

In LangChain agents, when a tool decorated with return_direct=True raises an exception, ToolNode produces a ToolMessage with status='error'. However, the routing logic in _make_tools_to_model_edge exits the agent loop based solely on return_direct flags, ignoring the error status. This causes the agent to return the error ToolMessage as the final output instead of routing back to the model for retry or graceful handling.

highConfidence 95%LangchainChat ModelsVector StoresEmbeddingsAffected V1.2.14

Origin Analysis

The exit condition in `_make_tools_to_model_edge` (factory.py lines ~1797-1805) checks only that all client-side tool calls correspond to tools with `return_direct=True`. It does not inspect the generated `ToolMessage` objects for `status == 'error'`. As a result, even when `ToolNode` correctly marks a failed tool call with an error status, the agent exits the loop immediately, depriving the model of the opportunity to react to the failure.
1. Define a tool with `@tool(return_direct=True)` that always raises an exception.\n2. Create a `FakeToolCallingModel` that returns a single tool call for that tool, then an empty response.\n3. Invoke `agent.invoke({"messages": [HumanMessage("run the tool")]})`.\n4. Observe that `result["messages"][-1]` is a `ToolMessage` with `status='error'` rather than an `AIMessage`, indicating the agent exited without giving the model a chance to handle the error.

Fixing Code Block

Edge Case Audit

This change alters routing behavior when return_direct tools fail. Potential risks: (1) If the agent framework or downstream code expects the previous early-exit behavior in error cases, this may break backward compatibility. (2) In concurrent or multi-threaded execution, the order and completeness of state['messages'] must be guaranteed; the linear scan over messages may be affected if messages are appended asynchronously. (3) If multiple tools are called and only some succeed, the logic will route back to the model for any error, which may be desirable but could lead to extra model round-trips. (4) The fix relies on ToolMessage.status being set correctly; custom tool wrappers or older langchain-core versions might not populate this field, leading to undetected errors. Rollback: revert to the original condition `if client_side_tool_calls and all(...): return end_destination` to restore old behavior.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state