Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@hak2979
Copy link

@hak2979 hak2979 commented Nov 1, 2025

Description:

This PR fixes a bug where the name parameter provided to create_agent() was not being set on AIMessage objects in the agent responses. Previously, AIMessage.name was always null regardless of the name parameter value, even though the parameter was documented to work.

The fix:

  • Added _set_agent_name_on_message() helper function to set name on AIMessage objects
  • Updated _handle_model_output() to accept and use agent_name parameter
  • Updated both sync (_execute_model_sync) and async (_execute_model_async) model execution paths to pass the name parameter
  • Added comprehensive test coverage with 8 test cases

This ensures backward compatibility - when name is not provided, AIMessage.name remains None.

Issue:

Fixes the bug reported where name parameter in create_agent() was not reflected in AIMessage responses. (Add issue number if you have it)

Dependencies:

None. No new dependencies required.

…vided

- Added _set_agent_name_on_message helper function to set name on AIMessage objects
- Updated _handle_model_output to accept and use agent_name parameter
- Updated _execute_model_sync and _execute_model_async to pass name parameter
- Added comprehensive test coverage (8 test cases) for agent name functionality

Previously, the name parameter in create_agent() was only used for naming the
compiled graph, not for setting AIMessage.name in responses. This fix ensures
that when a name is provided, it is correctly reflected in all AIMessage
responses from the agent.

Fixes: langchain-ai#33754
…gent_name.py

- Reformatted function parameters in _handle_model_output for better clarity.
- Cleaned up whitespace in test cases to enhance readability.
- Ensured consistent assertion formatting in tests for agent name verification.

These changes aim to improve code maintainability and readability without altering functionality.
@github-actions github-actions bot added langchain Related to the package `langchain` v1 Issue specific to LangChain 1.0 fix labels Nov 1, 2025
Comment on lines +810 to +818
return AIMessage(
content=msg.content,
tool_calls=msg.tool_calls,
invalid_tool_calls=msg.invalid_tool_calls,
additional_kwargs=msg.additional_kwargs,
response_metadata=msg.response_metadata,
usage_metadata=msg.usage_metadata,
name=agent_name,
id=msg.id,

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_output and then assign the name directly with output.name = agent_name

Copy link
Author

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.

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix langchain Related to the package `langchain` v1 Issue specific to LangChain 1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants