Skip to content

fix(langchain): pass trace_name to propagate_attributes() in on_chain_start#1608

Open
Spectual wants to merge 1 commit intolangfuse:mainfrom
Spectual:fix/pass-trace-name-in-on-chain-start
Open

fix(langchain): pass trace_name to propagate_attributes() in on_chain_start#1608
Spectual wants to merge 1 commit intolangfuse:mainfrom
Spectual:fix/pass-trace-name-in-on-chain-start

Conversation

@Spectual
Copy link
Copy Markdown

@Spectual Spectual commented Apr 3, 2026

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_name was not being passed to propagate_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_name to the propagate_attributes() call in on_chain_start. The span_name is already computed just above this call via self.get_langchain_run_name(serialized, **kwargs), so this is a one-line addition.

self._propagation_context_manager = propagate_attributes(
    user_id=parsed_trace_attributes.get("user_id", None),
    session_id=parsed_trace_attributes.get("session_id", None),
    tags=parsed_trace_attributes.get("tags", None),
    metadata=parsed_trace_attributes.get("metadata", None),
    trace_name=span_name,  # <-- added
)

Fixes #1602

Disclaimer: Experimental PR review

Greptile Summary

This PR fixes a one-line bug in CallbackHandler.on_chain_start where trace_name was omitted from the propagate_attributes() call at the root level, causing LangChain traces to never have their name set from the chain's serialized name.

Key changes:

  • In langfuse/langchain/CallbackHandler.py, trace_name=span_name is now passed to propagate_attributes() when parent_run_id is None (root chain start). The span_name is already computed just above via self.get_langchain_run_name(serialized, **kwargs), making this a minimal, targeted fix.
  • The fix is consistent with how span_name is already used to name the individual observation via start_observation(name=span_name, ...).
  • propagate_attributes() already accepted a trace_name parameter — 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_name is always a non-null string (defaults to "<unknown>" at minimum), propagate_attributes() already accepted trace_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

Filename Overview
langfuse/langchain/CallbackHandler.py One-line bug fix: passes span_name as trace_name to propagate_attributes() in on_chain_start so 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, ...)
Loading

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!

…_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
Copy link
Copy Markdown

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CallbackHandler.on_chain_start does not pass trace_name to propagate_attributes, causing non-deterministic trace names on LangGraph resume

1 participant