Skip to content

Commit 5b27f38

Browse files
maslo55555hntrl
andauthored
fix(@langchain/google-genai): support custom agent names in createAgent (#9583)
Co-authored-by: Hunter Lovell <[email protected]> Co-authored-by: Hunter Lovell <[email protected]>
1 parent f4ef9a1 commit 5b27f38

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

.changeset/great-dingos-exist.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@langchain/google-genai": patch
3+
---
4+
5+
fix(google-genai): support custom agent names in createAgent

libs/providers/langchain-google-genai/src/tests/chat_models.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { removeAdditionalProperties } from "../utils/zod_to_genai_parameters.js"
1515
import {
1616
convertBaseMessagesToContent,
1717
convertMessageContentToParts,
18+
getMessageAuthor,
1819
} from "../utils/common.js";
1920

2021
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -962,3 +963,43 @@ test("convertMessageContentToParts: correctly handles ToolMessage with array con
962963
},
963964
]);
964965
});
966+
967+
test("getMessageAuthor should return message type, not custom names", () => {
968+
const aiMessageWithName = new AIMessage({
969+
content: "Hello",
970+
name: "math_expert",
971+
});
972+
expect(getMessageAuthor(aiMessageWithName)).toBe("ai");
973+
974+
const aiMessage = new AIMessage({
975+
content: "Hello",
976+
});
977+
expect(getMessageAuthor(aiMessage)).toBe("ai");
978+
979+
const humanMessage = new HumanMessage({
980+
content: "Hello",
981+
});
982+
expect(getMessageAuthor(humanMessage)).toBe("human");
983+
984+
const toolMessage = new ToolMessage({
985+
content: "Result",
986+
tool_call_id: "123",
987+
});
988+
expect(getMessageAuthor(toolMessage)).toBe("tool");
989+
});
990+
991+
test("convertBaseMessagesToContent should handle AIMessages with custom names", () => {
992+
const messages = [
993+
new HumanMessage({ content: "What is 2+2?" }),
994+
new AIMessage({ content: "4", name: "math_expert" }),
995+
];
996+
997+
expect(() =>
998+
convertBaseMessagesToContent(messages, true, false, "model")
999+
).not.toThrow();
1000+
1001+
const result = convertBaseMessagesToContent(messages, true, false, "model");
1002+
expect(result).toHaveLength(2);
1003+
expect(result[0].role).toBe("user");
1004+
expect(result[1].role).toBe("model");
1005+
});

libs/providers/langchain-google-genai/src/utils/common.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,10 @@ const DUMMY_SIGNATURE =
5353
const iife = (fn: () => string) => fn();
5454

5555
export function getMessageAuthor(message: BaseMessage) {
56-
const type = message._getType();
5756
if (ChatMessage.isInstance(message)) {
5857
return message.role;
5958
}
60-
if (type === "tool") {
61-
return type;
62-
}
63-
return message.name ?? type;
59+
return message.type;
6460
}
6561

6662
/**
@@ -79,7 +75,7 @@ export function convertAuthorToRole(
7975
* */
8076
case "supervisor":
8177
case "ai":
82-
case "model": // getMessageAuthor returns message.name. code ex.: return message.name ?? type;
78+
case "model":
8379
return "model";
8480
case "system":
8581
return "system";

0 commit comments

Comments
 (0)