Skip to content

fix(observe): end spans when output transformation fails - #2134

Open
Elon-Jiang wants to merge 1 commit into
langfuse:mainfrom
Elon-Jiang:fix/observe-transform-span-cleanup
Open

Elon-Jiang wants to merge 1 commit into
langfuse:mainfrom
Elon-Jiang:fix/observe-transform-span-cleanup

Conversation

@Elon-Jiang

@Elon-Jiang Elon-Jiang commented Nov 17, 2026 •

Copy link
Copy Markdown

What does this PR do?

When @observe(transform_to_string=...) wraps a generator and its output formatter raises, _finalize() exits before ending the observation. The original exception reaches the caller, but no completed span is exported and later cleanup can invoke the formatter again.

Catch finalization failures in both generator wrappers, use the existing error finalizer to end the span, and re-raise the original exception. Public-interface tests cover synchronous/asynchronous streams, exhaustion/early close, successful/erroring formatters, original exception identity, and idempotent second close. The public docstring describes this behavior.

No signatures, generated clients, or dependencies change. No existing issue is required for this small bug fix. Related generator PRs (#1893, #2105, #1448, #1659, #1607) address different paths; their patches do not settle a failing output formatter.

Type of change

  • Bug fix
  • Documentation update

Verification

uv run --frozen pytest tests/unit/test_observe_transform.py tests/unit/test_observe.py -q
uv run --frozen ruff check .
uv run --frozen mypy langfuse --no-error-summary
uv run --frozen pre-commit run --files langfuse/_client/observe.py tests/unit/test_observe_transform.py
uv run --frozen pytest -n 4 --dist worksteal tests/unit
  • Baseline regression: 4 failed, 4 passed. After the fix, all 8 new cases and 23 related tests pass. Real public observe, generators and OpenTelemetry spans run using the project's existing in-memory-exporter fixture. No live provider or Langfuse server was used.
  • Ruff, mypy and all applicable actual pre-commit hooks pass.
  • Full unit suite comparison on the exact unchanged 077448c7a4ae8708f29e1434177bf9f3c667119b baseline: 997 passed, 2 skipped, 18 setup errors. With this change: 1,005 passed, 2 skipped, the same 18 setup errors. Every setup-error node is identical and belongs to tests/unit/test_prompt.py (AttributeError: Langfuse client is not initialized) in this credential-free environment. The full suite is not claimed to pass. E2E and live-provider suites were not run.

Checklist

  • Self-reviewed the diff using code_review.md with AI assistance; no human review is claimed.
  • Added tests for the changed behavior.
  • Updated the public docstring; no environment or example changes needed.
  • No generated files hand-edited.
  • No secrets or credentials committed.

AI assistance was used for investigation, implementation and local verification.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge.

Summary

Ends streamed observations when output formatting fails, then re-raises the original exception.

  • Observed streams end their span when output formatting fails.

No actionable issues found. Tests were inspected but not run during this review.

Reviews (1) · Last reviewed commit: "fix(observe): end spans when output tran..." · Reviewed by Greptile

@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 17, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.

2 participants