Skip to content

chat_stream() logs spurious warning "Failed to parse SSE data field as JSON" for [DONE] stream terminator #730

@Undiluted7027

Description

@Undiluted7027

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:
      return

But [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
Image Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions