Skip to content

fix(tracing): inject langfuse_session_id and langfuse_user_id into per-run config metadata#3092

Open
64johnlee wants to merge 1 commit into
bytedance:mainfrom
64johnlee:fix/langfuse-session-user-metadata
Open

fix(tracing): inject langfuse_session_id and langfuse_user_id into per-run config metadata#3092
64johnlee wants to merge 1 commit into
bytedance:mainfrom
64johnlee:fix/langfuse-session-user-metadata

Conversation

@64johnlee
Copy link
Copy Markdown
Contributor

Problem

Closes #2930

Langfuse traces produced by DeerFlow runs were not grouped by thread or attributed to the requesting user. The LangfuseCallbackHandler reads langfuse_session_id and langfuse_user_id from the LangChain run metadata dict, but neither key was being set anywhere in the run pipeline.

Solution

In run_agent() (runtime/runs/worker.py), immediately before the RunnableConfig is constructed, inject both keys into config["metadata"]:

  • langfuse_session_idthread_id (groups all runs for a thread into one Langfuse session)
  • langfuse_user_idruntime_ctx.get("user_id", "") (the user resolved by the gateway and propagated through _build_runtime_context)

No changes to model construction, callback factories, or the Langfuse handler itself are required — the handler already picks these values up from the metadata dict.

Test

Added test_run_agent_injects_langfuse_session_and_user_into_config_metadata to tests/test_run_worker_rollback.py. A DummyAgent captures config["metadata"] at stream time and the test asserts both keys are present with the correct values.

Also fixed a stray assertion that had leaked into the wrong test function in the same file.

18 passed in 0.24s

…r-run config metadata

LangfuseCallbackHandler reads langfuse_session_id and langfuse_user_id from
the LangChain run metadata dict. By setting these in config["metadata"] before
RunnableConfig is built in run_agent(), every per-run trace is automatically
grouped by thread and attributed to the correct user in the Langfuse dashboard
without any changes to model or callback construction.

Closes bytedance#2930

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Langfuse 支持 session_id 和 user_id

1 participant