Skip to content

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

Open
hassiebp wants to merge 5 commits intomainfrom
hassieb/lfe-8788-bug-langfuseresponsegeneratorasync-object-has-no-attribute
Open

fix(openai): preserve native v1 stream contract#1627
hassiebp wants to merge 5 commits intomainfrom
hassieb/lfe-8788-bug-langfuseresponsegeneratorasync-object-has-no-attribute

Conversation

@hassiebp
Copy link
Copy Markdown
Contributor

@hassiebp hassiebp commented Apr 15, 2026

Summary

Fix OpenAI v1 streaming instrumentation to preserve the original openai.Stream and openai.AsyncStream objects instead of replacing them with Langfuse wrapper types.

This keeps the native stream contract intact for _iterator, .response, close() / aclose(), async for, and manual __anext__() while still collecting streamed chunks for Langfuse generation updates.

Changes

  • instrument native OpenAI v1 stream objects in place by wrapping their internal iterator
  • keep the existing Langfuse wrapper classes as a fallback for non-OpenAI generator-style streams
  • add regression tests for sync and async native stream behavior, including async for and __anext__()

Verification

  • uv run --frozen ruff check langfuse/openai.py tests/unit/test_openai.py
  • uv run --frozen pytest tests/unit/test_openai.py

Refs LFE-8788.

Disclaimer: Experimental PR review

Greptile Summary

This PR fixes OpenAI v1 streaming instrumentation by patching _iterator and close/aclose on the native openai.Stream and openai.AsyncStream objects in place, rather than replacing them with Langfuse wrapper types. The approach is sound: _iterator replacement correctly intercepts all iteration paths (__iter__, __next__, __anext__, async for) as confirmed by the SDK's implementation, and the is_finalized flag reliably prevents double-finalization.

Confidence Score: 5/5

Safe to merge — core patching logic is correct, double-finalization is guarded, and tests cover all three iteration patterns.

All remaining findings are P2 style suggestions. The _finalize_stream_response_async wrapper is harmless. No P0/P1 logic bugs found; the _iterator replacement approach is confirmed sound by the OpenAI SDK's implementation.

No files require special attention.

Important Files Changed

Filename Overview
langfuse/openai.py Adds _instrument_openai_stream / _instrument_openai_async_stream to monkey-patch _iterator and close/aclose on native openai.Stream objects, preserving type identity while still collecting chunks for Langfuse; logic is sound but _finalize_stream_response_async is a no-op async wrapper.
tests/unit/test_openai.py Adds three regression tests covering sync native stream, async native stream, and manual __anext__ usage; DummyOpenAIStream / DummyOpenAIAsyncStream correctly bypass super().__init__ and set _iterator directly, which is appropriate for unit-level testing.

Sequence Diagram

sequenceDiagram
    participant User
    participant openai.Stream
    participant traced_iterator (generator)
    participant raw_iterator
    participant finalize_once
    participant LangfuseGeneration

    Note over User,LangfuseGeneration: _instrument_openai_stream patches _iterator and close in place

    User->>openai.Stream: for chunk in stream (or next())
    openai.Stream->>traced_iterator (generator): __next__() via self._iterator
    traced_iterator (generator)->>raw_iterator: next item
    raw_iterator-->>traced_iterator (generator): chunk
    traced_iterator (generator)->>traced_iterator (generator): items.append(chunk), set completion_start_time
    traced_iterator (generator)-->>openai.Stream: yield chunk
    openai.Stream-->>User: chunk

    Note over traced_iterator (generator): On exhaustion or close()

    traced_iterator (generator)->>finalize_once: finally block (is_finalized guard)
    finalize_once->>LangfuseGeneration: _finalize_stream_response → generation.end()

    alt User calls stream.close() explicitly
        User->>openai.Stream: close() [patched to traced_close]
        openai.Stream->>openai.Stream: await original_close() (closes HTTP)
        openai.Stream->>finalize_once: finally in traced_close
        finalize_once->>finalize_once: is_finalized=True → no-op if already finalized
    end
Loading

Reviews (1): Last reviewed commit: "fix(openai): preserve native v1 stream c..." | Re-trigger Greptile

@github-actions
Copy link
Copy Markdown

@claude review

Comment thread langfuse/openai.py
Comment thread langfuse/openai.py
Comment on lines +836 to +854
def _install_openai_stream_iteration_hooks() -> None:
global _openai_stream_iter_hook_installed

if not _is_openai_v1():
return

if not _openai_stream_iter_hook_installed:
original_iter = openai.Stream.__iter__

def traced_iter(self: Any) -> Any:
try:
yield from original_iter(self)
finally:
finalize_once = getattr(self, "_langfuse_finalize_once", None)
if finalize_once is not None:
finalize_once()

openai.Stream.__iter__ = traced_iter
_openai_stream_iter_hook_installed = True
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 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_iterator()'s finally block never fires and generation.end() is never called. Fix by installing an analogous async hook on openai.AsyncStream.__aiter__ and storing finalize_once as response._langfuse_finalize_once in _instrument_openai_async_stream.

Extended reasoning...

What the bug is and how it manifests

The PR fixes sync stream early-break finalization via two coordinated changes: (1) _install_openai_stream_iteration_hooks patches openai.Stream.__iter__ with a traced_iter that wraps the original with yield from original_iter(self) and calls self._langfuse_finalize_once() in a finally block, and (2) _instrument_openai_stream stores response._langfuse_finalize_once = finalize_once so the hook can reach the per-instance callback. Neither of these two steps was applied to the async path. _install_openai_stream_iteration_hooks never patches openai.AsyncStream.__aiter__, and _instrument_openai_async_stream never sets response._langfuse_finalize_once.

The specific code path that triggers it

openai.AsyncStream.__aiter__ (confirmed from OpenAI SDK _streaming.py) is an async generator: async for item in self._iterator: yield item. When a user does async for chunk in stream: break, Python creates an outer async generator G from __aiter__. G iterates self._iterator — which is traced_iterator() (call it TI) installed by _instrument_openai_async_stream. After yielding the first chunk, break causes Python to call await G.aclose(), throwing GeneratorExit into G at the yield item point.

Why existing code does not prevent it

G terminates on GeneratorExit, but self._iterator (TI) is stored on the response object as an attribute — it is not a local variable in G's frame. Python only automatically propagates aclose() to generators stored as frame-local variables (via yield from / async for on a local). Here, TI's refcount does not drop to zero synchronously, so TI.aclose() is never called. TI's finally: await finalize_once() never executes. traced_aclose / traced_close are only invoked by explicit stream.aclose() / stream.close() calls — they are not triggered by early break.

What the impact would be

generation.end() is never called immediately. The Langfuse generation leaks until either the user explicitly calls await stream.aclose() or the stream object is garbage-collected (non-deterministic). Users who read only the first N chunks from an async stream without remembering to call aclose() will silently lose Langfuse traces. This is an observability correctness regression: the previous LangfuseResponseGeneratorAsync.__aiter__ had a try/finally directly in the generator the caller iterates, guaranteeing deterministic finalization on any exit path including break.

How to fix it

Two symmetric changes are needed: (1) in _install_openai_stream_iteration_hooks, after patching openai.Stream.__iter__, also install an analogous async hook on openai.AsyncStream.__aiter__:

original_aiter = openai.AsyncStream.__aiter__
async def traced_aiter(self):
    try:
        async for x in original_aiter(self):
            yield x
    finally:
        cb = getattr(self, "_langfuse_finalize_once", None)
        if cb is not None:
            await cb()
openai.AsyncStream.__aiter__ = traced_aiter

(2) in _instrument_openai_async_stream, add response._langfuse_finalize_once = finalize_once immediately after defining finalize_once, mirroring the sync path (line 917 in the new code). A corresponding test test_openai_async_stream_break_still_finalizes_generation should also be added.

Step-by-step proof

  1. User calls await openai.chat.completions.create(..., stream=True) → returns native openai.AsyncStream s with s._iterator = traced_iterator() (TI).
  2. User writes: async for chunk in s: break
  3. Python calls s.__aiter__() → outer async generator G. G executes async for item in self._iterator: yield item, reading s._iterator = TI.
  4. G yields the first item. break causes Python to call await G.aclose().
  5. GeneratorExit is thrown into G at the yield item point. G has no try/finally, so it terminates immediately.
  6. TI is still alive: s._iterator still holds a strong reference. TI.aclose() is NOT called. TI's finally does NOT run.
  7. finalize_once() is never called; generation.end() is never called.
  8. The generation leaks until s is GC-collected (at which point s._iterator is released and TI eventually finalized — non-deterministic).
  9. The test suite has test_openai_stream_break_still_finalizes_generation for the sync case but no equivalent async test, confirming the async path was overlooked.

Comment thread langfuse/openai.py
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b5a7071be7

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread langfuse/openai.py
Comment on lines +998 to +1000
response._iterator = traced_iterator()
response.close = traced_close
response.aclose = traced_aclose
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Finalize async native streams on early loop exit

In _instrument_openai_async_stream, we only swap response._iterator and patch close/aclose, but unlike the sync path there is no AsyncStream iteration hook to finalize on async for ... break. Because the stream keeps a strong reference to response._iterator, breaking early leaves traced_iterator() unclosed, so its finally never runs and generation.end() is skipped until GC or explicit aclose(). In practice this can leave observations open and cause langfuse.flush() to miss partial async stream traces whenever callers stop consuming early.

Useful? React with 👍 / 👎.

Comment thread langfuse/openai.py
Comment on lines 1293 to 1308
pass

async def _finalize(self) -> None:
try:
model, completion, usage, metadata = (
_extract_streamed_response_api_response(self.items)
if self.resource.object == "Responses"
or self.resource.object == "AsyncResponses"
else _extract_streamed_openai_response(self.resource, self.items)
)

_create_langfuse_update(
completion,
self.generation,
self.completion_start_time,
model=model,
usage=usage,
metadata=metadata,
)
except Exception:
pass
finally:
self.generation.end()
if self._is_finalized:
return

self._is_finalized = True
_finalize_stream_response(
resource=self.resource,
items=self.items,
generation=self.generation,
completion_start_time=self.completion_start_time,
)

async def close(self) -> None:
"""Close the response and release the connection.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟣 The fallback async wrapper's close(), aclose(), and __aexit__ methods never call self._finalize(), so if a user receives a LangfuseResponseGeneratorAsync wrapper (for non-OpenAI generator-style async streams) and closes or context-manages it without iterating, generation.end() is never called and the Langfuse span leaks. The same issue exists for LangfuseResponseGeneratorSync.__exit__. This is a pre-existing issue that predates this PR; the PR's _is_finalized guard improvements to _finalize() make the fix straightforward — add await self._finalize() to close()/aclose()/__aexit__ and self._finalize() to __exit__.

Extended reasoning...

What the bug is and how it manifests

LangfuseResponseGeneratorAsync exposes close() and aclose() methods (lines ~1307–1319 in the modified file) that only delegate to self.response.close()/self.response.aclose() without calling self._finalize(). The __aexit__ method (line ~1293) is implemented as pass. For the sync counterpart, LangfuseResponseGeneratorSync.__exit__ is similarly a no-op pass (line ~1231). If a user receives the fallback wrapper (returned when the response lacks a _iterator attribute, i.e. for non-native-OpenAI generator-style streams) and calls await stream.aclose() or uses it as an async context manager without iterating, generation.end() is never called.

The specific code path that triggers it

The only paths in LangfuseResponseGeneratorAsync that call self._finalize() are: (1) __aiter__'s finally block (line ~1270), and (2) __anext__'s except StopAsyncIteration handler (line ~1285). Both require actual iteration. close() executes await self.response.close() and returns; aclose() calls await self.response.aclose(); __aexit__ does pass. None of these reach _finalize(). For the async generator returned by __aiter__, Python's async generator finalization is non-deterministic without asyncio loop shutdown hooks (PEP 525), so GC-based finalization is not reliable.

Why existing code does not prevent it

The PR added an _is_finalized idempotency guard to _finalize() (clearly intending to improve finalization correctness), but did not extend that pass to wire up close(), aclose(), or __aexit__/__exit__ as callers of _finalize(). The contrast with _instrument_openai_async_stream (the new native-stream path in this same PR) is clear: its traced_close and traced_aclose both call finalize_once() in a finally block. The fallback wrapper was not updated with the same pattern.

What the impact would be

Any code using the fallback path (non-OpenAI async generators fed through the instrumented API) that calls await stream.aclose() or uses async with stream: without exhausting the stream will silently leak the Langfuse generation span. generation.end() is never called, the span remains open indefinitely, and calls to langfuse.flush() will race against GC-based finalization.

How to fix it

Add await self._finalize() at the end of close() and aclose(), and replace pass in __aexit__ with await self._finalize(). For the sync class, replace pass in __exit__ with self._finalize(). The _is_finalized guard (already in place from this PR) prevents double-finalization if the stream was also consumed via iteration.

Step-by-step proof

  1. User's code receives a LangfuseResponseGeneratorAsync wrapper W (fallback path: async generator response without _iterator).
  2. User writes await W.aclose() without iterating.
  3. aclose() executes await self.response.aclose() and returns — no call to self._finalize().
  4. generation.end() is never called.
  5. The Langfuse generation span leaks until GC or explicit event loop shutdown.
  6. Alternatively, user writes async with W as gen: pass__aenter__ returns self.__aiter__(), a fresh async generator that has not been iterated; __aexit__ runs pass; the async generator object goes out of scope. Without asyncio asyncgen hooks, its finally block is not guaranteed to run synchronously.
  7. langfuse_client.flush() immediately after the with block may not see the generation end event.

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