feat(langchain): allow injecting trace_context into LangchainCallbackHandler (#1419) #1458
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.
Important
Add
trace_contextparameter toLangchainCallbackHandlerto enhance tracing capabilities inCallbackHandler.py.trace_contextparameter toLangchainCallbackHandlerconstructor inCallbackHandler.py.trace_contextinon_chain_start()whenobsis an instance ofLangfuse.TraceContextfromlangfuse.types.This description was created by
for da8e1be. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Overview
Greptile Summary
Added
trace_contextparameter toLangchainCallbackHandler, allowing users to inject external trace context (trace_id and optional parent_span_id) into the handler. This enables integrating Langchain traces with external tracing systems.trace_context: Optional[TraceContext]parameter to__init__on_chain_start, when the parent is theLangfuseclient,trace_contextis passed tostart_observationtrace_contextis only applied inon_chain_start. Standalone LLM calls, tool calls, and retriever calls that occur without a parent chain will not have thetrace_contextapplied, ason_tool_start,on_retriever_start, and__on_llm_actiondo not passtrace_contexteven when the parent observation is the Langfuse client.Confidence Score: 3/5
trace_contextis not applied consistently across all entry points (tool, retriever, LLM). This limits the feature's usefulness when Langchain components are invoked directly without a chain wrapper.langfuse/langchain/CallbackHandler.py-on_tool_start,on_retriever_start, and__on_llm_actionshould also passtrace_contextwhen the parent observation is the Langfuse clientImportant Files Changed
File Analysis
trace_contextparameter toLangchainCallbackHandlerallowing users to inject trace context, but only applies toon_chain_start- standalone LLM/tool/retriever calls don't passtrace_context.Sequence Diagram
sequenceDiagram participant User participant LangchainCallbackHandler participant Langfuse participant LangfuseSpan User->>LangchainCallbackHandler: __init__(trace_context) Note right of LangchainCallbackHandler: stores self.trace_context alt Chain Start (root level) User->>LangchainCallbackHandler: on_chain_start(parent_run_id=None) LangchainCallbackHandler->>LangchainCallbackHandler: _get_parent_observation() Note right of LangchainCallbackHandler: returns Langfuse client LangchainCallbackHandler->>Langfuse: start_observation(trace_context=✓) Langfuse-->>LangchainCallbackHandler: LangfuseSpan end alt Tool/LLM/Retriever Start (root level) User->>LangchainCallbackHandler: on_tool_start(parent_run_id=None) LangchainCallbackHandler->>LangchainCallbackHandler: _get_parent_observation() Note right of LangchainCallbackHandler: returns Langfuse client LangchainCallbackHandler->>Langfuse: start_observation(trace_context=✗) Note right of Langfuse: trace_context NOT passed! Langfuse-->>LangchainCallbackHandler: LangfuseSpan end