Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/langchain/src/CallbackHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,9 @@ export class CallbackHandler extends BaseCallbackHandler {
} else if (message.getType() === "generic") {
response = {
content: message.content,
role: "human",
// ChatMessage (getType() === "generic") carries a custom role; preserve
// it instead of hardcoding "human" (regression introduced in #680).
role: (message as any).role ?? "human",
};
} else if (message.getType() === "ai") {
response = { content: message.content, role: "assistant" };
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/langchain.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ChatMessage } from "@langchain/core/messages";
import { DynamicTool } from "@langchain/core/tools";
import { CallbackHandler } from "@langfuse/langchain";
import { LangfuseOtelSpanAttributes } from "@langfuse/tracing";
Expand All @@ -24,6 +25,16 @@ describe("LangChain callback handler integration tests", () => {
await teardownTestEnvironment(testEnv);
});

it("should preserve custom roles for ChatMessage (generic) inputs", () => {
const handler = new CallbackHandler();

const mapped = (handler as any).extractChatMessageContent(
new ChatMessage({ content: "hello", role: "developer" }),
);

expect(mapped).toEqual({ content: "hello", role: "developer" });
});

it("should mark LangChain tool runs as tool observations", async () => {
const calculatorTool = new DynamicTool({
name: "calculator",
Expand Down