Missing id in tool_calls of AIMessage when using stream_mode="messages" (Issue does not occur with other modes) #351

Closed
opened 2026-02-20 17:38:01 -05:00 by yindo · 3 comments
Owner

Originally created by @kangyic116 on GitHub (Dec 11, 2024).

Checked other resources

  • This is a bug, not a usage question. For questions, please use GitHub Discussions.
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

Example Code

from langgraph.graph import StateGraph
from langgraph.graph.message import add_messages
from langgraph.graph.state import CompiledStateGraph
from langgraph.prebuilt import ToolNode, tools_condition

def make_graph(llm: ChatOpenAI, tools=list[StructuredTool]) -> CompiledStateGraph:
    class State(TypedDict):
        messages: Annotated[list, add_messages]
    
    graph_builder = StateGraph(State)
    llm_with_tools = llm.bind_tools(tools)

    def chatbot(state: State):
        response = llm_with_tools.invoke(state["messages"])
        return {"messages": [response]}

    tool_node = ToolNode(tools)
    graph_builder.add_node("tools", tool_node)
    graph_builder.add_node("chatbot", chatbot)
    graph_builder.add_edge("tools", "chatbot")
    graph_builder.add_conditional_edges(
        "chatbot",
        tools_condition
    )
    graph_builder.set_entry_point("chatbot")
    graph = graph_builder.compile()
    return graph

def get_tools() -> list[StructuredTool]:
    class CalculatorInput(BaseModel):
        a: int = Field(description="first number")
        b: int = Field(description="second number")

    def multiply(a: int, b: int) -> int:
        """Multiply two numbers."""
        return a * b

    calculator = StructuredTool.from_function(
        func=multiply,
        name="Calculator",
        description="multiply numbers",
        args_schema=CalculatorInput,
        return_direct=True
    )
    tools = [calculator]
    return tools
# --------------
# issue occurs when streaming
for event in graph.stream({"messages": input}, stream_mode="messages"): 
    print(event)

Error Message and Stack Trace (if applicable)

File "/home/app.e0016/pattern_agent/agent.py", line 174, in stream_graph_updates_test
    for event in graph.stream({"messages": input}, stream_mode="messages"):
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/pregel/__init__.py", line 1656, in stream
    for _ in runner.tick(
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/pregel/runner.py", line 239, in tick
    _panic_or_proceed(
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/pregel/runner.py", line 539, in _panic_or_proceed
    raise exc
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/pregel/executor.py", line 76, in done
    task.result()
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 451, in result
    return self.__get_result()
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/pregel/retry.py", line 40, in run_with_retry
    return task.proc.invoke(task.input, config)
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/utils/runnable.py", line 408, in invoke
    input = step.invoke(input, config, **kwargs)
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/prebuilt/tool_node.py", line 247, in invoke
    return super().invoke(input, config, **kwargs)
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/utils/runnable.py", line 184, in invoke
    ret = context.run(self.func, input, **kwargs)
  File "/home/app.e0016421/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/prebuilt/tool_node.py", line 219, in _func
    outputs = [
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 621, in result_iterator
    yield _result_or_cancel(fs.pop())
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 319, in _result_or_cancel
    return fut.result(timeout)
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 451, in result
    return self.__get_result()
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
    raise self._exception
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langchain_core/runnables/config.py", line 527, in _wrapped_fn
    return contexts.pop().run(fn, *args)
  File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/prebuilt/tool_node.py", line 341, in _run_one
    raise TypeError(
TypeError: Tool Calculator returned unexpected type: <class 'int'>

Description

When using stream_mode set to "messages", the id field in the tool_calls property of the AIMessage in the input to the tool node's invoke function is missing. How can this issue be fixed? (This issue does not occur with other stream_mode settings.)

System Info

System Information

OS: Linux
OS Version: #161-Ubuntu SMP Fri Feb 3 14:49:04 UTC 2023
Python Version: 3.10.15 (main, Oct 3 2024, 07:27:34) [GCC 11.2.0]

Package Information

langchain_core: 0.3.24
langchain: 0.3.11
langchain_community: 0.3.11
langsmith: 0.2.2
langchain_openai: 0.2.12
langchain_text_splitters: 0.3.2
langgraph_sdk: 0.1.43

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.11.10
async-timeout: 4.0.3
dataclasses-json: 0.6.7
httpx: 0.28.0
httpx-sse: 0.4.0
jsonpatch: 1.33
langsmith-pyo3: Installed. No version info available.
numpy: 1.26.4
openai: 1.57.0
orjson: 3.10.12
packaging: 24.2
pydantic: 2.10.3
pydantic-settings: 2.6.1
PyYAML: 6.0.2
requests: 2.32.3
requests-toolbelt: 1.0.0
SQLAlchemy: 2.0.36
tenacity: 9.0.0
tiktoken: 0.8.0
typing-extensions: 4.12.2

Originally created by @kangyic116 on GitHub (Dec 11, 2024). ### Checked other resources - [X] This is a bug, not a usage question. For questions, please use GitHub Discussions. - [X] I added a clear and detailed title that summarizes the issue. - [X] I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example). - [X] I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue. ### Example Code ```python from langgraph.graph import StateGraph from langgraph.graph.message import add_messages from langgraph.graph.state import CompiledStateGraph from langgraph.prebuilt import ToolNode, tools_condition def make_graph(llm: ChatOpenAI, tools=list[StructuredTool]) -> CompiledStateGraph: class State(TypedDict): messages: Annotated[list, add_messages] graph_builder = StateGraph(State) llm_with_tools = llm.bind_tools(tools) def chatbot(state: State): response = llm_with_tools.invoke(state["messages"]) return {"messages": [response]} tool_node = ToolNode(tools) graph_builder.add_node("tools", tool_node) graph_builder.add_node("chatbot", chatbot) graph_builder.add_edge("tools", "chatbot") graph_builder.add_conditional_edges( "chatbot", tools_condition ) graph_builder.set_entry_point("chatbot") graph = graph_builder.compile() return graph def get_tools() -> list[StructuredTool]: class CalculatorInput(BaseModel): a: int = Field(description="first number") b: int = Field(description="second number") def multiply(a: int, b: int) -> int: """Multiply two numbers.""" return a * b calculator = StructuredTool.from_function( func=multiply, name="Calculator", description="multiply numbers", args_schema=CalculatorInput, return_direct=True ) tools = [calculator] return tools # -------------- # issue occurs when streaming for event in graph.stream({"messages": input}, stream_mode="messages"): print(event) ``` ### Error Message and Stack Trace (if applicable) ```shell File "/home/app.e0016/pattern_agent/agent.py", line 174, in stream_graph_updates_test for event in graph.stream({"messages": input}, stream_mode="messages"): File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/pregel/__init__.py", line 1656, in stream for _ in runner.tick( File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/pregel/runner.py", line 239, in tick _panic_or_proceed( File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/pregel/runner.py", line 539, in _panic_or_proceed raise exc File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/pregel/executor.py", line 76, in done task.result() File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 451, in result return self.__get_result() File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result raise self._exception File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/pregel/retry.py", line 40, in run_with_retry return task.proc.invoke(task.input, config) File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/utils/runnable.py", line 408, in invoke input = step.invoke(input, config, **kwargs) File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/prebuilt/tool_node.py", line 247, in invoke return super().invoke(input, config, **kwargs) File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/utils/runnable.py", line 184, in invoke ret = context.run(self.func, input, **kwargs) File "/home/app.e0016421/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/prebuilt/tool_node.py", line 219, in _func outputs = [ File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 621, in result_iterator yield _result_or_cancel(fs.pop()) File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 319, in _result_or_cancel return fut.result(timeout) File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 451, in result return self.__get_result() File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result raise self._exception File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langchain_core/runnables/config.py", line 527, in _wrapped_fn return contexts.pop().run(fn, *args) File "/home/app.e0016/miniconda3/envs/agent/lib/python3.10/site-packages/langgraph/prebuilt/tool_node.py", line 341, in _run_one raise TypeError( TypeError: Tool Calculator returned unexpected type: <class 'int'> ``` ### Description When using stream_mode set to "messages", the id field in the tool_calls property of the AIMessage in the input to the tool node's invoke function is missing. How can this issue be fixed? (This issue does not occur with other stream_mode settings.) ### System Info System Information ------------------ > OS: Linux > OS Version: #161-Ubuntu SMP Fri Feb 3 14:49:04 UTC 2023 > Python Version: 3.10.15 (main, Oct 3 2024, 07:27:34) [GCC 11.2.0] Package Information ------------------- > langchain_core: 0.3.24 > langchain: 0.3.11 > langchain_community: 0.3.11 > langsmith: 0.2.2 > langchain_openai: 0.2.12 > langchain_text_splitters: 0.3.2 > langgraph_sdk: 0.1.43 Optional packages not installed ------------------------------- > langserve Other Dependencies ------------------ > aiohttp: 3.11.10 > async-timeout: 4.0.3 > dataclasses-json: 0.6.7 > httpx: 0.28.0 > httpx-sse: 0.4.0 > jsonpatch: 1.33 > langsmith-pyo3: Installed. No version info available. > numpy: 1.26.4 > openai: 1.57.0 > orjson: 3.10.12 > packaging: 24.2 > pydantic: 2.10.3 > pydantic-settings: 2.6.1 > PyYAML: 6.0.2 > requests: 2.32.3 > requests-toolbelt: 1.0.0 > SQLAlchemy: 2.0.36 > tenacity: 9.0.0 > tiktoken: 0.8.0 > typing-extensions: 4.12.2
yindo closed this issue 2026-02-20 17:38:01 -05:00
Author
Owner

@vbarda commented on GitHub (Dec 12, 2024):

Which version of langgraph are you using? I tried running on the latest version (0.2.59) and cannot reproduce the issue

model = ChatOpenAI(model="gpt-4o", api_key="xxx")
tools = get_tools()
graph = make_graph(model, tools)

# issue occurs when streaming
for event in graph.stream({"messages": [("user", "what's 3 times 5")]}, stream_mode="messages"): 
    print(event)

the error you're seeing does suggest that the tool call ID is not being correctly passed to the tool.invoke() as it's returning a raw tool output instead of a ToolMessage

@vbarda commented on GitHub (Dec 12, 2024): Which version of langgraph are you using? I tried running on the latest version (0.2.59) and cannot reproduce the issue ```python model = ChatOpenAI(model="gpt-4o", api_key="xxx") tools = get_tools() graph = make_graph(model, tools) # issue occurs when streaming for event in graph.stream({"messages": [("user", "what's 3 times 5")]}, stream_mode="messages"): print(event) ``` the error you're seeing does suggest that the tool call ID is not being correctly passed to the `tool.invoke()` as it's returning a raw tool output instead of a `ToolMessage`
Author
Owner

@kangyic116 commented on GitHub (Dec 12, 2024):

I‘m using the latest version. Since I'm using a locally launched API service (started via vLLM), could this be the cause of the error?
llm = ChatOpenAI( model="Qwen1.5-72B", base_url="http://localhost:8001/v1/", api_key="EMPTY", temperature=0)
It seems the only difference is the model. If I want to use a local model instead of GPT, is there a good way to do that?
Would using ChatOllama be an option?

Thank you for your response!

langchain 0.3.11
langchain-community 0.3.11
langchain-core 0.3.24
langchain-openai 0.2.12
langchain-text-splitters 0.3.2
langgraph 0.2.59
langgraph-checkpoint 2.0.8
langgraph-sdk 0.1.43
langsmith 0.2.3

@kangyic116 commented on GitHub (Dec 12, 2024): I‘m using the latest version. Since I'm using a locally launched API service (started via vLLM), could this be the cause of the error? `llm = ChatOpenAI( model="Qwen1.5-72B", base_url="http://localhost:8001/v1/", api_key="EMPTY", temperature=0)` It seems the only difference is the model. If I want to use a local model instead of GPT, is there a good way to do that? Would using ChatOllama be an option? Thank you for your response! langchain 0.3.11 langchain-community 0.3.11 langchain-core 0.3.24 langchain-openai 0.2.12 langchain-text-splitters 0.3.2 langgraph 0.2.59 langgraph-checkpoint 2.0.8 langgraph-sdk 0.1.43 langsmith 0.2.3
Author
Owner

@vbarda commented on GitHub (Dec 18, 2024):

yea, using a custom API could be the problem, I suspect that the issue is actually in missing tool call IDs in the preceding AI message

for local models ChatOllama is a good option if that works for you!

either way, it doesn't seem like this is an issue with langgraph, so will close this issue, but feel free to reopen if you run into this again with ChatOllama / other providers

@vbarda commented on GitHub (Dec 18, 2024): yea, using a custom API could be the problem, I suspect that the issue is actually in missing tool call IDs in the preceding AI message for local models `ChatOllama` is a good option if that works for you! either way, it doesn't seem like this is an issue with langgraph, so will close this issue, but feel free to reopen if you run into this again with `ChatOllama` / other providers
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#351