You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
fromtypingimportAnnotated, Anyfromlangchain_core.messagesimportToolMessagefromlangchain_core.runnables.configimportRunnableConfigfromlangchain_core.toolsimporttoolfromlangchain_core.tools.baseimportInjectedToolCallIdfromlangchain_openaiimportChatOpenAIfromlanggraph.prebuiltimportcreate_react_agentfromlanggraph.prebuilt.chat_agent_executorimportAgentStatefromlanggraph.typesimportCommandUSER_INFO= [
{"user_id": "1", "name": "Bob Dylan", "location": "New York, NY"},
{"user_id": "2", "name": "Taylor Swift", "location": "Beverly Hills, CA"},
]
USER_ID_TO_USER_INFO= {info["user_id"]: infoforinfoinUSER_INFO}
classState(AgentState):
# updated by the tooluser_info: dict[str, Any]
defmain() ->None:
@tooldeflookup_user_info(tool_call_id: Annotated[str, InjectedToolCallId], config: RunnableConfig):
"""Use this to look up user information to better assist them with their questions."""user_id=config.get("configurable", {}).get("user_id")
ifuser_idisNone:
raiseValueError("Please provide user ID")
ifuser_idnotinUSER_ID_TO_USER_INFO:
raiseValueError(f"User '{user_id}' not found")
user_info=USER_ID_TO_USER_INFO[user_id]
returnCommand(
update={
# update the state keys"user_info": user_info,
# update the message history"messages": [
ToolMessage(
"Successfully looked up user information", tool_call_id=tool_call_id
)
],
}
)
defprompt(state: State):
user_info=state.get("user_info")
ifuser_infoisNone:
returnstate["messages"]
system_msg= (
f"User name is {user_info['name']}. User lives in {user_info['location']}"
)
return [{"role": "system", "content": system_msg}] +state["messages"]
model=ChatOpenAI(model="gpt-4o")
agent=create_react_agent(
model,
# pass the tool that can update state
[lookup_user_info],
state_schema=State,
# pass dynamic prompt functionprompt=prompt,
)
agent_input= {"messages": [("user", "hi, where do I live?")]}
agent_config= {"configurable": {"user_id": "1"}}
invoke_result=agent.invoke(
agent_input,
agent_config,
)
# print(invoke_result)forchunkinagent.stream(agent_input, agent_config, stream_mode='messages'):
print(chunk)
if__name__=='__main__':
main()
Error Message and Stack Trace (if applicable)
Description
As the title says, if you define a tool returning a Command to update the state, there is no ToolMessage for the tool call when using the messages streaming mode.
httpx: 0.28.1
jsonpatch<2.0,>=1.33: Installed. No version info available.
langchain-core<1.0.0,>=0.3.49: Installed. No version info available.
langsmith-pyo3: Installed. No version info available.
langsmith<0.4,>=0.1.125: Installed. No version info available.
openai-agents: Installed. No version info available.
openai<2.0.0,>=1.68.2: Installed. No version info available.
opentelemetry-api: Installed. No version info available.
opentelemetry-exporter-otlp-proto-http: Installed. No version info available.
opentelemetry-sdk: Installed. No version info available.
orjson: 3.10.16
packaging: 24.2
packaging<25,>=23.2: Installed. No version info available.
pydantic: 2.11.1
pydantic<3.0.0,>=2.5.2;: Installed. No version info available.
pydantic<3.0.0,>=2.7.4;: Installed. No version info available.
pytest: Installed. No version info available.
PyYAML>=5.3: Installed. No version info available.
requests: 2.32.3
requests-toolbelt: 1.0.0
rich: Installed. No version info available.
tenacity!=8.4.0,<10.0.0,>=8.1.0: Installed. No version info available.
tiktoken<1,>=0.7: Installed. No version info available.
typing-extensions>=4.7: Installed. No version info available.
zstandard: 0.23.0
The text was updated successfully, but these errors were encountered:
Indeed it does not return a message as the function didn't return a message and simply updated the state of the graph using Command. Still you can access the ToolMessage by using also the stream_mode 'updates'.
for chunk in agent.stream(agent_input, agent_config, stream_mode=['messages', 'updates']):
print(chunk)
Then, you may find among the chunks a structure like:
('updates', {'tools': {'user_info': {'user_id': '1', 'name': 'Bob Dylan', 'location': 'New York, NY'}, 'messages': [ToolMessage(content='Successfully looked up user information', name='lookup_user_info', tool_call_id='call_rwUieKngmiAAUy2vnZJaOlEq')]}})
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
As the title says, if you define a tool returning a
Command
to update the state, there is no ToolMessage for the tool call when using themessages
streaming mode.This is easily reproducible using the example in the How to update graph state from tools doc page.
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies
The text was updated successfully, but these errors were encountered: