-
Notifications
You must be signed in to change notification settings - Fork 2k
Closed
langfuse/langfuse-python
#1126Labels
Description
Describe the bug
Hi,
I'm using the drop in replacement for openai. I am running into separate issues when stream = True
- Iterating over the response throws an exception at the end
- The output isn't captured in the trace that gets logged in the cloud
Neither of these issue are present when stream=False.
To reproduce
More details below.
Issue 1:
Running the following code throws an exception at the end
from langfuse.openai import OpenAI, openai
client = OpenAI()
response = client.completions.create(
prompt = "tell me a joke",
stream=True,
)
for chunk in response:
print(chunk)
Exception
Traceback (most recent call last):
for chunk in response:
File ".venv/lib/python3.10/site-packages/langfuse/openai.py", line 864, in __iter__
self._finalize()
File ".venv/lib/python3.10/site-packages/langfuse/openai.py", line 888, in _finalize
model, completion, usage = _extract_streamed_openai_response(
File ".venv/lib/python3.10/site-packages/langfuse/openai.py", line 559, in _extract_streamed_openai_response
completion += choice.get("text", None)
I was able to fix this by using changing the iterator to use the inner response object
for chunk in response.response:
print(chunk)
Issue 2:
from langfuse.openai import OpenAI, openai
client = OpenAI()
openai.langfuse_debug = True
response = client.completions.create(
prompt = "tell me a joke",
stream=True,
)
for chunk in response.response:
print(chunk)
The code runs but when I look at the trace online, the output is null
I tried adding
langfuse_context.flush() and openai.flush_langfuse() at the end as per the documentation but neither seem to do anything.
With the debugger on, I can see that when stream=False, there's a second call to update the trace. However, with stream=True that call isn't there.
DEBUG:langfuse:uploading data: {'batch': [{'id': 'ae6422a2-03af-4150-9166-19cd5980ae24', 'type': 'generation-update', 'body': {'traceId': 'd5d662c9-d499-4742-a911-8c334540057e', 'output': "Sure, here's a classic for you:\n\nWhy don't scientists trust atoms?\n\nBecause they make up everything!", 'id': '1b009061-6e47-4b55-b017-e3889060cff5', 'endTime': datetime.datetime(2025, 2, 11, 21, 35, 8, 276151, tzinfo=datetime.timezone.utc), 'model': 'gpt-4o-2024-05-13', 'usage': {'input': 11, 'output': 20, 'total': 31, 'unit': <ModelUsageUnit.TOKENS: 'TOKENS'>}, 'usageDetails': {'prompt_tokens': 11, 'completion_tokens': 20, 'total_tokens': 31, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}}}, 'timestamp': datetime.datetime(2025, 2, 11, 21, 35, 8, 277613, tzinfo=datetime.timezone.utc)}, {'id': '77c0abbe-b299-40f2-beef-a453db6487ed', 'type': 'trace-create', 'body': {'id': 'd5d662c9-d499-4742-a911-8c334540057e', 'timestamp': datetime.datetime(2025, 2, 11, 21, 35, 8, 277652, tzinfo=datetime.timezone.utc), 'output': "Sure, here's a classic for you:\n\nWhy don't scientists trust atoms?\n\nBecause they make up everything!"}, 'timestamp': datetime.datetime(2025, 2, 11, 21, 35, 8, 277729, tzinfo=datetime.timezone.utc)}], 'metadata': {'batch_size': 2, 'sdk_integration': 'openai', 'sdk_name': 'python', 'sdk_version': '2.57.13a0'}}
SDK and container versions
langfuse==2.59.2
openai==1.61.1
Additional information
No response
Are you interested to contribute a fix for this bug?
No
dosubot