Skip to content

fix(openai): end observations cancelled before a response - #2110

Open
feiiiiii5 wants to merge 2 commits into
langfuse:mainfrom
feiiiiii5:codex/openai-cancel-before-response
Open

feiiiiii5 wants to merge 2 commits into
langfuse:mainfrom
feiiiiii5:codex/openai-cancel-before-response

Conversation

@feiiiiii5

@feiiiiii5 feiiiiii5 commented Nov 6, 2026 •

Copy link
Copy Markdown

What does this PR do?

End an OpenAI observation when cancellation interrupts the awaited request before a response arrives, then re-raise the original cancellation. Previously, the SDK propagated cancellation but Langfuse never ended or exported that observation.

The same wrapper covers chat completions, Responses, and embeddings. Cancellation after a stream returns continues through the existing stream finalizer.

Type of change

  • Bug fix

Verification

Five regressions use real AsyncOpenAI clients with a waiting HTTP mock transport and the in-memory exporter. All five fail on main because no ended observation is exported, and pass here. They retain the cancellation object/message and record no invented output, usage, cost, or error status.

Python 3.13.18, locked OpenAI 2.29.0 and HTTPX 0.28.1:

uv run --frozen pytest tests/unit/test_openai.py tests/unit/test_openai_prompt_extraction.py -q --tb=short
# 66 passed
uv run --frozen pytest -n 2 --dist worksteal tests/unit -q --tb=short
# 1020 passed, 2 skipped
uv run --frozen ruff check .
uv run --frozen ruff format --check langfuse/openai.py tests/unit/test_openai.py
uv run --frozen mypy langfuse --no-error-summary

The full suite uses the existing tracked fixtures and public CI dummy credentials. Lint, formatting, mypy, independent base/head replay, and diff review pass. Live-provider/server suites were not run; this behavior is covered by exporter-local unit tests.

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 applicable.)
  • I did not hand-edit generated files; if generated files changed, I used the upstream regeneration path.
  • I did not commit secrets or credentials.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge.

Summary

Ends OpenAI observations when cancellation interrupts a request before a response, then re-raises the original cancellation.

  • Async OpenAI cancellations now end their Langfuse observations.

Reviews (2) · Last reviewed commit: "test(openai): move cancellation transpor..." · 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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T16:20:57.000526Z 716a58e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Comment thread tests/unit/test_openai.py Resolved
@feiiiiii5

Copy link
Copy Markdown
Author

Moved httpx to the module imports in 0685844, addressing this comment. The full unit suite passes again (1020 passed, 2 skipped), along with Ruff and formatting checks.

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.

1 participant