Repository navigation
fix(otel): don't log an error when a span ends in another context - #1071
Merged
Abhijeet Prasad (AbhiPrasad) merged 1 commit intoNov 5, 2026
Merged
Conversation
> 🤖 **This text was AI-generated.**
With `BRAINTRUST_OTEL_COMPAT=true`, async frameworks such as LangGraph can
start a span in one asyncio task and end it in another. Each task runs in
its own `contextvars.Context`, so the token saved when the span became
current belongs to a different Context than the one ending it:
```
task A (Context 1) task B (Context 2)
on_chain_start on_chain_end
span.set_current() span.unset_current()
token = attach(ctx) context.detach(token)
-> ValueError (other Context)
-> OTel logs it at ERROR
```
OpenTelemetry's `context.detach()` catches that `ValueError` itself and
logs "Failed to detach context" with a traceback. Nothing reaches
Braintrust, so the `except ValueError` guard in the LangChain callback
handler never ran. Every such run printed an ERROR, even though span
parenting was correct (#1062).
The OTel `ContextManager` now resets OTel's default contextvars token
directly instead of calling `context.detach()`:
```python
try:
token.var.reset(token)
except ValueError:
# token came from another Context; leave both contexts as-is
log.debug(...)
except Exception:
log.exception("Failed to detach context")
```
- A token from another Context is logged at DEBUG. Neither context is
changed, so an unrelated span that is active where the run ends stays
current.
- Other failures, such as reusing a token, are still logged at ERROR and
not raised, same as before.
- Tokens from other OTel runtime implementations still go through
`context.detach()`.
The dead `try/except ValueError` around `span.unset_current()` in the
LangChain handler is removed, since `unset_current()` never raises.
Limitation: the Context where the span started still treats the ended span
as current. `BraintrustContextManager` skips ended spans when it looks
up the current one; the OTel manager does not yet.
Fixes #1062
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. |
Abhijeet Prasad (AbhiPrasad)
deleted the
abhi/fix-otel-context-detach-197c036d
branch
November 5, 2026 15:00
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.
With
BRAINTRUST_OTEL_COMPAT=true, async frameworks such as LangGraph can start a span in one asyncio task and end it in another. Each task runs in its owncontextvars.Context, so the token saved when the span became current belongs to a different Context than the one ending it:OpenTelemetry's
context.detach()catches thatValueErroritself and logs "Failed to detach context" with a traceback. Nothing reaches Braintrust, so theexcept ValueErrorguard in the LangChain callback handler never ran. Every such run printed an ERROR, even though span parenting was correct (#1062).The OTel
ContextManagernow resets OTel's default contextvars token directly instead of callingcontext.detach():context.detach().The dead
try/except ValueErroraroundspan.unset_current()in the LangChain handler is removed, sinceunset_current()never raises.Limitation: the Context where the span started still treats the ended span as current.
BraintrustContextManagerskips ended spans when it looks up the current one; the OTel manager does not yet.Fixes #1062