Skip to content

Python: Emit usage chunks via callback as they have empty content #12447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

moonbox3
Copy link
Contributor

Motivation and Context

We recently made an update to emit streaming chunk usage for the ChatCompletionAgent, but that can now include further complications as responses with usage will have empty content. We should be emitting this information via the optional on_intermediate_message callback, so that the caller has content provided in the normal flow (anything un-related to the final message should be handled via the callback).

Description

Fix the usage chunk flow and update the sample to show this behavior.

Contribution Checklist

@moonbox3 moonbox3 self-assigned this Jun 11, 2025
@moonbox3 moonbox3 requested a review from a team as a code owner June 11, 2025 01:52
@markwallace-microsoft markwallace-microsoft added the python Pull requests for the Python Semantic Kernel label Jun 11, 2025
@markwallace-microsoft
Copy link
Member

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
agents/chat_completion
   chat_completion_agent.py2283186%68, 73, 78, 84, 100, 102, 108–112, 170, 173, 183, 191, 194, 196, 205, 231, 250, 252–253, 255–256, 258, 406, 462–464, 516, 593
TOTAL26437395485% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
3630 22 💤 0 ❌ 0 🔥 1m 51s ⏱️

@@ -450,9 +450,22 @@ async def invoke_stream(
response.name = self.name
response_builder.append(response.content)

# Divert usage-based responses to the intermediate message handler, if present
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: is this what dotnet is doing? I wonder if this is needed as we are draining the messages below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We drain function calling related methods below because they exist in the chat history. Usage related chunks aren't contained in the chat history, so they need specific handling here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should the usage chunks be part of the chat history at the chat completion service level?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agents python Pull requests for the Python Semantic Kernel
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

3 participants