Skip to content

Commit 7fff2f9

Browse files
declan-scaleclaude
andcommitted
fix(tutorials): handle unknown tool name in at130-langgraph tools_node
Return an error ToolMessage for tool calls whose name isn't registered, mirroring LangGraph's ToolNode, so a hallucinated tool name keeps the graph running instead of raising an unhandled KeyError and crashing the workflow node. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent cc024b3 commit 7fff2f9

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

  • examples/tutorials/10_async/10_temporal/130_langgraph/project

examples/tutorials/10_async/10_temporal/130_langgraph/project/graph.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ async def tools_node(state: AgentState) -> dict[str, Any]:
7878
last = state["messages"][-1]
7979
results: list[Any] = []
8080
for call in getattr(last, "tool_calls", None) or []:
81-
output = await _TOOLS_BY_NAME[call["name"]].ainvoke(call["args"])
81+
tool = _TOOLS_BY_NAME.get(call["name"])
82+
# Mirror ToolNode: surface an unknown/hallucinated tool name as an error
83+
# ToolMessage so the graph keeps running instead of crashing the node.
84+
if tool is None:
85+
output = f"Error: unknown tool {call['name']!r}. Available: {list(_TOOLS_BY_NAME)}"
86+
else:
87+
output = await tool.ainvoke(call["args"])
8288
results.append(
8389
ToolMessage(content=str(output), tool_call_id=call["id"], name=call["name"])
8490
)

0 commit comments

Comments
 (0)