Skip to content

Commit 0788805

Browse files
Fix streaming text lost when usage data in same chunk
Some providers (e.g. Gemini) include both usage data and text content in the same streaming chunk. The early return on chunk.usage caused text and tool call parsing to be skipped entirely. Remove the early return and process usage alongside text/tool calls. Fixes microsoft#3434 Co-authored-by: Copilot <[email protected]>
1 parent 5810f65 commit 0788805

2 files changed

Lines changed: 47 additions & 13 deletions

File tree

‎python/packages/core/agent_framework/openai/_chat_client.py‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -404,21 +404,18 @@ def _parse_response_update_from_openai(
404404
) -> ChatResponseUpdate:
405405
"""Parse a streaming response update from OpenAI."""
406406
chunk_metadata = self._get_metadata_from_streaming_chat_response(chunk)
407-
if chunk.usage:
408-
return ChatResponseUpdate(
409-
role="assistant",
410-
contents=[
411-
Content.from_usage(
412-
usage_details=self._parse_usage_from_openai(chunk.usage), raw_representation=chunk
413-
)
414-
],
415-
model_id=chunk.model,
416-
additional_properties=chunk_metadata,
417-
response_id=chunk.id,
418-
message_id=chunk.id,
419-
)
420407
contents: list[Content] = []
421408
finish_reason: FinishReason | None = None
409+
410+
# Process usage data (may coexist with text/tool content in providers like Gemini).
411+
# See https://github.com/microsoft/agent-framework/issues/3434
412+
if chunk.usage:
413+
contents.append(
414+
Content.from_usage(
415+
usage_details=self._parse_usage_from_openai(chunk.usage), raw_representation=chunk
416+
)
417+
)
418+
422419
for choice in chunk.choices:
423420
chunk_metadata.update(self._get_metadata_from_chat_choice(choice))
424421
contents.extend(self._parse_tool_calls_from_openai(choice))

‎python/packages/core/tests/openai/test_openai_chat_client.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,43 @@ def test_usage_content_in_streaming_response(openai_unit_test_env: dict[str, str
728728
assert usage_content.usage_details["total_token_count"] == 150
729729

730730

731+
def test_streaming_chunk_with_usage_and_text(openai_unit_test_env: dict[str, str]) -> None:
732+
"""Test that text content is not lost when usage data is in the same chunk.
733+
734+
Some providers (e.g. Gemini) include both usage and text content in the
735+
same streaming chunk. See https://github.com/microsoft/agent-framework/issues/3434
736+
"""
737+
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk, Choice, ChoiceDelta
738+
from openai.types.completion_usage import CompletionUsage
739+
740+
client = OpenAIChatClient()
741+
742+
mock_chunk = ChatCompletionChunk(
743+
id="test-chunk",
744+
object="chat.completion.chunk",
745+
created=1234567890,
746+
model="gemini-2.0-flash-lite",
747+
choices=[
748+
Choice(
749+
index=0,
750+
delta=ChoiceDelta(content="Hello world", role="assistant"),
751+
finish_reason=None,
752+
)
753+
],
754+
usage=CompletionUsage(prompt_tokens=18, completion_tokens=5, total_tokens=23),
755+
)
756+
757+
update = client._parse_response_update_from_openai(mock_chunk)
758+
759+
# Should have BOTH text and usage content
760+
content_types = [c.type for c in update.contents]
761+
assert "text" in content_types, "Text content should not be lost when usage is present"
762+
assert "usage" in content_types, "Usage content should still be present"
763+
764+
text_content = next(c for c in update.contents if c.type == "text")
765+
assert text_content.text == "Hello world"
766+
767+
731768
def test_parse_text_with_refusal(openai_unit_test_env: dict[str, str]) -> None:
732769
"""Test that refusal content is parsed correctly."""
733770
from openai.types.chat.chat_completion import ChatCompletion, Choice

0 commit comments

Comments
 (0)