Skip to content

Commit c072a8b

Browse files
Python: fix(python): set Mistral metadata usage to CompletionUsage instead of tuple (#14629)
### 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 @baywet @eavanvalkenburg
1 parent c28d64d commit c072a8b

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

‎python/semantic_kernel/connectors/ai/mistral_ai/services/mistral_ai_chat_completion.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,9 @@ def _get_metadata_from_response(self, response: ChatCompletionResponse | Complet
251251
}
252252
# Check if usage exists and has a value, then add it to the metadata
253253
if hasattr(response, "usage") and response.usage is not None:
254-
metadata["usage"] = (
255-
CompletionUsage(
256-
prompt_tokens=response.usage.prompt_tokens,
257-
completion_tokens=response.usage.completion_tokens,
258-
),
254+
metadata["usage"] = CompletionUsage(
255+
prompt_tokens=response.usage.prompt_tokens,
256+
completion_tokens=response.usage.completion_tokens,
259257
)
260258

261259
return metadata

‎python/tests/unit/connectors/ai/mistral_ai/services/test_mistralai_chat_completion.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
)
1616

1717
from semantic_kernel.connectors.ai.chat_completion_client_base import ChatCompletionClientBase
18+
from semantic_kernel.connectors.ai.completion_usage import CompletionUsage
1819
from semantic_kernel.connectors.ai.function_call_choice_configuration import FunctionCallChoiceConfiguration
1920
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior, FunctionChoiceType
2021
from semantic_kernel.connectors.ai.mistral_ai.prompt_execution_settings.mistral_ai_prompt_execution_settings import (
@@ -112,6 +113,9 @@ async def test_complete_chat_contents(
112113
chat_history=chat_history, settings=mock_settings, kernel=kernel, arguments=arguments
113114
)
114115
assert content is not None
116+
assert isinstance(content[0].metadata["usage"], CompletionUsage)
117+
assert content[0].metadata["usage"].prompt_tokens == 10
118+
assert content[0].metadata["usage"].completion_tokens == 5
115119

116120

117121
mock_message_text_content = ChatMessageContent(role=AuthorRole.ASSISTANT, items=[TextContent(text="test")])

0 commit comments

Comments
 (0)