Skip to content

fix(mistral): preserve extended usage fields from API response#5693

Open
felipebridge wants to merge 1 commit into
pydantic:mainfrom
felipebridge:fix/mistral-usage-details
Open

fix(mistral): preserve extended usage fields from API response#5693
felipebridge wants to merge 1 commit into
pydantic:mainfrom
felipebridge:fix/mistral-usage-details

Conversation

@felipebridge
Copy link
Copy Markdown

_map_usage only extracted prompt_tokens and completion_tokens, silently dropping any additional fields the Mistral API returns (e.g. cached_tokens, reasoning_tokens). It also had a latent bug: if response.usage: evaluates truthiness on a Pydantic model instance, which is always True even when all token counts are zero.

This matches the pattern used by the Groq provider: call model_dump(exclude_none=True) to capture all fields, filter the three known token keys, and forward any remaining integer fields to RequestUsage.details.

Fixes #5669

The previous implementation only extracted prompt_tokens and
completion_tokens, silently discarding any additional fields the Mistral
API returns (e.g. cached_tokens, reasoning_tokens). It also used
`if response.usage:` which evaluates truthiness on a model instance —
always True even when all counts are zero.

Use model_dump(exclude_none=True) to capture all fields, filter known
token keys, and store the remainder in RequestUsage.details, matching
the pattern used by the Groq provider.

Fixes pydantic#5669
@github-actions github-actions Bot added size: S Small PR (≤100 weighted lines) bug Report that something isn't working, or PR implementing a fix labels May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Report that something isn't working, or PR implementing a fix size: S Small PR (≤100 weighted lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[provider-parity-explore] Provider parity: Usage accounting — Mistral silently drops all usage details beyond token counts

1 participant