Skip to content

Conversation

@majiayu000
Copy link
Contributor

Summary

  • Fixed usage info extraction in streaming responses to search all chunks instead of only the last one
  • This fixes issues with providers like Qwen3 that return usage in a different chunk than the final one

Test plan

  • Added test case for usage info not in last chunk
  • All 10 tests pass

Closes #10258

The previous implementation only looked at the last chunk for usage info,
which caused issues with some API providers (like Qwen3) that may return
usage in a different chunk than the final one.

Now we iterate through chunks in reverse order to find the last non-None
usage value, making usage extraction more robust across different providers.

Closes deepset-ai#10258
@majiayu000 majiayu000 requested a review from a team as a code owner December 18, 2025 09:35
@majiayu000 majiayu000 requested review from vblagoje and removed request for a team December 18, 2025 09:35
@vercel
Copy link

vercel bot commented Dec 18, 2025

@majiayu000 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant
Copy link

CLAassistant commented Dec 18, 2025

CLA assistant check
All committers have signed the CLA.

@vblagoje
Copy link
Member

Hey @majiayu000 this is actually a good fix, please read https://github.com/deepset-ai/haystack/blob/main/CONTRIBUTING.md and update your PR

@github-actions github-actions bot added the type:documentation Improvements on the docs label Dec 18, 2025
@majiayu000
Copy link
Contributor Author

Thanks for the feedback! I've added the release notes. Ready for review.

@vblagoje
Copy link
Member

Thanks for the feedback! I've added the release notes. Ready for review.

Nice, couple of formatting issues still - see the failures, you can reproduce them locally - all commands on CI are available locally as well

@coveralls
Copy link
Collaborator

coveralls commented Dec 18, 2025

Pull Request Test Coverage Report for Build 20404913201

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.003%) to 92.263%

Files with Coverage Reduction New Missed Lines %
components/generators/utils.py 3 88.46%
Totals Coverage Status
Change from base Build 20377983188: 0.003%
Covered Lines: 14191
Relevant Lines: 15381

💛 - Coveralls

usage = next((u for chunk in reversed(chunks) if (u := chunk.meta.get("usage")) is not None), None)

meta = {
"model": chunks[-1].meta.get("model"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does getting a model and received_at work for Qwen @majiayu000 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @vblagoje, I tested with Qwen3 (qwen/qwen3-235b-a22b) via OpenRouter:

  • model and received_at appear in every chunk, so getting them from chunks[-1] works correctly
  • usage only appears in the last chunk

The current implementation should work fine for these fields.

Note: This was tested via OpenRouter. If you have access to Qwen's native DashScope API, additional verification would be helpful as the original issue was reported with the native API.

@majiayu000 majiayu000 force-pushed the fix/issue-10258-qwen3-streaming-usage branch from cef261d to d0532cd Compare December 21, 2025 04:54
@vblagoje
Copy link
Member

Thanks @majiayu000 - I'll take a look today one last time and we'll move forward to integrate!

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

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The Qwen3 model cannot obtain the meta usage for streaming.

4 participants