fix(@langchain/google-genai): support custom agent names in createAgent #9583
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.
Description
Fixes an issue where using
createAgentwith a customnameparameter (e.g.,name: 'my_agent') causes Google GenAI to throw an error:Unknown / unsupported author: <custom_agent_name>.Problem
When creating a ReactAgent with a custom name, the agent sets this name on AIMessage objects. However, Google GenAI's
getMessageAuthorfunction was returning the custom name instead of the message type, causingconvertAuthorToRoleto fail since Google GenAI only supports standard roles:model,user,function, andsystem.Solution
Modified
getMessageAuthorinlibs/providers/langchain-google-genai/src/utils/common.tsto always return the message type, ignoring custom names which are metadata for tracking in multi-agent scenarios but not supported by Google GenAI's API.Changes
getMessageAuthorto return message type instead ofmessage.name ?? typeTesting
Added unit tests in
chat_models.test.ts:getMessageAuthorreturns message type for AIMessages with custom namesconvertBaseMessagesToContenthandles messages with custom agent names without throwing errorsExample
After this fix, the following code works correctly: