Skip to content

Commit d5c32a9

Browse files
authored
fix: async streaming chunk indices in GoogleGenAIChatGenerator start at 0, not 1 (#3410)
1 parent 3fbf2dd commit d5c32a9

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

integrations/google_genai/src/haystack_integrations/components/generators/google_genai/chat/chat_generator.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,14 @@ async def _handle_streaming_response_async(
361361
i = 0
362362
chunk = None
363363
async for chunk in response_stream:
364-
i += 1
365-
366364
streaming_chunk = _convert_google_chunk_to_streaming_chunk(
367365
chunk=chunk, index=i, component_info=component_info, model=self._model
368366
)
369367
chunks.append(streaming_chunk)
370368

371369
# Stream the chunk
372370
await streaming_callback(streaming_chunk)
371+
i += 1
373372

374373
# Use custom aggregation that supports reasoning content
375374
message = _aggregate_streaming_chunks_with_reasoning(chunks)

integrations/google_genai/tests/test_chat_generator.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,30 @@ async def callback(c):
501501
assert "Hello async" in results["replies"][0].text
502502
assert len(callback_chunks) == 1
503503

504+
@pytest.mark.asyncio
505+
async def test_run_async_streaming_chunk_indices_start_at_zero(self, monkeypatch, mock_streaming_chunk):
506+
monkeypatch.setenv("GOOGLE_API_KEY", "test-api-key")
507+
component = GoogleGenAIChatGenerator()
508+
509+
chunks = [mock_streaming_chunk(text="Hello "), mock_streaming_chunk(text="world", finish_reason="STOP")]
510+
511+
async def mock_stream():
512+
for chunk in chunks:
513+
yield chunk
514+
515+
component._client.aio.models.generate_content_stream = AsyncMock(return_value=mock_stream())
516+
517+
callback_chunks = []
518+
519+
async def callback(c):
520+
callback_chunks.append(c)
521+
522+
await component.run_async([ChatMessage.from_user("Hi")], streaming_callback=callback)
523+
524+
# Async indices must match the sync path (0-based); the first text chunk must mark the stream start
525+
assert [c.index for c in callback_chunks] == [0, 1]
526+
assert callback_chunks[0].start is True
527+
504528
@pytest.mark.asyncio
505529
async def test_run_async_extracts_system_message(self, monkeypatch, mock_response):
506530
monkeypatch.setenv("GOOGLE_API_KEY", "test-api-key")

0 commit comments

Comments
 (0)