Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python: Introduced a new condition to yield `StreamingChatMessageCont…
…ent` directly when usage data is available. (#9753) ### Motivation and Context issue: #9751 This pull request addresses a bug where setting `stream_options.include_usage` to `True` does not return token usage, resulting in `None` for the `usage` field. The issue occurs when using Azure OpenAI's GPT-4o and GPT-4omini models. In particular, if the last chunk of the response has an empty `choices` list, the chunk is skipped entirely, and the token usage is not processed correctly. In the Azure OpenAI implementation, if `usage` information is included, the chunk should be processed appropriately. However, the current code skips processing when `choices` is empty. This pull request fixes this behavior so that the chunk is processed when `usage` is present, even if `choices` is empty. ### Description This fix includes the following changes: - Modified the relevant section in `azure_chat_completion.py` to ensure that chunks with empty `choices` are not skipped if `usage` information is present. - Specifically, the condition `if len(chunk.choices) == 0:` was updated to allow chunks with `usage` data to be processed correctly. With these changes, setting `stream_options.include_usage` to `True` will correctly return token usage data, even for chunks where the `choices` list is empty. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: Tao Chen <[email protected]>
- Loading branch information