Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Update how we look for finish_reason #9046

Merged
merged 6 commits into from
Mar 17, 2025
Merged

Conversation

sjrl
Copy link
Contributor

@sjrl sjrl commented Mar 17, 2025

Related Issues

  • fixes failing Mistral tests in core-integrations

Proposed Changes:

Changes how we look for the finish_reason when processing streaming chunks. OpenAI and Mistral put the finish_reason in different chunks so we just now look for the last one that is not None.

How did you test it?

Tested manually for mistral and ensured existing OpenAI tests still work.

Notes for the reviewer

Checklist

  • I have read the contributors guidelines and the code of conduct
  • I have updated the related issue with new insights and changes
  • I added unit tests and updated the docstrings
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I documented my code
  • I ran pre-commit hooks and fixed any issue

@sjrl sjrl requested a review from a team as a code owner March 17, 2025 08:42
@sjrl sjrl requested review from Amnah199 and removed request for a team March 17, 2025 08:42
@sjrl sjrl added the ignore-for-release-notes PRs with this flag won't be included in the release notes. label Mar 17, 2025
@coveralls
Copy link
Collaborator

coveralls commented Mar 17, 2025

Pull Request Test Coverage Report for Build 13898923105

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 89.966%

Totals Coverage Status
Change from base Build 13859234349: 0.04%
Covered Lines: 9728
Relevant Lines: 10813

💛 - Coveralls

@sjrl sjrl marked this pull request as draft March 17, 2025 08:49
@sjrl sjrl marked this pull request as ready for review March 17, 2025 08:55
@sjrl sjrl self-assigned this Mar 17, 2025
@github-actions github-actions bot added topic:tests type:documentation Improvements on the docs labels Mar 17, 2025
@sjrl sjrl requested a review from Amnah199 March 17, 2025 11:02
@sjrl
Copy link
Contributor Author

sjrl commented Mar 17, 2025

Hey @Amnah199 this turned into a slightly larger refactor to help simplify how we process streaming chunks and to add tests to make sure we capture the usage correctly when it's asked for. All of these changes still work with the Mistral Chat Generator

Comment on lines -578 to -582
def _convert_usage_chunk_to_streaming_chunk(self, chunk: ChatCompletionChunk) -> StreamingChunk:
"""
Converts the usage chunk received from the OpenAI API when `include_usage` is set to `True` to a StreamingChunk.

:param chunk: The usage chunk returned by the OpenAI API.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this function since we didn't actually use the processed usage data from this when constructing the final ChatMessage. See the updated _convert_streaming_chunks_to_chat_message where we actually use the native chunk from OpenAI to provide the usage data.

Copy link
Contributor

@Amnah199 Amnah199 Mar 17, 2025

Choose a reason for hiding this comment

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

Right, I'll run it locally for the original use case behind this PR for verification that this works.

Copy link
Contributor Author

@sjrl sjrl Mar 17, 2025

Choose a reason for hiding this comment

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

I've also added a unit test and an integration test testing for usage stats when streaming and include_usage: True so I hope that covers it, but let me know if you have any issues

"index": 0,
"finish_reason": finish_reason,
"completion_start_time": chunks[0].meta.get("received_at"), # first chunk received
"usage": chunk.usage or {},
"usage": dict(last_chunk.usage or {}), # last chunk has the final usage data if available
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated this to follow what we did in our normal chat completion processing where we store the dict version of the usage otherwise it's returned as an openai Python type

Copy link
Contributor

@Amnah199 Amnah199 left a comment

Choose a reason for hiding this comment

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

LG!

@sjrl sjrl merged commit 6f98cc2 into main Mar 17, 2025
15 checks passed
@sjrl sjrl deleted the openai-streaming-usage branch March 17, 2025 12:25
sjrl added a commit that referenced this pull request Mar 18, 2025
* Update how we look for finish_Reason

* Additional change

* Add unit test and integration test

* Refactor

* Use correct mock

* PR comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ignore-for-release-notes PRs with this flag won't be included in the release notes. topic:tests type:documentation Improvements on the docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants