Skip to content

fix(openai): finalize sync stream on early break

2dcc02b
Select commit
Loading
Failed to load commit list.
Merged

fix(openai): preserve native v1 stream contract #1627

fix(openai): finalize sync stream on early break
2dcc02b
Select commit
Loading
Failed to load commit list.
Claude / Claude Code Review completed Apr 16, 2026 in 17m 29s

Code review found 1 important issue

Found 5 candidates, confirmed 2. See review comments for details.

Details

Severity Count
🔴 Important 1
🟡 Nit 0
🟣 Pre-existing 0
Severity File:Line Issue
🔴 Important langfuse/openai.py:836-854 Async stream early-break leaves generation un-finalized (missing aiter hook)

Annotations

Check failure on line 854 in langfuse/openai.py

See this annotation in the file changed.

@claude claude / Claude Code Review

Async stream early-break leaves generation un-finalized (missing __aiter__ hook)

The sync early-break fix (commit 2dcc02b) has no async equivalent: `_install_openai_stream_iteration_hooks` only patches `openai.Stream.__iter__` and never installs a hook on `openai.AsyncStream.__aiter__`, and `_instrument_openai_async_stream` never sets `response._langfuse_finalize_once`. When a user does `async for chunk in stream: break`, Python closes the outer `__aiter__` generator but does not propagate `aclose()` to `self._iterator` (an attribute reference, not a frame-local), so `traced