Skip to content

Python: fix(python): set Mistral metadata usage to CompletionUsage instead of tuple - #14629

Open
Hardik K (hardikkaurani) wants to merge 1 commit into
microsoft:mainfrom
hardikkaurani:fix/mistral-metadata-usage-tuple
Open

Hardik K (hardikkaurani) wants to merge 1 commit into
microsoft:mainfrom
hardikkaurani:fix/mistral-metadata-usage-tuple

Conversation

@hardikkaurani

Copy link
Copy Markdown

Motivation and Context

Fixes #14617

In the Python MistralAIChatCompletion connector, metadata["usage"] was being set to a 1-tuple (CompletionUsage(...),) instead of CompletionUsage(...) due to a trailing comma in mistral_ai_chat_completion.py.

Because of this:

  1. The telemetry decorator checks isinstance(usage, CompletionUsage), which evaluates to False for a tuple, silently omitting gen_ai.usage.input_tokens and gen_ai.usage.output_tokens from telemetry spans.
  2. Callers accessing result.metadata["usage"].prompt_tokens encounter AttributeError: 'tuple' object has no attribute 'prompt_tokens'.

Description

  • Removed trailing comma in _get_metadata_from_response within MistralAIChatCompletion so metadata["usage"] is assigned CompletionUsage.
  • Added assertions in test_get_chat_message_contents verifying that metadata["usage"] is an instance of CompletionUsage with expected token counts.

Fixes #14617

cc Vincent Biret (@baywet) Eduard van Valkenburg (@eavanvalkenburg)

Copilot AI balanced review requested due to automatic review settings October 19, 2026 20:08
@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner October 19, 2026 20:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Oct 19, 2026
@github-actions github-actions Bot changed the title fix(python): set Mistral metadata usage to CompletionUsage instead of tuple Python: fix(python): set Mistral metadata usage to CompletionUsage instead of tuple Oct 19, 2026
@hardikkaurani

Copy link
Copy Markdown
Author

Hi Vincent Biret (@baywet) Eduard van Valkenburg (@eavanvalkenburg), opened this PR to fix #14617 where MistralAIChatCompletion was setting metadata["usage"] to a tuple due to a trailing comma, breaking telemetry spans and caller token access. Regression assertions included in test_mistralai_chat_completion.py. Ready for review, thank you!

This branch was successfully deployed

1 active deployment
github-app-auth — d7defdd6 Deployed Oct 19, 2026 by hardikkaurani via team_check #648
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: MistralAIChatCompletion sets metadata['usage'] to a tuple, so usage is missing from telemetry

2 participants