Repository navigation
fix(langchain): propagate root trace attributes to child runs under ainvoke - #2068
Conversation
…invoke Co-authored-by: hassieb.pakzad <hassieb.pakzad@clickhouse.com>
|
@claude review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
Claude Code Review
No review was started: this request came from a bot account. Manual reviews can only be requested by someone with write access to this repository. Ask a maintainer to comment @claude review, or have your automation post the comment from a user account with write access.
Tip: disable this comment in your organization's Code Review settings.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9c582abcd8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
What does this PR do?
When trace attributes are passed through LangChain invocation metadata (
config={"metadata": {"langfuse_session_id": ...}}) and the runnable is called withainvoke, only the root observation gotsession_id(anduser_id,tags,trace_name, propagatedmetadata). Every child generation, tool and chain had none, so those observations dropped out of session-scoped views such as the Sessions page tool filters. Syncinvokeand an explicitpropagate_attributes(...)wrapper both worked.Root cause:
CallbackHandleris a sync handler withrun_inline=False. In async runs LangChain calls it viarun_in_executor(None, copy_context().run, ...), so each callback runs in a fresh copy of the caller's context. The rooton_chain_startenterspropagate_attributes, but that context change stays in the root callback's copy, so the child callbacks never see it and their spans start without the propagated keys.Fix:
propagate_attributeson_RootRunState.propagation_context.start_observationcalls (chain, tool, retriever, LLM) run inside_use_root_propagated_attributes. It fills in only thelangfuse.propagated.*keys that are missing from the current context. Scalar values already set in the current context win, andmetadata/tagsare merged. If nothing differs, nothing is attached, so the sync path does not change.Fixes LFE-17917
Type of change
Verification
test_root_chain_metadata_propagates_session_id_to_children_on_ainvokefailed before the fix (KeyError: 'session.id'onChatPromptTemplate) and passes after it.test_child_run_keeps_attributes_propagated_in_its_own_contextchecks that a child's ownpropagate_attributesstill wins over the root value.I did not run e2e or live-provider tests. The behavior is covered by unit tests with the in-memory exporter.
Checklist
code_review.md..env.templateif needed. (Not needed: this brings async in line with the documented behavior.)Fix the remaining async session override case before merging.
Summary
Saves the root chain's propagated attributes and makes them available while child observations start.
Diagram
%%{init: {'theme': 'neutral'}}%% flowchart TD A["Caller context: session outer"] --> B["Root callback: invocation sets session inner"] B --> C["Save root context"] A --> D["Async child callback inherits session outer"] C --> E["Merge saved root attributes"] D --> E E --> F["Existing value wins: child keeps session outer"]Reviews (1) · Last reviewed commit: "fix(langchain): propagate root trace att..." · Reviewed by Greptile