Repository navigation
fix(langchain): keep message names in generation input - #2038
Conversation
chrikrah
left a comment
There was a problem hiding this comment.
@mosafariuk I'd merge this at d9cec13. On langchain-core 1.2.22 the name lives on BaseMessage.name. The merge base reads only additional_kwargs, so a name set with name= never reached the input.
$ .venv/bin/python -m pytest tests/unit/test_langchain.py tests/unit/test_langchain_utils.py -q -p no:cacheprovider # d9cec13, langfuse from the tree
30 passed in 5.10s
# langfuse/langchain/CallbackHandler.py from 21834c5 (merge base), your test kept
FAILED tests/unit/test_langchain.py::test_chat_model_input_keeps_message_names
E At index 0 diff: ('user', None) != ('user', 'alice')
1 failed, 29 passed in 3.94s
# not run: the rest of tests/unit, mypy, and a live provider
non-blocking: _convert_message_to_dict also builds the generation output at CallbackHandler.py:1309. A named AIMessage from the model now keeps its name there too. That looks right to me, but no test pins it. One assertion on the output span would cover it.
@hassiebp when both are set, message.name now wins over additional_kwargs["name"]. I'd keep that order. The old value still shows, because additional_kwargs is copied in whole. Does any integration you know read name from the old place?
|
thanks for your contribution! |
What does this PR do?
The LangChain
CallbackHandlerdrops thenameof every message from the captured generation input._convert_message_to_dictonly readsmessage.additional_kwargs["name"]. Current LangChain stores the name on the message itself (BaseMessage.name), so it never reaches Langfuse. That includesHumanMessage(name="alice"),ToolMessage(name=...)andFunctionMessage, whosenameis required, even though LangChain does send those names to the provider. For example, with a realChatOpenAIand only the HTTP layer mocked:This PR reads
message.nameand falls back toadditional_kwargs["name"], so integrations that still put it there keep working. With the change:The langfuse-js counterpart (#950) describes this as matching what the Python SDK already does, so this brings the Python behaviour in line with that.
Fixes # (no existing issue)
Type of change
Verification
The new
test_chat_model_input_keeps_message_namesintests/unit/test_langchain.pyfollows the existing exporter-local pattern (patchedChatOpenAI._generate, in-memory exporter). It coversmessage.nameon human, tool and function messages, plus the legacyadditional_kwargs["name"]fallback. It fails onmain(('user', None) != ('user', 'alice')) and passes with this change.Checklist
code_review.md..env.templateif needed.

Confidence Score: 5/5
The PR appears safe to merge.Summary
The PR keeps message names in LangChain generation input.
No actionable issues were found. Tests were not run during this review.
Reviews (1) · Last reviewed commit: "fix(langchain): keep message names in ge..." · Reviewed by Greptile