Skip to content

feat: Update streaming chunk #9424

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

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft

feat: Update streaming chunk #9424

wants to merge 11 commits into from

Conversation

sjrl
Copy link
Contributor

@sjrl sjrl commented May 22, 2025

Related Issues

Proposed Changes:

Updated StreamingChunk to add the fields tool_call, tool_call_result, index, and start to make it easier to format the stream in a streaming callback.

  • Added new dataclass ToolCallDelta for the StreamingChunk.tool_call field to reflect that the arguments are a string delta rather than a Dict
  • Updated print_streaming_chunk utility method to use these new fields. This especially improves the formatting when using this with Agent.
  • Updated OpenAIChatGenerator and HuggingFaceAPIChatGenerator to follow the new dataclass.
  • Updated ToolInvoker to follow the new format and also it now returns a final StreamingChunk that contains finish_reason="tool_call_result" in its metadata.

How did you test it?

  • TODO add more unit tests

  • Tested Agent streaming locally with OpenAIChatGenerator

Before Changes
Screenshot 2025-05-22 at 13 35 23

After Changes

  • Notable differences
    • Possible to add the [ASSISTANT] header
    • Easier to have better spacing between sections (e.g. between two tool calls)
Screenshot 2025-05-22 at 13 34 11

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

@github-actions github-actions bot added topic:tests type:documentation Improvements on the docs labels May 22, 2025
sjrl added 2 commits May 22, 2025 12:13
…date HuggingFaceAPIChatGenerator to start following new StreamingChunk
Comment on lines +50 to +51
tool_call: Optional[ToolCallDelta] = None
tool_call_result: Optional[ToolCallResult] = None
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 thought about making

content = Union[str, ToolCallDelta, ToolCallResult]

but this would be a breaking change b/c users expect content to always be a string. And this breaks StreamingChunk to ChatMessage implementations (mostly in private methods).

@sjrl sjrl requested a review from tstadel May 22, 2025 11:44
@coveralls
Copy link
Collaborator

coveralls commented May 22, 2025

Pull Request Test Coverage Report for Build 15191185377

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.
  • 63 unchanged lines in 9 files lost coverage.
  • Overall coverage decreased (-0.07%) to 90.342%

Files with Coverage Reduction New Missed Lines %
components/builders/answer_builder.py 1 98.53%
dataclasses/init.py 1 46.15%
core/super_component/super_component.py 4 95.83%
components/generators/chat/hugging_face_api.py 6 93.88%
dataclasses/streaming_chunk.py 6 84.62%
components/generators/chat/openai.py 9 95.36%
components/tools/tool_invoker.py 9 83.67%
components/generators/utils.py 11 10.53%
core/super_component/utils.py 16 86.61%
Totals Coverage Status
Change from base Build 15182795258: -0.07%
Covered Lines: 11169
Relevant Lines: 12363

💛 - Coveralls

@tstadel
Copy link
Member

tstadel commented May 23, 2025

Looks very good to me already. 👍

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.

Extend StreamingChunk to have tool_calls, tool_results, finish_reason, usage
3 participants