From cf63a7918997e6d4d65568cc469ee8d988c484fd Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 16 Dec 2025 18:03:25 +0000 Subject: [PATCH] feat: Add trace_context to LangchainCallbackHandler Co-authored-by: hassieb --- langfuse/langchain/CallbackHandler.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/langfuse/langchain/CallbackHandler.py b/langfuse/langchain/CallbackHandler.py index 7e23b155a..7f6479867 100644 --- a/langfuse/langchain/CallbackHandler.py +++ b/langfuse/langchain/CallbackHandler.py @@ -104,6 +104,19 @@ def __init__( Args: public_key: Optional Langfuse public key. If not provided, will use the default client configuration. update_trace: Whether to update the Langfuse trace with the chains input / output / metadata / name. Defaults to False. + trace_context: Optional context for connecting to an existing trace (distributed tracing) or + setting a custom trace id for the root LangChain run. Pass a `TraceContext` dict, e.g. + `{"trace_id": ""}` (and optionally `{"parent_span_id": ""}`) to link + the trace to an upstream system. + + Example: + Use a custom trace id without context managers: + + ```python + from langfuse.langchain import CallbackHandler + + handler = CallbackHandler(trace_context={"trace_id": "my-trace-id"}) + ``` """ self.client = get_client(public_key=public_key) self.run_inline = True