Skip to content

fix(tracing): don't record a child's exception on its parent span - #2175

Open
zuhalsicim wants to merge 2 commits into
langfuse:mainfrom
zuhalsicim:fix/handled-child-error-parent-status
Open

zuhalsicim wants to merge 2 commits into
langfuse:mainfrom
zuhalsicim:fix/handled-child-error-parent-status

Conversation

@zuhalsicim

@zuhalsicim zuhalsicim commented Nov 28, 2026 •

Copy link
Copy Markdown

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:

with langfuse.start_as_current_observation(name="answer") as parent:
    try:
        with parent.start_as_current_observation(name="llm-call"):
            raise ValueError("rate limited")
    except ValueError:
        pass  # retry or fallback, the request still succeeds

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:

child-via-span-method    status=ERROR  events=['exception']
parent-via-span-method   status=ERROR  events=['exception']
child-via-client         status=ERROR  events=['exception']
parent-via-client        status=UNSET  events=[]

_create_span_with_parent_context activates the parent with otel_trace_api.use_span(parent_span), and use_span records exceptions and sets ERROR by default. The child's own context manager already does both, so the parent is now activated with record_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_experiment item shows this, because the task span is created with span.start_as_current_observation(): its experiment-item-run span 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 with update(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 escapes parent.start_as_current_observation(...) no longer marks the parent as ERROR. The child still records it. This matches OpenTelemetry semantics and how nested @observe functions already behave.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Refactor
  • Documentation update
  • Tooling, CI, or repo maintenance

Verification

uv run --frozen pytest tests/unit/test_otel.py -k "handled_child_exception or leaving_parent_block or escaping_manual_parent"
# main: 4 failed; this branch: 4 passed
uv run --frozen pytest tests/unit/test_otel.py tests/unit/test_propagate_attributes.py tests/unit/test_observe.py tests/unit/test_experiment.py tests/unit/test_resource_manager.py tests/unit/test_span_processor.py
# 349 passed, 2 skipped
uv run --frozen pytest -n auto --dist worksteal tests/unit
# 1036 passed, 2 skipped; 9 failed and 1 error, the same tests on main (Windows only: symlink rights, a backslash path, time.tzset, the atexit subprocess tests)
uv run --frozen ruff check .
uv run --frozen ruff format --check .
uv run --frozen mypy langfuse --no-error-summary

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

  • I self-reviewed the diff using code_review.md.
  • I added or updated tests for behavior changes.
  • I updated docs, examples, or .env.template if needed. (not needed)
  • I did not hand-edit generated files; if generated files changed, I used the upstream regeneration path.
  • I did not commit secrets or credentials.

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 default use_span exception recording (record_exception=False, set_status_on_exception=False), so only the child records the failure—matching langfuse.start_as_current_observation() and nested @observe behavior.

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 exception events in paths like run_experiment).

Intentional change: manually ended parents (start_observation() + .end()) are no longer auto-marked ERROR when an exception escapes a child; callers can set update(level="ERROR") if needed. Unit tests in test_otel.py cover 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.RetriggerView in GreptileConfidence 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.

  • Caught child failures leave the parent span unchanged.

Reviews (4) · Last reviewed commit: "test(tracing): cover a child exception e..." · Reviewed by Greptile

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.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@CLAassistant

CLAassistant commented Nov 28, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@langfuse-ai-gate-approver

langfuse-ai-gate-approver Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

AI review gate — AI review passed

Clean review from: Cursor Bugbot.

Configuration: the repository's .github/ai-gate.yml on the base branch (the defaults without one).

History (latest 5 heads, newest first)

  • da68de28ece4 gate pending — Waiting for AI review
  • 2796fc703676 gate pending — Waiting for AI review

@1AbdullahButt 1AbdullahButt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 the start_as_current_observation call and the parent is ended in finally would lock in the new contract.
  • langfuse/_client/client.py:1441: mentioning that @observe already 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.
@zuhalsicim

Copy link
Copy Markdown
Author

Thanks for checking it this carefully. Both are in now: a test where the exception escapes and the manual parent is ended in finally (it fails on main and passes here), and the comment says @observe already behaves this way. I also added the manual-parent change to the description as a release note.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants