fix: ensure raw_stream.response is httpx.Response in langfuse streaming adapter
When Langfuse is enabled, the object passed to OpenAI’s stream manager as raw_stream is adapter LangfuseResponseGeneratorAsync. The OpenAI manager sets self._response = raw_stream.response and then calls await self._response.aclose(). LangfuseResponseGeneratorAsync previously set response to the AsyncStream itself (not to httpx.Response), so the manager tried to call aclose() on an AsyncStream, causing:
AttributeError: 'AsyncStream' object has no attribute 'aclose'
Steps to reproduce
- openai == 2.4.0
- langfuse == 3.7.0
from langfuse.openai import AsyncOpenAI
request_options = {
"model": "...",
"messages": [{"role": "user", "content": "Hello"}],
"n": 1,
}
async with AsyncOpenAI().chat.completions.stream(**request_options) as stream:
async for event in stream:
print(event)
[!IMPORTANT] Ensure
raw_stream.responseishttpx.Responseby renamingresponsetostreamand settingself.responsetostream.responsein relevant classes and functions.
- Behavior:
- Ensure
raw_stream.responseishttpx.Responsein async adapter by renamingresponsetostreamin_wrapand_wrap_asyncfunctions.- Set
self.responsetostream.responseinLangfuseResponseGeneratorSyncandLangfuseResponseGeneratorAsyncclasses.- Classes:
LangfuseResponseGeneratorSync: Renamedresponsetostreamand setself.responsetostream.response.LangfuseResponseGeneratorAsync: Renamedresponsetostreamand setself.responsetostream.response.- Functions:
_wrap: Renamedresponsetostream._wrap_async: Renamedresponsetostream.This description was created by
for 816c68a7d57abb8363f929dcb0aded68c8e4c3e2. You can customize this summary. It will automatically update as commits are pushed.
Greptile encountered an error while reviewing this PR. Please reach out to [email protected] for assistance.
@hassiebp Hi! Maybe add some tests?