-
Notifications
You must be signed in to change notification settings - Fork 8.5k
fix(tracing): nest LangChain spans under Langflow component spans in Phoenix #12008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xNekr
wants to merge
1
commit into
langflow-ai:main
Choose a base branch
from
0xNekr:fix/phoenix-span-nesting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+16
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
head -n 320 src/backend/base/langflow/services/tracing/arize_phoenix.py | tail -n +100Repository: langflow-ai/langflow
Length of output: 9260
🏁 Script executed:
sed -n '260,320p' src/backend/base/langflow/services/tracing/arize_phoenix.pyRepository: langflow-ai/langflow
Length of output: 2442
🏁 Script executed:
sed -n '100,120p' src/backend/base/langflow/services/tracing/arize_phoenix.pyRepository: langflow-ai/langflow
Length of output: 1254
🌐 Web query:
OpenTelemetry Python contextvars attach detach token execution context async💡 Result:
In OpenTelemetry Python, context propagation (across sync code and
async/await) is implemented via a runtime context (typicallycontextvars), and the core API is:opentelemetry.context.attach(ctx) -> Token: makesctxthe current context for the caller’s current execution unit (thread / async task) and returns a token representing the previous context. [1]opentelemetry.context.detach(token): restores the context that was current before the matchingattach(). Always pair it withattach()(usetry/finally). [1][2]Typical safe pattern (including async):
If you don’t
detach()after modifying/attaching context, you can get errors due to unmatched attach/detach (the SDK can detect leaked contexts). [3]Sources: OpenTelemetry Python context API docs [1], OpenTelemetry Python context attach/detach guidance [2], example + warning about failing to detach [3].
[1] (opentelemetry-python.readthedocs.io)
[2] (deepwiki.com)
[3] (docs.honeycomb.io)
Citations:
🌐 Web query:
OpenTelemetry Python context.attach context.detach same thread async task💡 Result:
In OpenTelemetry Python,
context.attach(ctx)/context.detach(token)are implemented on top of Pythoncontextvars, so the returned token is only valid in the same “current Context” (i.e., the same logical execution unit) where it was created. If youdetach()in a differentcontextvarscontext, Python raisesValueError: Token was created in a different Context(or similar). [1][2]What “same thread” means with
asyncioEven if everything runs on the same OS thread, different
asyncio.Tasks can have differentcontextvarscontexts, and the token must be detached in the same task / same context that did the attach. OpenTelemetry describes the scope as “same thread or async task.” [3]Common ways this breaks
attach()in one coroutine/task, butdetach()runs:create_task, callbacks, background tasks), orGeneratorExitwhere finalization happens under a different context. This often shows up exactly as the “created in a different Context” error in streaming/generator scenarios. [4]Safe pattern
Do
detach(token)in afinallyin the same coroutine call path that didattach():If you need the same context in a new task, propagate the context value into the task at creation time (rather than moving the token across tasks), and use task-local attach/detach there.
Sources
[1] OpenTelemetry Python Context API (
attach/detach) (opentelemetry-python.readthedocs.io)[2] PEP 567:
Token.reset()raisesValueErrorif used in a different Context (peps.python.org)[3] OpenTelemetry Python context behavior “same thread or async task” (deepwiki.com)
[4] Example async streaming/generator teardown causing “different Context” token detach error (github.com)
Citations:
Wrap token detach in try/finally to prevent context leaks on exceptions.
OpenTelemetry's
attach()/detach()pattern requires detach to happen even if attribute-setting operations fail. Currently, exceptions during the output/log/status attribute operations (lines 306–314) skip the detach, leaving the context attached and causing resource leaks. Additionally, ifadd_trace()andend_trace()execute in different async tasks or contexts, the detach will raiseValueErrorbecause the token is context-scoped. Wrap the detach in atry/finallyblock withinend_trace():Suggested fix
Also applies to: 305–311
🤖 Prompt for AI Agents