Repository navigation
Commit c072a8b
authored
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 @eavanvalkenburg1 parent c28d64d commit c072a8b
2 files changed
Lines changed: 7 additions & 5 deletions
File tree
- python
- semantic_kernel/connectors/ai/mistral_ai/services
- tests/unit/connectors/ai/mistral_ai/services
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
259 | 257 | | |
260 | 258 | | |
261 | 259 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
112 | 113 | | |
113 | 114 | | |
114 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
115 | 119 | | |
116 | 120 | | |
117 | 121 | | |
| |||
0 commit comments