Files
semantic-kernel/python/semantic_kernel/connectors
da8ca43064 Python: fix (openai): only propagate usage metadata for final streaming chunk without choices (#12977)
### Motivation and Context

1. Why is this change required?
- The previous condition copied usage metadata whenever chunk.usage
existed, which could incorrectly propagate usage into chunks that
already contained choices.

2. What problem does it solve?
- Prevents incorrect propagation of usage metadata that can cause empty
messages or duplicated metadata when a streaming chunk contains choices
plus usage.

3. What scenario does it contribute to?
- Streaming OpenAI chat responses where the API may send a final
usage-only chunk (no choices) to report usage stats.

4. If it fixes an open issue, please link to the issue here.
   - No linked issue.

### Description

- File modified:
-
python/semantic_kernel/connectors/ai/open_ai/services/open_ai_chat_completion_base.py

- Change summary:
- In
OpenAIChatCompletionBase._inner_get_streaming_chat_message_contents,
    replace:
      if chunk.usage is not None
    with:
      if len(chunk.choices) == 0 and chunk.usage is not None

- Design notes:
- This is a minimal, targeted fix to ensure usage metadata is only
copied when the streaming chunk contains no choices (typical for the
final usage-only chunk).
  - Behavior of other streaming logic is unchanged.
  - Recommend adding unit tests covering:
- A chunk that contains choices and usage (should not propagate usage).
- A usage-only final chunk (should propagate usage to the synthesized
choice).

### Contribution Checklist

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the SK Contribution Guidelines and formatting
script raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

Files changed:
-
python/semantic_kernel/connectors/ai/open_ai/services/open_ai_chat_completion_base.py

---------

Co-authored-by: chentiantian <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Evan Mattson <[email protected]>
2025-09-03 16:35:54 +00:00
..
2024-11-19 16:36:01 +00:00