From 447e9cd23104d474d256e5ee5edfa36680a72f81 Mon Sep 17 00:00:00 2001 From: klaasmeinke <48212135+klaasmeinke@users.noreply.github.com> Date: Wed, 7 Jan 2026 18:57:28 +0100 Subject: [PATCH 1/2] Refactor trace ID handling in Langchain execution Updated the method to pass a custom trace ID in Langchain execution by using the 'trace_context' argument. Removed the previous method of using 'trace_context' in the span. --- .../trace-ids-and-distributed-tracing.mdx | 28 +++++-------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/pages/docs/observability/features/trace-ids-and-distributed-tracing.mdx b/pages/docs/observability/features/trace-ids-and-distributed-tracing.mdx index 18843595f2..271e711cb9 100644 --- a/pages/docs/observability/features/trace-ids-and-distributed-tracing.mdx +++ b/pages/docs/observability/features/trace-ids-and-distributed-tracing.mdx @@ -293,7 +293,7 @@ const completion = await openai.chat.completions.create({ -To pass a custom trace ID to a Langchain execution, you can wrap the execution in a span that sets a predefined trace ID. You can also retrieve the last trace ID a callback handler has created via `langfuse_handler.last_trace_id`. +To pass a custom trace ID to a Langchain execution, you can add a trace ID to the callback handler using the `trace_context` argument. You can also retrieve the last trace ID a callback handler has created via `langfuse_handler.last_trace_id`. ```python from langfuse import get_client, Langfuse @@ -305,27 +305,13 @@ langfuse = get_client() external_request_id = "req_12345" predefined_trace_id = Langfuse.create_trace_id(seed=external_request_id) -langfuse_handler = CallbackHandler() +langfuse_handler = CallbackHandler(trace_context={"trace_id": predefined_trace_id) -# Use the predefined trace ID with trace_context -with langfuse.start_as_current_observation( - as_type="span", - name="langchain-request", - trace_context={"trace_id": predefined_trace_id} -) as span: - - with propagate_attributes( - span.update_trace( - input={"person": "Ada Lovelace"} - ) - - # LangChain execution will be part of this trace - response = chain.invoke( - {"person": "Ada Lovelace"}, - config={"callbacks": [langfuse_handler]} - ) - - span.update_trace(output={"response": response}) +# LangChain execution will be part of this trace +response = chain.invoke( + {"person": "Ada Lovelace"}, + config={"callbacks": [langfuse_handler]} +) print(f"Trace ID: {predefined_trace_id}") # Use this for scoring later print(f"Trace ID: {langfuse_handler.last_trace_id}") # Care needed in concurrent environments where handler is reused From 9dd17bda97aae304786bd776a326047ec54f7094 Mon Sep 17 00:00:00 2001 From: klaasmeinke <48212135+klaasmeinke@users.noreply.github.com> Date: Fri, 9 Jan 2026 20:04:21 +0100 Subject: [PATCH 2/2] Update pages/docs/observability/features/trace-ids-and-distributed-tracing.mdx Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> --- .../features/trace-ids-and-distributed-tracing.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/docs/observability/features/trace-ids-and-distributed-tracing.mdx b/pages/docs/observability/features/trace-ids-and-distributed-tracing.mdx index 271e711cb9..e84411cfa2 100644 --- a/pages/docs/observability/features/trace-ids-and-distributed-tracing.mdx +++ b/pages/docs/observability/features/trace-ids-and-distributed-tracing.mdx @@ -305,7 +305,7 @@ langfuse = get_client() external_request_id = "req_12345" predefined_trace_id = Langfuse.create_trace_id(seed=external_request_id) -langfuse_handler = CallbackHandler(trace_context={"trace_id": predefined_trace_id) +langfuse_handler = CallbackHandler(trace_context={"trace_id": predefined_trace_id}) # LangChain execution will be part of this trace response = chain.invoke(