Metadata (session_id / user_id / tags) not attached to parent trace with LangChain ReAct agent; only appears on child span #8493
Metadata (session_id / user_id / tags) not attached to parent trace with LangChain ReAct agent; only appears on child spanSummary Environment
Minimal reproagent = create_react_agent(
model=model,
tools=[get_weather],
prompt=PROMPT
)
thread_id = "example-thread-1"
langfuse_handler = LangfuseCallbackHandler()
res = agent.invoke(
{"messages": [{"role": "user", "content": "what is the weather in sf"}]},
config={
"run_name": "Weather Agent",
"callbacks": [langfuse_handler],
"configurable": {"thread_id": thread_id},
"metadata": {
"langfuse_session_id": thread_id,
"langfuse_user_id": "user_456",
"langfuse_tags": ["production", "humor-bot"],
"custom_field": "additional metadata"
}
}
)Expected behavior
Actual behavior
{
"resourceAttributes": {
"telemetry.sdk.language": "python",
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.version": "1.36.0",
"service.name": "unknown_service"
},
"scope": {
"name": "langfuse-sdk",
"version": "3.2.4",
"attributes": { "public_key": "REDACTED" }
}
}
{
"custom_field": "additional metadata",
"thread_id": "example-thread-1",
"resourceAttributes": {
"telemetry.sdk.language": "python",
"telemetry.sdk.name": "opentelemetry",
"telemetry.sdk.version": "1.36.0",
"service.name": "unknown_service"
},
"scope": {
"name": "langfuse-sdk",
"version": "3.2.4",
"attributes": { "public_key": "REDACTED" }
}
}What I’ve tried
|
Replies: 3 comments 10 replies
|
When using the Langfuse Python SDK (v3) with LangChain, metadata such as However, the documentation states these fields are handled as follows:
If you observe these fields only on a child span and not on the parent trace, this may be because the parent trace is created outside of the Langfuse callback’s lifecycle or due to the order of how OpenTelemetry spans are set up in the LangChain agent's orchestration. The SDK expects the metadata to be available when the top-level trace is created, but if the parent trace is created before metadata is passed, it will not be updated retroactively(1). Actionable steps:
If you need to guarantee these fields are on the parent trace, you may need to explicitly create the trace and set the metadata using the SDK before invoking the agent, ensuring all subsequent spans and generations are children of that trace. There is no documented workaround in the SDK to patch or update the parent trace’s metadata after it is created(1). 📚 Sources: Have another question? Just tag @inkeep. |
|
Hi @eschullert, could you please share the link to the trace where this is the case? |
|
I had a similar problem and setting the update_trace flag helped me. I think this is the simplest solution. |






Thank you @eschullert and @thomasgtaylor, I agree it would be better if the metadata attached to the config of the langgraph invocation would be attached to the top-level span. Looping in @hassiebp to check if this is possible.
The second option to add attributes (including metadata) to a trace is by using an enclosing span. This should populate the metadata to the top-level trace (documented here):