Repository navigation
fix(tracing): don't record a child's exception on its parent span - #2175
zuhalsicim wants to merge 2 commits into
Conversation
span.start_as_current_observation() activates the parent with otel_trace_api.use_span(), whose defaults record any exception that passes through and set the parent's status to ERROR. The child already records the exception itself, so: - an exception the caller catches inside the parent still marks the parent as ERROR (Langfuse shows it as level ERROR), and - an exception that leaves the parent's own block is recorded on the parent twice. langfuse.start_as_current_observation() does neither. Activate the parent with record_exception=False and set_status_on_exception=False.
AI review gate — AI review passedClean review from: Cursor Bugbot. Configuration: the repository's History (latest 5 heads, newest first)
|
1AbdullahButt
left a comment
There was a problem hiding this comment.
LGTM. This is the right fix, with one behaviour change worth calling out. I ran pytest tests/unit/test_otel.py with uv sync --frozen on Python 3.12: 100 passed and 2 skipped on the head, while on main 3 fail, including both new tests.
I also ran a scratch test against the in-memory exporter. When a generation child raises and the error is caught inside with client.start_as_current_observation("parent") as p:, the parent ends as ERROR with an exception event on main. On the head it ends as UNSET. Nested @observe functions already behave the head's way (the outer span stays UNSET), so this also makes the two APIs consistent.
The behaviour change: with a manual parent (start_observation()), an exception that escapes parent.start_as_current_observation(...) used to mark the parent ERROR. Now nothing marks it, even when the error is never handled. That matches OTel semantics, but it deserves a release-note line.
Suggestions:
tests/unit/test_otel.py:147: the manual case only covers an exception that is handled. A case where it escapes thestart_as_current_observationcall and the parent is ended infinallywould lock in the new contract.langfuse/_client/client.py:1441: mentioning that@observealready behaves this way would stop someone "fixing" it back.
A manual parent (start_observation()) is not a context manager, so an exception that escapes parent.start_as_current_observation() is now recorded only on the child, and the parent's owner sets its status. Lock that in with a test that ends the parent in finally, and note in the comment that nested @observe functions already behave this way.
|
Thanks for checking it this carefully. Both are in now: a test where the exception escapes and the manual parent is ended in |
What does this PR do?
When a child is created from the parent object with
parent.start_as_current_observation(), an exception raised in the child is also recorded on the parent, and the parent's OTel status is set to ERROR. This happens even when the code catches the exception inside the parent, for example on a retry or a fallback:Langfuse turns OTel status ERROR into level ERROR when no level is set, so the parent shows up as an error. With
langfuse.start_as_current_observation()for the child, the parent stays clean. Both variants against an in-memory exporter, on main:_create_span_with_parent_contextactivates the parent withotel_trace_api.use_span(parent_span), anduse_spanrecords exceptions and sets ERROR by default. The child's own context manager already does both, so the parent is now activated withrecord_exception=False, set_status_on_exception=False.This also removes a duplicate. When the exception leaves the parent's block, the parent recorded it twice. A failed
run_experimentitem shows this, because the task span is created withspan.start_as_current_observation(): itsexperiment-item-runspan had 2 exception events, now 1.One behaviour change: a parent created with
start_observation()and ended by hand is no longer marked ERROR by an exception that escapes its child. The child still records it, and the caller can set the parent's level withupdate(level="ERROR"), as for any manually ended observation.There is no issue for this yet.
Release note
With a manual parent (
start_observation()), an exception that escapesparent.start_as_current_observation(...)no longer marks the parent as ERROR. The child still records it. This matches OpenTelemetry semantics and how nested@observefunctions already behave.Type of change
Verification
Windows 11, Python 3.12, opentelemetry-sdk 1.45.2, with the unit-test env vars from ci.yml. No e2e or live-provider runs; the in-memory exporter covers the change.
Checklist
code_review.md..env.templateif needed. (not needed)Note
Medium Risk
Changes OpenTelemetry span status and exception propagation for nested observations, which affects how traces appear in Langfuse but is localized to parent activation during child creation.
Overview
Fixes incorrect ERROR status on parent observations when children are started via
parent.start_as_current_observation(). In_create_span_with_parent_context, activating the parent span no longer uses OpenTelemetry's defaultuse_spanexception recording (record_exception=False,set_status_on_exception=False), so only the child records the failure—matchinglangfuse.start_as_current_observation()and nested@observebehavior.Caught child exceptions inside the parent no longer mark the parent as failed (e.g. retry/fallback flows). Uncaught exceptions from a parent context manager still set ERROR on both parent and child, but without duplicating exception events on the parent (previously two
exceptionevents in paths likerun_experiment).Intentional change: manually ended parents (
start_observation()+.end()) are no longer auto-marked ERROR when an exception escapes a child; callers can setupdate(level="ERROR")if needed. Unit tests intest_otel.pycover handled children, manual parents, and propagating failures.Reviewed by Cursor Bugbot for commit da68de2. Bugbot is set up for automated code reviews on this repo. Configure here.


Confidence Score: 5/5
The PR appears safe to merge.Summary
Stops child exceptions from being recorded on the parent merely because the parent was temporarily activated.
Reviews (4) · Last reviewed commit: "test(tracing): cover a child exception e..." · Reviewed by Greptile