feat: support trace propagation via langfuse_trace_id in metadata. #1264
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.
🔗 Enable External Trace Linking via
metadata['langfuse_trace_id']
inCallbackHandler
Summary
This PR allows users to manually propagate a
trace_id
across multiple LangGraph or LangChain runs by including alangfuse_trace_id
in themetadata
of theRunnableConfig
.Problem
Currently, the
CallbackHandler
inlangfuse.langchain
usesclient.start_span()
without providing a way to pass a customtrace_id
. This makes it impossible to link multiple distinct LangGraph executions under a single trace, which is especially important for stateful applications where each step (even across executions) is part of a cohesive experience.Solution
This PR modifies the
on_chain_start
method to pass atrace_context
with a custom trace ID if it's provided undermetadata['langfuse_trace_id']
.✅ Usage Example
You can now pass
langfuse_trace_id
inRunnableConfig.metadata
:This enables all runs within the graph to be linked to the same trace in Langfuse, even if they occur in separate invocations (e.g., due to checkpoints, retries, or async workflows with fastapi).
💡 Why It Matters
📈 Improves observability for multi-step or multi-invocation workflows.
🔗 Links traces across separate LangGraph or Runnable executions without monkey-patching or custom handlers.
🧠 Supports advanced use cases like LangGraph checkpointing, retries, and workflows that resume over time.
🛠️ Notes
✅ This change is fully backward-compatible.
🔒 If langfuse_trace_id is not present in metadata, the behavior remains unchanged.
Important
Adds support for custom trace ID propagation via
metadata['langfuse_trace_id']
inCallbackHandler
, allowing trace linking across multiple runs.metadata['langfuse_trace_id']
inCallbackHandler
.on_chain_start
to includetrace_context
with custom trace ID if provided.langfuse_trace_id
inRunnableConfig.metadata
to link multiple runs under a single trace.langfuse_trace_id
is absent.This description was created by
for dccbfaa. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR enhances the Langfuse Python SDK's tracing capabilities by adding support for trace propagation across multiple LangGraph or LangChain runs. The core change is in the
CallbackHandler
class, which now accepts alangfuse_trace_id
in the metadata to link related operations under a single trace.The modification is focused on the
on_chain_start
method, where the code now checks for alangfuse_trace_id
in the metadata and uses it to create a trace context. This allows developers to maintain continuity in tracing across separate executions, which is particularly valuable for stateful applications, async workflows, and scenarios involving checkpoints or retries.The implementation integrates seamlessly with LangChain's existing patterns by utilizing the
RunnableConfig.metadata
field, making it intuitive for developers familiar with the ecosystem.Confidence score: 5/5