Skip to content

refactor: Update to StreamingChunk, better index setting and change tool_call to tool_calls#9525

Merged
sjrl merged 13 commits intomainfrom
update-to-chat-completion-chunk-conversion
Jun 23, 2025
Merged

refactor: Update to StreamingChunk, better index setting and change tool_call to tool_calls#9525
sjrl merged 13 commits intomainfrom
update-to-chat-completion-chunk-conversion

Conversation

@sjrl
Copy link
Copy Markdown
Contributor

@sjrl sjrl commented Jun 17, 2025

Related Issues

Proposed Changes:

  • Update _convert_chat_completion_chunk_to_streaming_chunk to properly set index=None in the case for streaming chunks that only contain role information or have their content set to None (e.g. when providing finish_reason). This was the original intent of StreamingChunk.index, but was missed in the original PR.
  • Update StreamingChunk to have a tool_calls field instead of a singular tool_call. So tool_calls becomes Optional[List[ToolCallDelta]] to better reflect that a OpenAI Chunk can have multiple tool calls in it. I was able to test this behavior for the first time when using the MistralChatGenerator.
    • Updated relevant parts of the code and tests to reflect this change.
    • Updated the original release note to reflect this change as well.
    • Updated the print_streaming_callback function to accommodate this change

How did you test it?

  • Updated and added unit tests.

Notes for the reviewer

Example of OpenAI Agent Streaming Formatting
Screenshot 2025-06-18 at 10 52 56

Example of Mistral Agent Streaming Formatting
Screenshot 2025-06-18 at 11 05 32

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 added 2 commits June 17, 2025 10:29
…enAIChatGenerator so we can overwrite it in integrations that inherit from it
@sjrl sjrl requested a review from a team as a code owner June 17, 2025 08:41
@sjrl sjrl requested review from anakin87 and julian-risch and removed request for a team June 17, 2025 08:41
@sjrl
Copy link
Copy Markdown
Contributor Author

sjrl commented Jun 17, 2025

@anakin87 I'm setting @julian-risch as the reviewer since he reviewed the original PR that introduced the new fields to StreamingChunk so he has the most context for these changes.

@sjrl sjrl added the ignore-for-release-notes PRs with this flag won't be included in the release notes. label Jun 17, 2025
@sjrl
Copy link
Copy Markdown
Contributor Author

sjrl commented Jun 17, 2025

Sorry @julian-risch and @anakin87 I'm switching this to draft for now. I've run into some challenges with Mistral so I'm considering rethinking how StreamingChunk works with respect to handling multiple tool calls.

@sjrl sjrl marked this pull request as draft June 17, 2025 08:53
@sjrl sjrl removed the request for review from anakin87 June 17, 2025 08:54
@github-actions github-actions Bot added the type:documentation Improvements on the docs label Jun 17, 2025
@coveralls
Copy link
Copy Markdown
Collaborator

coveralls commented Jun 17, 2025

Pull Request Test Coverage Report for Build 15818567920

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 22 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.02%) to 90.157%

Files with Coverage Reduction New Missed Lines %
tracing/datadog.py 2 96.0%
components/generators/chat/openai.py 3 96.36%
core/pipeline/async_pipeline.py 3 67.74%
dataclasses/streaming_chunk.py 5 91.23%
components/generators/utils.py 9 75.0%
Totals Coverage Status
Change from base Build 15728526114: -0.02%
Covered Lines: 11569
Relevant Lines: 12832

💛 - Coveralls

@sjrl sjrl marked this pull request as ready for review June 18, 2025 09:06
@sjrl sjrl requested a review from a team as a code owner June 18, 2025 09:06
@sjrl sjrl requested review from dfokina and removed request for a team June 18, 2025 09:06
@sjrl sjrl changed the title refactor: Make _convert_chat_completion_chunk_to_streaming_chunk a member function of OpenAIChatGenerator and small fixes to setting StreamingChunk.index refactor: Update to StreamingChunk, better index setting and change tool_call to tool_calls Jun 18, 2025
Copy link
Copy Markdown
Member

@julian-risch julian-risch left a comment

Choose a reason for hiding this comment

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

Looks good to me! 👍 I didn't find anything that needs to be changed. For the three consecutive print lines, I suggest to combine them but I don't want to block the PR because of that simplification.

Comment thread haystack/dataclasses/streaming_chunk.py
Comment thread haystack/components/generators/utils.py Outdated
Comment thread haystack/components/generators/utils.py Outdated
Comment thread haystack/components/generators/utils.py Outdated
@sjrl sjrl enabled auto-merge (squash) June 23, 2025 08:03
@sjrl sjrl merged commit ec37138 into main Jun 23, 2025
19 checks passed
@sjrl sjrl deleted the update-to-chat-completion-chunk-conversion branch June 23, 2025 08:14
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.

Check if the util method _convert_chat_completion_chunk_to_streaming_chunk works as expected for integrations that inherit OpenAIChatGenerator

3 participants