fix(langchain): preserve custom ChatMessage role instead of hardcoding "human"#833
Open
i-anubhav-anand wants to merge 1 commit into
Open
fix(langchain): preserve custom ChatMessage role instead of hardcoding "human"#833i-anubhav-anand wants to merge 1 commit into
i-anubhav-anand wants to merge 1 commit into
Conversation
…g "human" LangChain ChatMessage (getType() === "generic") is the message class for arbitrary custom roles (e.g. "developer"). extractChatMessageContent hardcoded role: "human" for it, discarding the real role — a regression from langfuse#680, which replaced the prior `role: message.role` with the literal "human" when switching from instanceof ChatMessage to getType() checks. Restores `message.role` (falling back to "human" only when absent). Adds a unit test that maps a ChatMessage with role "developer" (fails before: role was "human").
|
@i-anubhav-anand is attempting to deploy a commit to the langfuse Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
LangChain
ChatMessage(getType() === "generic") is the message class for arbitrary custom roles (e.g."developer","user_proxy").extractChatMessageContenthardcodes the role for it:So
new ChatMessage({ content: "hi", role: "developer" })is traced asrole: "human". Every other branch (human/ai/system/function/tool) maps the role correctly.This is a regression from #680, which swapped
instanceof ChatMessageforgetType() === "generic"and replaced the originalrole: message.rolewith the literal"human".Fix restores
message.role(falling back to"human"only when absent).Test
Adds a unit test in
tests/integration/langchain.integration.test.tsmapping aChatMessagewith role"developer". Fails before (role: "human"), passes after.prettier/eslintclean.Type of change
Greptile Summary
Fixes a regression introduced in PR #680 where
ChatMessageobjects with custom roles (e.g."developer","user_proxy") were always traced asrole: "human"instead of their actual role.CallbackHandler.ts: InextractChatMessageContent, the"generic"branch now reads(message as any).role ?? "human"instead of hardcoding"human". ImportingChatMessagefrom the already-used@langchain/core/messagespackage would replace theas anycast with a properly typed assertion.langchain.integration.test.ts: Adds a test verifying aChatMessagewithrole: "developer"round-trips correctly; the test is self-contained (no network/server needed) and would be better placed in a unit test file.Confidence Score: 4/5
The fix is safe to merge — it corrects a clear role-preservation bug with a minimal, targeted change and a covering test.
The core logic change is correct and well-scoped. The only rough edge is using
as anywhen a properChatMessagetype is already available from the imported package, which leaves the.roleaccess unchecked at compile time. The new test lives in the integration suite despite requiring no external dependencies, so it may not run in fast unit-test CI jobs. Neither issue affects runtime correctness of the fix.The
as anycast inCallbackHandler.tsline 936 is the only spot worth a second look; the test file placement is a minor workflow concern.Sequence Diagram
sequenceDiagram participant LC as LangChain participant CB as CallbackHandler participant LF as Langfuse LC->>CB: onLLMStart / onChatModelStart(messages: BaseMessage[]) CB->>CB: extractChatMessageContent(message) alt "message.getType() === "human"" CB-->>CB: "role = "user"" else "message.getType() === "generic" (ChatMessage)" Note over CB: BEFORE: role hardcoded to "human"<br/>AFTER: role = message.role ?? "human" CB-->>CB: "role = message.role ?? "human"" else "message.getType() === "ai"" CB-->>CB: "role = "assistant"" else "message.getType() === "system"" CB-->>CB: "role = "system"" end CB->>LF: trace with correct role preservedPrompt To Fix All With AI
Reviews (1): Last reviewed commit: "fix(langchain): preserve custom ChatMess..." | Re-trigger Greptile