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)