[GH-ISSUE #1173] [langgraph]: LangGraph StateGraph does not work with ToolRuntime #158

Open
opened 2026-02-17 17:19:18 -05:00 by yindo · 3 comments
Owner

Originally created by @bachxoai on GitHub (Oct 28, 2025).
Original GitHub issue: https://github.com/langchain-ai/docs/issues/1173

Type of issue

issue / bug

Language

Python

Description

Description

When using a StateGraph-based LangGraph agent that includes a tool expecting a ToolRuntime parameter, the runtime argument is not passed correctly to the tool at execution time, resulting in a Pydantic validation error.


Code Example

Here’s how the compiled graph is created:

def custom_create_agent(
    model: BaseChatModel,
    state_schema: type[AgentState[ResponseT]],
    context_schema: type[ContextT],
    tools: ToolType,
):
    graph = StateGraph(
        state_schema=state_schema,
        context_schema=context_schema,
    )

    tool_node = ToolNode(tools)
    chatbot_node = AgentNode(model, tools)
    a_node = ANode(model)
    nodes = [tool_node, chatbot_node, a_node]
    for node in nodes:
        graph.add_node(getattr(node, "name", str(node)), node)

    graph.add_edge(START, chatbot_node.name)
    graph.add_conditional_edges(chatbot_node.name, after_chatbot_edge)
    graph.add_edge(tool_node.name, chatbot_node.name)
    graph.add_edge(a_node.name, END)

    return graph.compile()

Context schema:

class AgentContext(BaseModel):
    resource_ids: list[str]
    project_id: str
    user_id: str
    current_message: str

Tool definition:

@tool("semantic_search", description="Search for relevant documents using RAG.")
async def semantic_search(
    query: str,
    runtime: ToolRuntime[AgentContext],
    tool_call_id: Annotated[str, InjectedToolCallId],
):
    context = runtime.context
    resource_ids = context.resource_ids
    # RAG logic here

Agent initialization:

self._agent = custom_create_agent(
    model=self._model,
    state_schema=CustomAgentState,
    context_schema=AgentContext,
    tools=[semantic_search],
)

Agent usage:

async for chunk in self._agent.astream(
    {"messages": messages},
    config=runnable_config,
    stream_mode=["messages", "updates"],
    context=agent_context,
):
    yield chunk

Actual Behavior

When the agent invokes the semantic_search tool, the tool receives a malformed input (missing runtime) and throws a Pydantic validation error:

[ToolMessage(content="Error: 1 validation error for semantic_search
runtime
  Field required [type=missing, input_value={'query': 'summary overvi...4d29-9f5e-27ced45d80ed'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.12/v/missing
 Please fix your mistakes.", 
name='semantic_search', 
id='15c30ab9-09c8-4113-91a2-c091ac0300d0', 
tool_call_id='c30863cc-857e-4d29-9f5e-27ced45d80ed', 
status='error')]

Expected Behavior

ToolRuntime should be automatically instantiated and passed to the tool function when using StateGraph just like in standard LangChain or LangGraph agents (outside of graph execution).


Environment

  • langgraph: 1.0.1
  • langchain: 1.0.2
  • Python: 3.12.10
  • OS: MacOS

Possible Cause

It seems that StateGraph (or its ToolNode integration) does not properly inject the ToolRuntime context into the tool call when the graph is compiled.


Additional Notes

This issue prevents tools that rely on ToolRuntime[ContextT] from accessing the execution context, which breaks contextual RAG or similar workflows when using StateGraph.

I tried to do the same with the create_agent from langchain.agents and it worked well.

Originally created by @bachxoai on GitHub (Oct 28, 2025). Original GitHub issue: https://github.com/langchain-ai/docs/issues/1173 ### Type of issue issue / bug ### Language Python ### Description #### **Description** When using a `StateGraph`-based LangGraph agent that includes a tool expecting a `ToolRuntime` parameter, the runtime argument is **not passed correctly** to the tool at execution time, resulting in a Pydantic validation error. --- #### **Code Example** Here’s how the compiled graph is created: ```python def custom_create_agent( model: BaseChatModel, state_schema: type[AgentState[ResponseT]], context_schema: type[ContextT], tools: ToolType, ): graph = StateGraph( state_schema=state_schema, context_schema=context_schema, ) tool_node = ToolNode(tools) chatbot_node = AgentNode(model, tools) a_node = ANode(model) nodes = [tool_node, chatbot_node, a_node] for node in nodes: graph.add_node(getattr(node, "name", str(node)), node) graph.add_edge(START, chatbot_node.name) graph.add_conditional_edges(chatbot_node.name, after_chatbot_edge) graph.add_edge(tool_node.name, chatbot_node.name) graph.add_edge(a_node.name, END) return graph.compile() ``` Context schema: ```python class AgentContext(BaseModel): resource_ids: list[str] project_id: str user_id: str current_message: str ``` Tool definition: ```python @tool("semantic_search", description="Search for relevant documents using RAG.") async def semantic_search( query: str, runtime: ToolRuntime[AgentContext], tool_call_id: Annotated[str, InjectedToolCallId], ): context = runtime.context resource_ids = context.resource_ids # RAG logic here ``` Agent initialization: ```python self._agent = custom_create_agent( model=self._model, state_schema=CustomAgentState, context_schema=AgentContext, tools=[semantic_search], ) ``` Agent usage: ```python async for chunk in self._agent.astream( {"messages": messages}, config=runnable_config, stream_mode=["messages", "updates"], context=agent_context, ): yield chunk ``` --- #### **Actual Behavior** When the agent invokes the `semantic_search` tool, the tool receives a malformed input (missing `runtime`) and throws a Pydantic validation error: ``` [ToolMessage(content="Error: 1 validation error for semantic_search runtime Field required [type=missing, input_value={'query': 'summary overvi...4d29-9f5e-27ced45d80ed'}, input_type=dict] For further information visit https://errors.pydantic.dev/2.12/v/missing Please fix your mistakes.", name='semantic_search', id='15c30ab9-09c8-4113-91a2-c091ac0300d0', tool_call_id='c30863cc-857e-4d29-9f5e-27ced45d80ed', status='error')] ``` --- #### **Expected Behavior** `ToolRuntime` should be automatically instantiated and passed to the tool function when using `StateGraph` just like in standard LangChain or LangGraph agents (outside of graph execution). --- #### **Environment** * **langgraph**: `1.0.1` * **langchain**: `1.0.2` * **Python**: `3.12.10` * **OS**: `MacOS` --- #### **Possible Cause** It seems that `StateGraph` (or its `ToolNode` integration) does not properly inject the `ToolRuntime` context into the tool call when the graph is compiled. --- #### **Additional Notes** This issue prevents tools that rely on `ToolRuntime[ContextT]` from accessing the execution context, which breaks contextual RAG or similar workflows when using `StateGraph`. I tried to do the same with the `create_agent` from `langchain.agents` and it worked well.
yindo added the langchainexternal labels 2026-02-17 17:19:18 -05:00
Author
Owner

@e792a8 commented on GitHub (Nov 19, 2025):

ToolRuntime injection of ToolNode is now supported, at least my langgraph 1.0.2 works. So firstly, try upgrading.

But second, ToolRuntime[WithTypeArguments] annotation is still not supported. So for now, try removing the [TypeArgumets]. I am going to file a new issue for this.

@e792a8 commented on GitHub (Nov 19, 2025): `ToolRuntime` injection of `ToolNode` is [now supported](https://github.com/langchain-ai/langgraph/issues/6318#issuecomment-3463242273), at least my langgraph 1.0.2 works. So firstly, try upgrading. But second, `ToolRuntime[WithTypeArguments]` annotation is still not supported. So for now, try removing the `[TypeArgumets]`. I am going to file a new issue for this.
Author
Owner

@umair313 commented on GitHub (Dec 24, 2025):

ToolRuntime injection of ToolNode is now supported, at least my langgraph 1.0.2 works. So firstly, try upgrading.

But second, ToolRuntime[WithTypeArguments] annotation is still not supported. So for now, try removing the [TypeArgumets]. I am going to file a new issue for this.

@e792a8 what was your langchain and langchain_core versions? where it worked could you share?

@umair313 commented on GitHub (Dec 24, 2025): > `ToolRuntime` injection of `ToolNode` is [now supported](https://github.com/langchain-ai/langgraph/issues/6318#issuecomment-3463242273), at least my langgraph 1.0.2 works. So firstly, try upgrading. > > But second, `ToolRuntime[WithTypeArguments]` annotation is still not supported. So for now, try removing the `[TypeArgumets]`. I am going to file a new issue for this. @e792a8 what was your langchain and langchain_core versions? where it worked could you share?
Author
Owner

@Guemri-Jawher commented on GitHub (Jan 21, 2026):

I tried LangChain 1.0.7 and LangGraph 1.0.6, but they didn’t work properly. After downgrading to version 1.0.2, everything worked as expected.

You should note that tool_node = ToolNode(tools) has been replaced with tool_node = _ToolNode(tools)

@Guemri-Jawher commented on GitHub (Jan 21, 2026): I tried LangChain 1.0.7 and LangGraph 1.0.6, but they didn’t work properly. After downgrading to version 1.0.2, everything worked as expected. You should note that tool_node = ToolNode(tools) has been replaced with tool_node = _ToolNode(tools)
yindo changed title from [langgraph]: LangGraph StateGraph does not work with ToolRuntime to [GH-ISSUE #1173] [langgraph]: LangGraph StateGraph does not work with ToolRuntime 2026-06-05 17:25:22 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#158