DOC: <Issue related to /tutorials/Developers/agents> Clarification needed on error key usage in handle_tool_error #28

Closed
opened 2026-02-21 17:17:00 -05:00 by yindo · 1 comment
Owner

Originally created by @Innary on GitHub (Oct 8, 2024).

Description:

The handle_tool_error function references an error key from the state object:

def handle_tool_error(state) -> dict:
    error = state.get("error")
    tool_calls = state["messages"][-1].tool_calls
    return {
        "messages": [
            ToolMessage(
                content=f"Error: {repr(error)}\n please fix your mistakes.",
                tool_call_id=tc["id"],
            )
            for tc in tool_calls
        ]
    }

However, it's unclear where this error key comes from. There is no indication that this key is defined or added to the state anywhere before it is accessed. The State class is defined as:

class State(TypedDict):
    messages: Annotated[list[AnyMessage], add_messages]

This class doesn't seem to include any error key. The function create_tool_node_with_fallback seems to expect an error key to be present when an exception occurs:

def create_tool_node_with_fallback(tools: list) -> dict:
    return ToolNode(tools).with_fallbacks(
        [RunnableLambda(handle_tool_error)], exception_key="error"
    )

Questions:

  1. At what point is the error key introduced into the state object?
  2. Is there a specific mechanism or flow that guarantees the error key is added to state before handle_tool_error is invoked?
  3. Should the State class or the related handling code be responsible for defining or initializing the error key?

It appears that the error key is expected to exist due to the exception handling setup, but the exact moment or process of its introduction into the state is unclear.

Originally created by @Innary on GitHub (Oct 8, 2024). **Description:** The `handle_tool_error` function references an `error` key from the `state` object: ```python def handle_tool_error(state) -> dict: error = state.get("error") tool_calls = state["messages"][-1].tool_calls return { "messages": [ ToolMessage( content=f"Error: {repr(error)}\n please fix your mistakes.", tool_call_id=tc["id"], ) for tc in tool_calls ] } ``` However, it's unclear where this `error` key comes from. There is no indication that this key is defined or added to the `state` anywhere before it is accessed. The `State` class is defined as: ```python class State(TypedDict): messages: Annotated[list[AnyMessage], add_messages] ``` This class doesn't seem to include any `error` key. The function `create_tool_node_with_fallback` seems to expect an `error` key to be present when an exception occurs: ```python def create_tool_node_with_fallback(tools: list) -> dict: return ToolNode(tools).with_fallbacks( [RunnableLambda(handle_tool_error)], exception_key="error" ) ``` **Questions:** 1. At what point is the `error` key introduced into the `state` object? 2. Is there a specific mechanism or flow that guarantees the `error` key is added to `state` before `handle_tool_error` is invoked? 3. Should the `State` class or the related handling code be responsible for defining or initializing the `error` key? It appears that the `error` key is expected to exist due to the exception handling setup, but the exact moment or process of its introduction into the `state` is unclear.
yindo closed this issue 2026-02-21 17:17:00 -05:00
Author
Owner
@Innary commented on GitHub (Oct 8, 2024): https://python.langchain.com/api_reference/core/runnables/langchain_core.runnables.base.Runnable.html#langchain_core.runnables.base.Runnable.with_fallbacks
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langsmith-docs#28