fix(mistral): report usage metrics in streaming mode#1697
Open
jackatorcflo wants to merge 1 commit intostrands-agents:mainfrom
Open
fix(mistral): report usage metrics in streaming mode#1697jackatorcflo wants to merge 1 commit intostrands-agents:mainfrom
jackatorcflo wants to merge 1 commit intostrands-agents:mainfrom
Conversation
|
Assessment: Approve This is a clean, well-documented bug fix that correctly addresses the Mistral SDK v1+ API structure change for accessing usage data in streaming mode. Review Summary
Good job on the thorough PR description with external documentation references! 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixed a bug where Mistral models were not reporting usage metrics (prompt tokens, completion tokens, total tokens) when used in streaming mode.
The root cause was that the streaming response handler checked for usage data at
chunk.usage(top-level), but Mistral's v1+ SDK returns all streaming data nested under adataproperty. According to the Mistral Python SDK migration guide, streaming chunks are accessed viachunk.data.choices[0].delta.contentwith the additional.datawrapper layer. This same nesting applies to theusagefield, making the correct pathchunk.data.usage.This is confirmed by the Langfuse Mistral integration example, which shows the correct access pattern:
This bug manifested silently; Mistral model streaming worked, but we noticed that we never received token data for streaming Mistral models.
Related Issues
Documentation PR
Type of Change
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
I manually tested this before and after:
I updated the Mistral tests to mock the usage at the correct level (under data).
I ran
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.