-
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
base: master
Are you sure you want to change the base?
Conversation
…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.
| 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, |
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_output and then assign the name directly with output.name = agent_name
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.
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 :)
AIMessage responses when name parameter is provided
Description:
This PR fixes a bug where the
nameparameter provided tocreate_agent()was not being set onAIMessageobjects in the agent responses. Previously,AIMessage.namewas alwaysnullregardless of thenameparameter value, even though the parameter was documented to work.The fix:
_set_agent_name_on_message()helper function to set name on AIMessage objects_handle_model_output()to accept and useagent_nameparameter_execute_model_sync) and async (_execute_model_async) model execution paths to pass the name parameterThis ensures backward compatibility - when
nameis not provided,AIMessage.nameremainsNone.Issue:
Fixes the bug reported where
nameparameter increate_agent()was not reflected in AIMessage responses. (Add issue number if you have it)Dependencies:
None. No new dependencies required.