Skip to content

Commit 997bf1c

Browse files
committed
fix: append the current model chunk to contents in async streaming
1 parent 5936664 commit 997bf1c

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

google/genai/_extra_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,14 @@ async def parse_config_for_mcp_sessions(
536536
def append_chunk_contents(
537537
contents: Union[types.ContentListUnion, types.ContentListUnionDict],
538538
chunk: types.GenerateContentResponse,
539-
) -> None:
540-
"""Appends the contents of the chunk to the contents list."""
539+
) -> Union[types.ContentListUnion, types.ContentListUnionDict]:
540+
"""Appends the contents of the chunk to the contents list and returns it."""
541541
if chunk is not None and chunk.candidates is not None:
542542
chunk_content = chunk.candidates[0].content
543543
contents = t.t_contents(contents) # type: ignore[assignment]
544544
if isinstance(contents, list) and chunk_content is not None:
545545
contents.append(chunk_content) # type: ignore[arg-type]
546+
return contents
546547

547548

548549
def prepare_resumable_upload(

google/genai/models.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5168,7 +5168,7 @@ def generate_content_stream(
51685168
# Yield chunks only if there's no function response parts.
51695169
for chunk in response:
51705170
if not function_map:
5171-
_extra_utils.append_chunk_contents(contents, chunk)
5171+
contents = _extra_utils.append_chunk_contents(contents, chunk)
51725172
yield chunk
51735173
else:
51745174
if (
@@ -5181,7 +5181,7 @@ def generate_content_stream(
51815181
chunk, function_map
51825182
)
51835183
if not func_response_parts:
5184-
_extra_utils.append_chunk_contents(contents, chunk)
5184+
contents = _extra_utils.append_chunk_contents(contents, chunk)
51855185
yield chunk
51865186

51875187
else:
@@ -5191,7 +5191,7 @@ def generate_content_stream(
51915191
chunk.automatic_function_calling_history = (
51925192
automatic_function_calling_history
51935193
)
5194-
_extra_utils.append_chunk_contents(contents, chunk)
5194+
contents = _extra_utils.append_chunk_contents(contents, chunk)
51955195
yield chunk
51965196
if (
51975197
chunk is None
@@ -6938,7 +6938,7 @@ async def async_generator(model, contents, config): # type: ignore[no-untyped-d
69386938
# Yield chunks only if there's no function response parts.
69396939
async for chunk in response: # type: ignore[attr-defined]
69406940
if not function_map:
6941-
_extra_utils.append_chunk_contents(contents, chunk)
6941+
contents = _extra_utils.append_chunk_contents(contents, chunk)
69426942
yield chunk
69436943
else:
69446944
if (
@@ -6953,7 +6953,7 @@ async def async_generator(model, contents, config): # type: ignore[no-untyped-d
69536953
)
69546954
)
69556955
if not func_response_parts:
6956-
_extra_utils.append_chunk_contents(contents, chunk)
6956+
contents = _extra_utils.append_chunk_contents(contents, chunk)
69576957
yield chunk
69586958

69596959
else:
@@ -6964,7 +6964,7 @@ async def async_generator(model, contents, config): # type: ignore[no-untyped-d
69646964
chunk.automatic_function_calling_history = (
69656965
automatic_function_calling_history
69666966
)
6967-
_extra_utils.append_chunk_contents(contents, chunk)
6967+
contents = _extra_utils.append_chunk_contents(contents, chunk)
69686968
yield chunk
69696969
if (
69706970
chunk is None

0 commit comments

Comments
 (0)