Skip to content

fix: Support both "data:" and "data: " formats for streaming responses#2584

Open
CaroyalKnight wants to merge 3 commits into
googleapis:mainfrom
CaroyalKnight:fix/handle-stream-data-colon-variant
Open

fix: Support both "data:" and "data: " formats for streaming responses#2584
CaroyalKnight wants to merge 3 commits into
googleapis:mainfrom
CaroyalKnight:fix/handle-stream-data-colon-variant

Conversation

@CaroyalKnight

Copy link
Copy Markdown

If the streaming output starts with data: instead of data: , for example:

data:{"candidates":[{"content":{"role":"model","parts":[{"text":"Learn ...","thought":true}]}}],"usageMetadata":{"trafficType":"ON_DEMAND"}}

an error will occur.

Traceback (most recent call last):
  File ".venv/lib/python3.12/site-packages/google/genai/_api_client.py", line 495, in _load_json_from_response
    return json.loads(response)
           ^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/anaconda3/lib/python3.12/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/anaconda3/lib/python3.12/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/anaconda3/lib/python3.12/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "genai_request.py", line 29, in <module>
    for chunk in stream:
                 ^^^^^^
  File ".venv/lib/python3.12/site-packages/google/genai/models.py", line 6658, in generate_content_stream
    for chunk in response:
                 ^^^^^^^^
  File ".venv/lib/python3.12/site-packages/google/genai/models.py", line 5024, in _generate_content_stream
    for response in self._api_client.request_streamed(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/google/genai/_api_client.py", line 1629, in request_streamed
    for chunk in session_response.segments():
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/google/genai/_api_client.py", line 308, in segments
    yield self._load_json_from_response(chunk)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/google/genai/_api_client.py", line 497, in _load_json_from_response
    raise errors.UnknownApiResponseError(
google.genai.errors.UnknownApiResponseError: Failed to parse response as JSON. Raw response: data:{"candidates":[{"content":{"role":"model","parts":[{"text":"Learn ...","thought":true}]}}],"usageMetadata":{"trafficType":"ON_DEMAND"}}

Therefore, some changes have been made to support both data: and data: .

@google-cla

google-cla Bot commented Jun 11, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@Venkaiahbabuneelam Venkaiahbabuneelam self-assigned this Jun 11, 2026
@Venkaiahbabuneelam Venkaiahbabuneelam added the size:S Code changes < 10 lines label Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S Code changes < 10 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants