-
Notifications
You must be signed in to change notification settings - Fork 82
Description
SDK Version (required)
5.20.5
Describe the bug
When using ClientV2.chat_stream(), a warning is logged at the end of every successful stream:
Failed to parse SSE data field as JSON: Expecting value: line 1 column 2 (char 1), data: [DONE]
The Cohere API sends [DONE] as the final SSE data value to signal end-of-stream. In v2/raw_client.py, _iter() already handles None as a terminator:
if _sse.data == None:
returnBut [DONE] falls through to parse_sse_obj(), which tries json.loads("[DONE]"), fails, and logs the warning. After that, parse_obj_as(V2ChatStreamResponse, sse_event) raises a Pydantic ValueError, which is caught by the outer except and silently skipped. So the stream completes correctly and the warning is purely spurious.
Steps to reproduce:
import logging
import cohere
# Enable all logging so the spurious warning is visible
logging.basicConfig(level=logging.DEBUG)
co = cohere.ClientV2()
print("Streaming response:")
for event in co.chat_stream(
model="command-r7b-12-2024",
messages=[{"role": "user", "content": "Who created you?"}],
):
event_type = getattr(event, "type", None)
if event_type == "content-delta":
text = (
event.delta.message.content.text
if event.delta and event.delta.message and event.delta.message.content
else ""
)
print(text, end="", flush=True)
print("\nDone.")A fix in v2/raw_client.py could be:
if _sse.data in (None, "[DONE]"):
return
