Skip to content

Commit 9b2d886

Browse files
authored
Merge branch 'main' into fix/retry-httpx-timeout
2 parents 3a69649 + f8a2e7e commit 9b2d886

3 files changed

Lines changed: 58 additions & 6 deletions

File tree

‎google/genai/_api_client.py‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,22 @@ def _iter_response_stream(self) -> Iterator[str]:
341341

342342
chunk = ''
343343
balance = 0
344+
data_buffer: list[str] = []
344345
if isinstance(self.response_stream, httpx.Response):
345346
response_stream = self.response_stream.iter_lines()
346347
else:
347348
response_stream = self.response_stream.iter_lines(decode_unicode=True)
348349
for line in response_stream:
349350
if not line:
351+
if data_buffer:
352+
yield '\n'.join(data_buffer)
353+
data_buffer = []
350354
continue
351355

352356
# In streaming mode, the response of JSON is prefixed with "data: " which
353357
# we must strip before parsing.
354358
if line.startswith('data: '):
355-
yield line[len('data: '):]
359+
data_buffer.append(line[len('data: '):])
356360
continue
357361

358362
# When API returns an error message, it comes line by line. So we buffer
@@ -372,6 +376,8 @@ def _iter_response_stream(self) -> Iterator[str]:
372376
# If there is any remaining chunk, yield it.
373377
if chunk:
374378
yield chunk
379+
if data_buffer:
380+
yield '\n'.join(data_buffer)
375381

376382
async def _aiter_response_stream(self) -> AsyncIterator[str]:
377383
"""Asynchronously iterates over chunks retrieved from the API."""
@@ -387,16 +393,20 @@ async def _aiter_response_stream(self) -> AsyncIterator[str]:
387393

388394
chunk = ''
389395
balance = 0
396+
data_buffer: list[str] = []
390397
# httpx.Response has a dedicated async line iterator.
391398
if isinstance(self.response_stream, httpx.Response):
392399
try:
393400
async for line in self.response_stream.aiter_lines():
394401
if not line:
402+
if data_buffer:
403+
yield '\n'.join(data_buffer)
404+
data_buffer = []
395405
continue
396406
# In streaming mode, the response of JSON is prefixed with "data: "
397407
# which we must strip before parsing.
398408
if line.startswith('data: '):
399-
yield line[len('data: '):]
409+
data_buffer.append(line[len('data: '):])
400410
continue
401411

402412
# When API returns an error message, it comes line by line. So we buffer
@@ -415,6 +425,8 @@ async def _aiter_response_stream(self) -> AsyncIterator[str]:
415425
# If there is any remaining chunk, yield it.
416426
if chunk:
417427
yield chunk
428+
if data_buffer:
429+
yield '\n'.join(data_buffer)
418430
finally:
419431
# Close the response and release the connection.
420432
await self.response_stream.aclose()
@@ -432,12 +444,15 @@ async def _aiter_response_stream(self) -> AsyncIterator[str]:
432444
# Decode the bytes and remove trailing whitespace and newlines.
433445
line = line_bytes.decode('utf-8').rstrip()
434446
if not line:
447+
if data_buffer:
448+
yield '\n'.join(data_buffer)
449+
data_buffer = []
435450
continue
436451

437452
# In streaming mode, the response of JSON is prefixed with "data: "
438453
# which we must strip before parsing.
439454
if line.startswith('data: '):
440-
yield line[len('data: '):]
455+
data_buffer.append(line[len('data: '):])
441456
continue
442457

443458
# When API returns an error message, it comes line by line. So we
@@ -456,6 +471,8 @@ async def _aiter_response_stream(self) -> AsyncIterator[str]:
456471
# If there is any remaining chunk, yield it.
457472
if chunk:
458473
yield chunk
474+
if data_buffer:
475+
yield '\n'.join(data_buffer)
459476
finally:
460477
# Release the connection back to the pool for potential reuse.
461478
self.response_stream.release()

‎google/genai/tests/client/test_async_stream.py‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ async def test_httpx_simple_lines(responses: api_client.HttpResponse):
137137

138138
@pytest.mark.asyncio
139139
async def test_httpx_data_prefix(responses: api_client.HttpResponse):
140-
lines = ["data: { 'message': 'hello' }", "data: { 'status': 'ok' }"]
140+
lines = ["data: { 'message': 'hello' }", "", "data: { 'status': 'ok' }"]
141141
mock_response = MockHTTPXResponse(lines)
142142
responses.response_stream = mock_response
143143

@@ -147,13 +147,47 @@ async def test_httpx_data_prefix(responses: api_client.HttpResponse):
147147
mock_response.aiter_lines.assert_called_once()
148148
mock_response.aclose.assert_called_once()
149149

150+
@pytest.mark.asyncio
151+
async def test_httpx_multiline_data(responses: api_client.HttpResponse) -> None:
152+
lines = [
153+
"data: {",
154+
"data: 'message': 'hello'",
155+
"data: }",
156+
"",
157+
]
158+
mock_response = MockHTTPXResponse(lines)
159+
responses.response_stream = mock_response
160+
161+
results = [line async for line in responses._aiter_response_stream()]
162+
163+
assert results == ["{\n 'message': 'hello'\n}"]
164+
mock_response.aiter_lines.assert_called_once()
165+
mock_response.aclose.assert_called_once()
166+
167+
168+
def test_httpx_multiline_data_sync(responses: api_client.HttpResponse) -> None:
169+
lines = [
170+
"data: {",
171+
"data: 'message': 'hello'",
172+
"data: }",
173+
"",
174+
]
175+
mock_response = MagicMock(spec=httpx.Response)
176+
mock_response.iter_lines.return_value = iter(lines)
177+
responses.response_stream = mock_response
178+
179+
results = list(responses._iter_response_stream())
180+
181+
assert results == ["{\n 'message': 'hello'\n}"]
182+
150183

151184
@pytest.mark.asyncio
152185
async def test_httpx_multiple_json_chunk(responses: api_client.HttpResponse):
153186
lines = [
154187
'{ "id": 1 }',
155188
"",
156189
'data: { "id": 2 }',
190+
"",
157191
'data: { "id": 3 }',
158192
]
159193
mock_response = MockHTTPXResponse(lines)
@@ -214,7 +248,7 @@ async def test_aiohttp_simple_lines(responses: api_client.HttpResponse):
214248
@pytest.mark.asyncio
215249
async def test_aiohttp_data_prefix(responses: api_client.HttpResponse):
216250
api_client.has_aiohttp = True # Force aiohttp
217-
lines = ["data: { 'message': 'hello' }", "data: { 'status': 'ok' }"]
251+
lines = ["data: { 'message': 'hello' }", "", "data: { 'status': 'ok' }"]
218252
# Use the mock class that pretends to be aiohttp.ClientResponse
219253
mock_response = MockAIOHTTPResponse(lines)
220254
responses.response_stream = mock_response
@@ -234,6 +268,7 @@ async def test_aiohttp_multiple_json_chunks(responses: api_client.HttpResponse):
234268
'{ "id": 1 }',
235269
"", # empty line to check robustness
236270
'data: { "id": 2 }',
271+
"",
237272
'data: { "id": 3 }',
238273
]
239274
# Use the mock class that pretends to be aiohttp.ClientResponse

‎google/genai/types.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8131,7 +8131,7 @@ def _from_response(
81318131
inspect.isclass(response_schema)
81328132
and not (
81338133
isinstance(response_schema, builtin_types.GenericAlias)
8134-
) # Needed for Python 3.9 and 3.10
8134+
) # Needed for Python 3.10
81358135
and issubclass(response_schema, pydantic.BaseModel)
81368136
):
81378137
# Pydantic schema.

0 commit comments

Comments
 (0)