Repository navigation
fix(observe): end spans when output transformation fails - #2134
Open
Elon-Jiang wants to merge 1 commit into
Open
Elon-Jiang wants to merge 1 commit into
Elon-Jiang wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
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
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/unitobserve, generators and OpenTelemetry spans run using the project's existing in-memory-exporter fixture. No live provider or Langfuse server was used.077448c7a4ae8708f29e1434177bf9f3c667119bbaseline: 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 totests/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
code_review.mdwith AI assistance; no human review is claimed.AI assistance was used for investigation, implementation and local verification.


Confidence Score: 5/5
The PR appears safe to merge.Summary
Ends streamed observations when output formatting fails, then re-raises the original exception.
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