Skip to content

fix(openai): remove redundant try/finally in ResponseStream close and aclose - #4080

Merged
dvirski merged 1 commit into
mainfrom
fix(openai)-remove-redundant-try/finally-in-ResponseStream-close-and-aclose-
May 14, 2026
Merged

fix(openai): remove redundant try/finally in ResponseStream close and aclose#4080
dvirski merged 1 commit into
mainfrom
fix(openai)-remove-redundant-try/finally-in-ResponseStream-close-and-aclose-

Conversation

@dvirski

@dvirski dvirski commented May 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #3969 — Python 3.14 SyntaxWarning caused by return statement inside finally block in ResponseStream close/aclose

Summary by CodeRabbit

  • Bug Fixes
    • Improved cleanup when closing streamed responses in the OpenAI integration, reducing the chance of lingering resources and improving stability for consumers of streaming functionality.
    • Both synchronous and asynchronous stream closures are now handled more reliably, decreasing resource leaks and connection issues for streaming use cases.

Review Change Stack

@CLAassistant

CLAassistant commented May 6, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9343c7d3-5267-4cdd-a159-806c0a8b3a7a

📥 Commits

Reviewing files that changed from the base of the PR and between 40faa87 and 4d6f7e0.

📒 Files selected for processing (1)
  • packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/v1/responses_wrappers.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/v1/responses_wrappers.py

📝 Walkthrough

Walkthrough

ResponseStream.close() and ResponseStream.aclose() now call _ensure_cleanup() directly, then conditionally delegate to the wrapped stream's close/aclose when present, removing the previous try/finally around the cleanup call.

Changes

ResponseStream cleanup delegation

Layer / File(s) Summary
ResponseStream close/aclose update
packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/v1/responses_wrappers.py
ResponseStream.close() and ResponseStream.aclose() call _ensure_cleanup() and then delegate to the wrapped object's close()/aclose() if available; the prior try/finally was removed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • galzilber
  • nina-kollman
  • max-deygin-traceloop
  • netanel-tl
  • doronkopit5

Poem

🐰 I nudged the stream to say goodnight,
Cleanup first, then dim the light,
No tangled finally to make me frown,
I hop away as logs go down,
Close and aclose — the burrow's right.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: removing redundant try/finally blocks in ResponseStream close and aclose methods.
Linked Issues check ✅ Passed The code changes directly address issue #3969 by removing return statements from finally blocks to eliminate the Python 3.14 SyntaxWarning.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the SyntaxWarning issue in ResponseStream close and aclose methods; no unrelated modifications present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix(openai)-remove-redundant-try/finally-in-ResponseStream-close-and-aclose-

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dvirski
dvirski requested a review from netanel-tl May 6, 2026 16:32
@doronkopit5

Copy link
Copy Markdown
Member

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

@dvirski
dvirski force-pushed the fix(openai)-remove-redundant-try/finally-in-ResponseStream-close-and-aclose- branch from 2c6aa65 to 40faa87 Compare May 12, 2026 11:13
@dvirski
dvirski force-pushed the fix(openai)-remove-redundant-try/finally-in-ResponseStream-close-and-aclose- branch from 40faa87 to 4d6f7e0 Compare May 14, 2026 10:42
@dvirski
dvirski merged commit 3d5ba04 into main May 14, 2026
12 checks passed
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.

🐛 Bug Report: SyntaxWarning: 'return' in a 'finally' block

3 participants