Skip to content

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

Description

Describe the bug

In the Python MistralAIChatCompletion connector, metadata["usage"] is set to a one-element tuple instead of a CompletionUsage, because of a trailing comma (mistral_ai_chat_completion.py#L254-L259):

metadata["usage"] = (
    CompletionUsage(
        prompt_tokens=response.usage.prompt_tokens,
        completion_tokens=response.usage.completion_tokens,
    ),
)

As a result:

  • the telemetry decorator only records token usage when isinstance(usage, CompletionUsage) (decorators.py#L422-L427), so Mistral chat spans carry no gen_ai.usage.input_tokens / gen_ai.usage.output_tokens;
  • user code that reads result.metadata["usage"].prompt_tokens (as it does for the other connectors) raises AttributeError: 'tuple' object has no attribute 'prompt_tokens'.

Expected behavior

metadata["usage"] is a CompletionUsage, as in the OpenAI, Google, Bedrock and Ollama connectors.

Fix

Remove the trailing comma (the outer parentheses then become a plain grouping).

Platform

  • Language: Python, semantic-kernel main (7a92cd4)

Found by reading the code while checking how connectors report usage to telemetry; it was not run against the Mistral API here. This report was drafted with Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pythonPull requests for the Python Semantic Kerneltriage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions