I’m encountering an error when invoking a tool that requires tool_runtime inside a LangGraph StateGraph agent.
Code snippet:
# -*- coding: utf-8 -*-
"""tool_runtime_issue
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1kFjX0i4jL4P9HD5wLsZHO4YprX2TlkM7
"""
!apt-get install -qq -y graphviz libgraphviz-dev > /dev/null 2>&1
!pip install --quiet langgraph==1.0.3 langchain==1.0.0 langchain_openai==1.0.0 grandalf pygraphviz > /dev/null 2>&1
from pydantic import BaseModel, Field
from typing import Annotated, Sequence
from langchain_core.messages.base import BaseMessage
from langgraph.graph import StateGraph, START, END, add_messages
from langchain.tools import tool, ToolRuntime
from langgraph.prebuilt import ToolNode
from langchain_core.messages import AIMessage, ToolCall
from langgraph.types import Command
class FooState(BaseModel):
messages: Annotated[Sequence[BaseMessage], add_messages]
called_tool: bool = False
@tool("foo_tool", description = "Please call this tool so that I can try how it's working")
def foo_tool(
tool_runtime: ToolRuntime
) -> Command:
msg = tool_runtime.state.messages # I need to access the state
...
return Command(update={
"called_tool": True})
tool_node = ToolNode([foo_tool])
def foo_llm(state):
return {"messages": AIMessage(content = "foo", tool_calls = [ToolCall(name = "foo_tool", args = {}, id = '1213')])}
"""## graph"""
agent_builder = StateGraph(state_schema = FooState)
agent_builder.add_node("foo_llm", foo_llm)
agent_builder.add_node("tools", tool_node)
# Edges
agent_builder.add_edge(START, "foo_llm")
agent_builder.add_edge("foo_llm", "tools")
agent_builder.add_edge("tools", END)
agent = agent_builder.compile()
output = agent.invoke(input = {})
output
Error output:
{'messages': [AIMessage(content='foo', additional_kwargs={}, response_metadata={}, id='e1e5fef6-c762-4720-983a-9c7792178ced', tool_calls=[{'name': 'foo_tool', 'args': {}, 'id': '1213', 'type': 'tool_call'}]),
ToolMessage(content="Error invoking tool 'foo_tool' with kwargs {} with error:\n tool_runtime: Field required\n Please fix the error and try again.", name='foo_tool', id='82b5047b-d58f-4fc7-972d-367a2c8e2cd5', tool_call_id='1213', status='error')]}
Expected behavior:
The foo_tool should receive the automatically injected tool_runtime, which is not an actual argument visible to the LLM. Instead, the call fails due to missing fields.
Am I getting something wrong or is it a bug?