Skip to content

fix(openai): remove return from finally blocks to fix SyntaxWarning on Python 3.14#3972

Open
RRocaP wants to merge 1 commit intotraceloop:mainfrom
RRocaP:fix/syntax-warning-return-in-finally
Open

fix(openai): remove return from finally blocks to fix SyntaxWarning on Python 3.14#3972
RRocaP wants to merge 1 commit intotraceloop:mainfrom
RRocaP:fix/syntax-warning-return-in-finally

Conversation

@RRocaP
Copy link
Copy Markdown

@RRocaP RRocaP commented Apr 11, 2026

Summary

Fixes #3969

Python 3.14 emits SyntaxWarning: 'return' in a 'finally' block for
ResponseStream.close() and ResponseStream.aclose() in
responses_wrappers.py.

Problem

return inside a finally block silently discards any pending exception,
which Python 3.14 now warns about:

SyntaxWarning: 'return' in a 'finally' block
  return self.__wrapped__.close()

Fix

Move the return statements out of the finally block. Since
_ensure_cleanup() is idempotent and lock-guarded, this is
behaviorally equivalent — cleanup runs first, then the wrapped
stream's close/aclose is called.

Files changed

  • packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/v1/responses_wrappers.py

Test plan

  • No SyntaxWarning on Python 3.14
  • Existing tests pass

Summary by CodeRabbit

  • Bug Fixes
    • Improved response stream closure handling in the OpenAI instrumentation to ensure proper cleanup of resources.

…n Python 3.14

Move return statements out of finally blocks in ResponseStream.close()
and ResponseStream.aclose(). Python 3.14 emits SyntaxWarning for
return inside finally, because the return silently discards any
pending exception.

The cleanup (_ensure_cleanup) is idempotent and already guarded by
a lock, so running it in try with an empty finally and then returning
the wrapped method's result achieves the same behavior without the
warning.

Fixes traceloop#3969
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f6888222-fa4d-407e-a0af-05e6fcead47f

📥 Commits

Reviewing files that changed from the base of the PR and between 786d49f and 3dec32e.

📒 Files selected for processing (1)
  • packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/v1/responses_wrappers.py

📝 Walkthrough

Walkthrough

The changes refactor the ResponseStream.close() and ResponseStream.aclose() methods to eliminate return statements within finally blocks, resolving a Python 3.14 SyntaxWarning. The wrapped method invocations are relocated outside the try/finally construct using top-level conditionals.

Changes

Cohort / File(s) Summary
ResponseStream Method Refactoring
packages/opentelemetry-instrumentation-openai/opentelemetry/instrumentation/openai/v1/responses_wrappers.py
Removed return statements from finally blocks in close() and aclose() methods. Replaced early-return conditionals with top-level conditionals that execute after try/finally completes, and changed finally blocks to pass.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A finally block with returns? Oh dear!
Python 3.14 whispers its warning clear.
We hop and restructure the flow with care,
Moving returns outside—the syntax now fair!
No more warnings, just hopping ahead,
With cleaner code and fewer threads of dread. 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% 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 directly addresses the main change: removing return statements from finally blocks to fix SyntaxWarning on Python 3.14, which matches the changeset.
Linked Issues check ✅ Passed The PR successfully implements the objective from issue #3969: eliminating SyntaxWarning by removing return statements from finally blocks in ResponseStream.close() and aclose() methods.
Out of Scope Changes check ✅ Passed All changes are scoped to the specific file and methods addressing the SyntaxWarning issue; no unrelated modifications are present.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

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

2 participants