fix(langchain): pass trace_name to propagate_attributes() in on_chain_start#1608
Open
Spectual wants to merge 1 commit intolangfuse:mainfrom
Open
fix(langchain): pass trace_name to propagate_attributes() in on_chain_start#1608Spectual wants to merge 1 commit intolangfuse:mainfrom
Spectual wants to merge 1 commit intolangfuse:mainfrom
Conversation
…_start When a LangChain chain starts at the root level, `on_chain_start` was not passing `trace_name` to `propagate_attributes()`, so the trace name was not being set correctly for the resulting trace. Fixes langfuse#1602
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.
Problem
In
CallbackHandler.on_chain_start, when a LangChain chain starts at the root level (parent_run_id is None),propagate_attributes()is called to set trace-level attributes. However,trace_namewas not being passed topropagate_attributes(), so the trace name derived from the chain's serialized name was never propagated to the resulting trace.This means traces created via the LangChain callback handler would not have their name set correctly — the trace name would be missing or fall back to a default instead of reflecting the actual chain name.
Fix
Pass
trace_name=span_nameto thepropagate_attributes()call inon_chain_start. Thespan_nameis already computed just above this call viaself.get_langchain_run_name(serialized, **kwargs), so this is a one-line addition.Fixes #1602
Disclaimer: Experimental PR review
Greptile Summary
This PR fixes a one-line bug in
CallbackHandler.on_chain_startwheretrace_namewas omitted from thepropagate_attributes()call at the root level, causing LangChain traces to never have their name set from the chain's serialized name.Key changes:
langfuse/langchain/CallbackHandler.py,trace_name=span_nameis now passed topropagate_attributes()whenparent_run_id is None(root chain start). Thespan_nameis already computed just above viaself.get_langchain_run_name(serialized, **kwargs), making this a minimal, targeted fix.span_nameis already used to name the individual observation viastart_observation(name=span_name, ...).propagate_attributes()already accepted atrace_nameparameter — this was simply never being passed.Confidence Score: 5/5
This PR is safe to merge — it is a minimal, targeted one-line bug fix with no risk of regression.
The change is a single argument addition to an already-existing function call.
span_nameis always a non-null string (defaults to"<unknown>"at minimum),propagate_attributes()already acceptedtrace_name, and the fix is isolated to root-level chain starts. No new logic paths, no structural changes, and no custom rules are violated.No files require special attention.
Important Files Changed
span_nameastrace_nametopropagate_attributes()inon_chain_startso root-level LangChain chains correctly set the trace name.Sequence Diagram
sequenceDiagram participant LC as LangChain participant CH as CallbackHandler participant PA as propagate_attributes() participant LF as Langfuse Trace LC->>CH: on_chain_start(serialized, inputs, parent_run_id=None) CH->>CH: span_name = get_langchain_run_name(serialized) CH->>CH: parsed_trace_attributes = _parse_langfuse_trace_attributes(metadata, tags) CH->>PA: propagate_attributes(user_id, session_id, tags, metadata, trace_name=span_name) Note over PA: Before fix: trace_name was not passed → trace name missing Note over PA: After fix: trace_name=span_name → trace name correctly set PA->>LF: Sets trace-level attributes (incl. trace name) on active span CH->>LF: start_observation(name=span_name, ...)Reviews (1): Last reviewed commit: "fix(langchain): pass trace_name to propa..." | Re-trigger Greptile
(2/5) Greptile learns from your feedback when you react with thumbs up/down!