fix(mistral): remove accidental tuple wrapping of CompletionUsage in metadata#13676
fix(mistral): remove accidental tuple wrapping of CompletionUsage in metadata#13676frankgoldfish wants to merge 1 commit intomicrosoft:mainfrom
Conversation
… metadata
In `_get_metadata_from_response`, the `CompletionUsage` object was wrapped in
a single-element tuple due to a trailing comma after the closing parenthesis:
metadata["usage"] = (
CompletionUsage(...), # <-- trailing comma made this a tuple
)
This caused `metadata["usage"]` to be a `tuple` instead of a `CompletionUsage`
instance, breaking any code that expected to access token counts via
`metadata["usage"].prompt_tokens` or `metadata["usage"].completion_tokens`.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
moonbox3
left a comment
There was a problem hiding this comment.
Any unit test that needs to be added/updated for this?
There was a problem hiding this comment.
Pull request overview
Fixes token usage metadata in the Mistral chat completion connector so metadata["usage"] is a CompletionUsage instance (not an accidental single element tuple), preventing runtime AttributeError when accessing token counts.
Changes:
- Remove trailing comma that wrapped
CompletionUsage(...)in a single element tuple. - Ensure
metadata["usage"]is assigned directly to aCompletionUsageinstance whenresponse.usageis present.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if hasattr(response, "usage") and response.usage is not None: | ||
| metadata["usage"] = ( | ||
| CompletionUsage( | ||
| prompt_tokens=response.usage.prompt_tokens, | ||
| completion_tokens=response.usage.completion_tokens, | ||
| ), | ||
| metadata["usage"] = CompletionUsage( | ||
| prompt_tokens=response.usage.prompt_tokens, | ||
| completion_tokens=response.usage.completion_tokens, | ||
| ) |
There was a problem hiding this comment.
This bug fix changes the shape of metadata["usage"] (tuple -> CompletionUsage), but there is no regression test asserting the type/attributes of metadata["usage"] on returned ChatMessageContent / streaming chunks. Add a unit test that verifies response.metadata["usage"] is a CompletionUsage instance and that prompt_tokens/completion_tokens are accessible to prevent the tuple-wrapping bug from reappearing.
Summary
In
MistralAIChatCompletion._get_metadata_from_response, theCompletionUsageobject is accidentally wrapped in a single-element tuple due to a trailing comma after the closing parenthesis of the constructor call.Bug
Any code that reads
metadata["usage"].prompt_tokensormetadata["usage"].completion_tokenswill get anAttributeErrorat runtime because tuples don't have those attributes.Fix
Files Changed
python/semantic_kernel/connectors/ai/mistral_ai/services/mistral_ai_chat_completion.pyTest plan
metadata["usage"]is aCompletionUsageinstance (not a tuple) after a MistralAI chat completion responsemetadata["usage"].prompt_tokensandmetadata["usage"].completion_tokensare accessible without error🤖 Generated with Claude Code