fix(tracing): inject langfuse_session_id and langfuse_user_id into per-run config metadata#3092
Open
64johnlee wants to merge 1 commit into
Open
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Closes #2930
Langfuse traces produced by DeerFlow runs were not grouped by thread or attributed to the requesting user. The
LangfuseCallbackHandlerreadslangfuse_session_idandlangfuse_user_idfrom 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 theRunnableConfigis constructed, inject both keys intoconfig["metadata"]:langfuse_session_id→thread_id(groups all runs for a thread into one Langfuse session)langfuse_user_id→runtime_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_metadatatotests/test_run_worker_rollback.py. ADummyAgentcapturesconfig["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.