Skip to content

fix(prebuilt): add timeout parameter to ToolNode for async tool execution#6973

Open
Br1an (Br1an67) wants to merge 1 commit intolangchain-ai:mainfrom
Br1an67:fix/tool-node-timeout
Open

fix(prebuilt): add timeout parameter to ToolNode for async tool execution#6973
Br1an (Br1an67) wants to merge 1 commit intolangchain-ai:mainfrom
Br1an67:fix/tool-node-timeout

Conversation

@Br1an67
Copy link

Description: When tools involve network calls (e.g., MCP servers with sse_read_timeout), ToolNode.ainvoke() can hang indefinitely if the underlying transport times out silently without propagating the exception. This adds an optional timeout parameter (in seconds) that wraps each individual async tool coroutine with asyncio.wait_for, ensuring that hanging tool calls are cancelled and handled gracefully.

When a tool exceeds the timeout:

  • If handle_tool_errors is enabled (default): returns a ToolMessage with status="error" and a descriptive message
  • If handle_tool_errors is False: raises TimeoutError

The timeout applies per-tool-call, so in parallel execution fast tools still complete successfully even if slow ones time out. When no timeout is specified, behavior is unchanged (full backward compatibility).

Example usage:

tool_node = ToolNode(mcp_tools, timeout=30)
await tool_node.ainvoke(input)

Issue: Closes #6412

Dependencies: None

…tion

When tools involve network calls (e.g. MCP servers with sse_read_timeout),
ToolNode.ainvoke() can hang indefinitely if the underlying transport times
out silently. This adds an optional `timeout` parameter that wraps each
individual tool coroutine with `asyncio.wait_for`, ensuring that hanging
tool calls are cancelled and handled gracefully.

When a tool exceeds the timeout:
- If handle_tool_errors is enabled (default), returns a ToolMessage with
  status='error' and a descriptive message
- If handle_tool_errors is False, raises TimeoutError

The timeout applies per-tool-call, so in parallel execution fast tools
still complete successfully even if slow ones time out.

Closes langchain-ai#6412
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant