-
Notifications
You must be signed in to change notification settings - Fork 19.6k
fix(langchain): set agent name on AIMessage responses when name parameter is provided
#33778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hak2979
wants to merge
3
commits into
langchain-ai:master
Choose a base branch
from
hak2979:fix/#33754
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+265
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
242 changes: 242 additions & 0 deletions
242
libs/langchain_v1/tests/unit_tests/agents/test_agent_name.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| """Test cases for agent name parameter in create_agent. | ||
|
|
||
| This module tests that the 'name' parameter provided to create_agent | ||
| is correctly set on AIMessage objects in the agent responses. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
| from langchain_core.messages import AIMessage, HumanMessage | ||
|
|
||
| from langchain.agents import create_agent | ||
| from tests.unit_tests.agents.model import FakeToolCallingModel | ||
|
|
||
|
|
||
| def test_create_agent_with_name_sets_name_on_ai_message() -> None: | ||
| """Test that providing a name to create_agent sets it on AIMessage responses.""" | ||
| agent_name = "Test Agent" | ||
| model = FakeToolCallingModel() | ||
|
|
||
| agent = create_agent( | ||
| model=model, | ||
| tools=[], | ||
| system_prompt="You are a helpful assistant", | ||
| name=agent_name, | ||
| ) | ||
|
|
||
| result = agent.invoke({"messages": [HumanMessage(content="Hello")]}) | ||
|
|
||
| # Verify that the AIMessage has the agent name set | ||
| ai_messages = [msg for msg in result["messages"] if isinstance(msg, AIMessage)] | ||
| assert len(ai_messages) > 0, "Expected at least one AIMessage in response" | ||
| assert ai_messages[-1].name == agent_name, ( | ||
| f"Expected AIMessage.name to be '{agent_name}', got '{ai_messages[-1].name}'" | ||
| ) | ||
|
|
||
|
|
||
| def test_create_agent_without_name_has_none_name() -> None: | ||
| """Test that not providing a name results in AIMessage with name=None (backward compatibility).""" | ||
| model = FakeToolCallingModel() | ||
|
|
||
| agent = create_agent( | ||
| model=model, | ||
| tools=[], | ||
| system_prompt="You are a helpful assistant", | ||
| # name not provided | ||
| ) | ||
|
|
||
| result = agent.invoke({"messages": [HumanMessage(content="Hello")]}) | ||
|
|
||
| # Verify that the AIMessage has name=None (backward compatibility) | ||
| ai_messages = [msg for msg in result["messages"] if isinstance(msg, AIMessage)] | ||
| assert len(ai_messages) > 0, "Expected at least one AIMessage in response" | ||
| assert ai_messages[-1].name is None, ( | ||
| f"Expected AIMessage.name to be None, got '{ai_messages[-1].name}'" | ||
| ) | ||
|
|
||
|
|
||
| def test_create_agent_with_name_and_tools() -> None: | ||
| """Test that agent name is set correctly when agent uses tools.""" | ||
| from langchain_core.tools import tool | ||
|
|
||
| @tool | ||
| def test_tool(input: str) -> str: | ||
| """A test tool.""" | ||
| return f"Result: {input}" | ||
|
|
||
| agent_name = "Tool Using Agent" | ||
| model = FakeToolCallingModel( | ||
| tool_calls=[ | ||
| [{"args": {"input": "test"}, "id": "1", "name": "test_tool"}], | ||
| [], # Second call has no tool calls | ||
| ] | ||
| ) | ||
|
|
||
| agent = create_agent( | ||
| model=model, | ||
| tools=[test_tool], | ||
| system_prompt="You are a helpful assistant", | ||
| name=agent_name, | ||
| ) | ||
|
|
||
| result = agent.invoke({"messages": [HumanMessage(content="Use the tool")]}) | ||
|
|
||
| # Verify that the final AIMessage has the agent name set | ||
| ai_messages = [msg for msg in result["messages"] if isinstance(msg, AIMessage)] | ||
| assert len(ai_messages) > 0, "Expected at least one AIMessage in response" | ||
| # The last AIMessage should have the name | ||
| assert ai_messages[-1].name == agent_name, ( | ||
| f"Expected AIMessage.name to be '{agent_name}', got '{ai_messages[-1].name}'" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_create_agent_with_name_async() -> None: | ||
| """Test that agent name is set correctly in async invocations.""" | ||
| agent_name = "Async Test Agent" | ||
| model = FakeToolCallingModel() | ||
|
|
||
| agent = create_agent( | ||
| model=model, | ||
| tools=[], | ||
| system_prompt="You are a helpful assistant", | ||
| name=agent_name, | ||
| ) | ||
|
|
||
| result = await agent.ainvoke({"messages": [HumanMessage(content="Hello")]}) | ||
|
|
||
| # Verify that the AIMessage has the agent name set | ||
| ai_messages = [msg for msg in result["messages"] if isinstance(msg, AIMessage)] | ||
| assert len(ai_messages) > 0, "Expected at least one AIMessage in response" | ||
| assert ai_messages[-1].name == agent_name, ( | ||
| f"Expected AIMessage.name to be '{agent_name}', got '{ai_messages[-1].name}'" | ||
| ) | ||
|
|
||
|
|
||
| def test_create_agent_name_preserved_across_tool_calls() -> None: | ||
| """Test that agent name is preserved in all AIMessage responses, including after tool calls.""" | ||
| from langchain_core.tools import tool | ||
|
|
||
| @tool | ||
| def calculator(expression: str) -> str: | ||
| """Evaluate a mathematical expression.""" | ||
| return f"Result: {expression}" | ||
|
|
||
| agent_name = "Calculator Agent" | ||
| # Model will make a tool call, then return a final response | ||
| model = FakeToolCallingModel( | ||
| tool_calls=[ | ||
| [{"args": {"expression": "2+2"}, "id": "1", "name": "calculator"}], | ||
| [], # Final response without tool calls | ||
| ] | ||
| ) | ||
|
|
||
| agent = create_agent( | ||
| model=model, | ||
| tools=[calculator], | ||
| system_prompt="You are a helpful calculator assistant", | ||
| name=agent_name, | ||
| ) | ||
|
|
||
| result = agent.invoke({"messages": [HumanMessage(content="What is 2+2?")]}) | ||
|
|
||
| # Verify all AIMessages have the agent name set | ||
| ai_messages = [msg for msg in result["messages"] if isinstance(msg, AIMessage)] | ||
| assert len(ai_messages) > 0, "Expected at least one AIMessage in response" | ||
| for ai_msg in ai_messages: | ||
| assert ai_msg.name == agent_name, ( | ||
| f"Expected all AIMessages to have name '{agent_name}', but got '{ai_msg.name}'" | ||
| ) | ||
|
|
||
|
|
||
| def test_create_agent_name_not_overwritten_if_already_set() -> None: | ||
| """Test that if AIMessage already has a name, it is not overwritten.""" | ||
| # This test ensures backward compatibility - if a model returns an AIMessage | ||
| # with a name already set, we should preserve it rather than overwrite it | ||
| agent_name = "Test Agent" | ||
|
|
||
| # Create a custom model that returns AIMessage with name already set | ||
| class ModelWithNamedResponse(FakeToolCallingModel): | ||
| def _generate(self, messages, **kwargs): | ||
| message = AIMessage( | ||
| content="Response with existing name", | ||
| name="ModelSetName", # Model sets its own name | ||
| ) | ||
| from langchain_core.outputs import ChatGeneration, ChatResult | ||
|
|
||
| return ChatResult(generations=[ChatGeneration(message=message)]) | ||
|
|
||
| model = ModelWithNamedResponse() | ||
|
|
||
| agent = create_agent( | ||
| model=model, | ||
| tools=[], | ||
| system_prompt="You are a helpful assistant", | ||
| name=agent_name, | ||
| ) | ||
|
|
||
| result = agent.invoke({"messages": [HumanMessage(content="Hello")]}) | ||
|
|
||
| # The message already has a name, so it should be preserved | ||
| ai_messages = [msg for msg in result["messages"] if isinstance(msg, AIMessage)] | ||
| assert len(ai_messages) > 0 | ||
| # If the model already set a name, it should be preserved | ||
| # (though in practice, most models don't set names) | ||
| # This test verifies our code doesn't overwrite existing names | ||
| assert ai_messages[-1].name is not None | ||
|
|
||
|
|
||
| def test_create_agent_with_empty_string_name() -> None: | ||
| """Test that empty string name is handled correctly (should still set it).""" | ||
| model = FakeToolCallingModel() | ||
|
|
||
| agent = create_agent( | ||
| model=model, | ||
| tools=[], | ||
| system_prompt="You are a helpful assistant", | ||
| name="", # Empty string | ||
| ) | ||
|
|
||
| result = agent.invoke({"messages": [HumanMessage(content="Hello")]}) | ||
|
|
||
| ai_messages = [msg for msg in result["messages"] if isinstance(msg, AIMessage)] | ||
| assert len(ai_messages) > 0 | ||
| # Empty string should still be set (not None) | ||
| # Though this might not be a realistic use case | ||
| assert ai_messages[-1].name == "" | ||
|
|
||
|
|
||
| def test_create_agent_no_name_parameter_with_tools() -> None: | ||
| """Test that agent works correctly when name parameter is completely omitted, even with tools.""" | ||
| from langchain_core.tools import tool | ||
|
|
||
| @tool | ||
| def test_tool(input: str) -> str: | ||
| """A test tool.""" | ||
| return f"Result: {input}" | ||
|
|
||
| model = FakeToolCallingModel( | ||
| tool_calls=[ | ||
| [{"args": {"input": "test"}, "id": "1", "name": "test_tool"}], | ||
| [], # Second call has no tool calls | ||
| ] | ||
| ) | ||
|
|
||
| # Explicitly not passing name parameter at all | ||
| agent = create_agent( | ||
| model=model, | ||
| tools=[test_tool], | ||
| system_prompt="You are a helpful assistant", | ||
| ) | ||
|
|
||
| result = agent.invoke({"messages": [HumanMessage(content="Use the tool")]}) | ||
|
|
||
| # Verify that the AIMessage has name=None when name is not provided | ||
| ai_messages = [msg for msg in result["messages"] if isinstance(msg, AIMessage)] | ||
| assert len(ai_messages) > 0, "Expected at least one AIMessage in response" | ||
| # All AIMessages should have name=None when name parameter is not provided | ||
| for ai_msg in ai_messages: | ||
| assert ai_msg.name is None, ( | ||
| f"Expected AIMessage.name to be None when name parameter is omitted, got '{ai_msg.name}'" | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need to create a new AIMessage object here. You could just add your validation on
_handle_model_outputand then assign the name directly withoutput.name = agent_nameThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, while this change reduces the number of lines, the main reason for it was to enforce a fixed structure, which improves overall maintainability, readability, and consistency across the codebase. Please let me know if I’ve misunderstood anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair, but i see other places where we are updating just one field from AIMessage and it's using direct assignment. However, it's just a nit, so up to you :)