Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions python/packages/core/agent_framework/openai/_chat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,21 +310,19 @@ def _parse_response_update_from_openai(
) -> ChatResponseUpdate:
"""Parse a streaming response update from OpenAI."""
chunk_metadata = self._get_metadata_from_streaming_chat_response(chunk)
if chunk.usage:
return ChatResponseUpdate(
role=Role.ASSISTANT,
contents=[
Content.from_usage(
usage_details=self._parse_usage_from_openai(chunk.usage), raw_representation=chunk
)
],
model_id=chunk.model,
additional_properties=chunk_metadata,
response_id=chunk.id,
message_id=chunk.id,
)
contents: list[Content] = []
finish_reason: FinishReason | None = None

# BUGFIX: Process usage alongside text/tool calls instead of early return
# Gemini (and potentially other providers) include both usage and content in the same chunk
Comment on lines +316 to +317
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment format "BUGFIX:" is not consistent with the existing comment style in this file. Other explanatory comments in the codebase (e.g., line 414: "When you enable asynchronous content filtering...") use a more descriptive, natural language style without special prefixes. Consider rephrasing to match the existing style, such as: "Process usage alongside text/tool calls to preserve all content types. Some providers (e.g., Gemini) include both usage and content in the same chunk."

Suggested change
# BUGFIX: Process usage alongside text/tool calls instead of early return
# Gemini (and potentially other providers) include both usage and content in the same chunk
# Process usage alongside text and tool calls to preserve all content types.
# Some providers (for example Gemini) include both usage and content in the same chunk.

Copilot uses AI. Check for mistakes.
if chunk.usage:
contents.append(
Content.from_usage(
usage_details=self._parse_usage_from_openai(chunk.usage), raw_representation=chunk
)
)

# Process text and tool calls from choices
for choice in chunk.choices:
chunk_metadata.update(self._get_metadata_from_chat_choice(choice))
contents.extend(self._parse_tool_calls_from_openai(choice))
Expand All @@ -335,6 +333,7 @@ def _parse_response_update_from_openai(
contents.append(text_content)
if reasoning_details := getattr(choice.delta, "reasoning_details", None):
contents.append(Content.from_text_reasoning(protected_data=json.dumps(reasoning_details)))

return ChatResponseUpdate(
created_at=datetime.fromtimestamp(chunk.created, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
contents=contents,
Expand Down
Loading