From eb19b80d1328cc4e32f21352882fb4e23d94bd8d Mon Sep 17 00:00:00 2001 From: Vadym Barda Date: Thu, 6 Feb 2025 17:24:53 -0500 Subject: [PATCH] langgraph: add agent name to AI messages in `create_react_agent` (#3340) --- libs/langgraph/langgraph/prebuilt/chat_agent_executor.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py b/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py index aaf3ae074..04d18b1d4 100644 --- a/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py +++ b/libs/langgraph/langgraph/prebuilt/chat_agent_executor.py @@ -636,7 +636,9 @@ def create_react_agent( # Define the function that calls the model def call_model(state: AgentState, config: RunnableConfig) -> AgentState: _validate_chat_history(state["messages"]) - response = model_runnable.invoke(state, config) + response = cast(AIMessage, model_runnable.invoke(state, config)) + # add agent name to the AIMessage + response.name = name has_tool_calls = isinstance(response, AIMessage) and response.tool_calls all_tools_return_direct = ( all(call["name"] in should_return_direct for call in response.tool_calls) @@ -673,7 +675,9 @@ def create_react_agent( async def acall_model(state: AgentState, config: RunnableConfig) -> AgentState: _validate_chat_history(state["messages"]) - response = await model_runnable.ainvoke(state, config) + response = cast(AIMessage, await model_runnable.ainvoke(state, config)) + # add agent name to the AIMessage + response.name = name has_tool_calls = isinstance(response, AIMessage) and response.tool_calls all_tools_return_direct = ( all(call["name"] in should_return_direct for call in response.tool_calls)