Skip to content

iterative streaming #241

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

pgrayy
Copy link
Member

@pgrayy pgrayy commented Jun 18, 2025

Description

We are currently working on support for an iterative async stream method on the agent class (#83). As part of this work, we need to yield underlying events of the model stream. This PR thus converts the stream_messages function to a generator.

Related Issues

#83

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Other (please describe):

Testing

  • hatch fmt --linter
  • hatch fmt --formatter
  • hatch test --all
  • Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
  • hatch run test-lint

Checklist

  • I have read the CONTRIBUTING document
  • I have added tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

for event in stream_messages(model, system_prompt, messages, tool_config):
if "callback" in event:
inputs = {**event["callback"], **(kwargs if "delta" in event["callback"] else {})}
callback_handler(**inputs)
Copy link
Member Author

@pgrayy pgrayy Jun 18, 2025

Choose a reason for hiding this comment

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

@zastrowm had the following comment:

I presume that test_event_loop has enough UT coverage verifying the callbacks of events are as expected? At which point we have a lot of confidence that we maintained backwards compatibility.

This was a good call out. We didn't actually have the best coverage on callbacks. I added a test that actually caught an issue with passing kwargs which we need for backwards compatibility. I personally don't think passing kwargs is useful and so we should revisit when we formally type events (tracking issue).

inputs = {**event["callback"], **(kwargs if "delta" in event["callback"] else {})}
callback_handler(**inputs)
else:
stop_reason, message, usage, metrics = event["stop"]
Copy link
Member Author

Choose a reason for hiding this comment

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

@zastrowm had the following comment:

So stop right now is not an event that callback gets passed. Given that we don't need backwards compability here, what would you think about turning this into a typed event? Could also be a follow-up PR

Will handle in a follow up PR as part of #242

if "toolUse" in delta_content:
if "input" not in state["current_tool_use"]:
state["current_tool_use"]["input"] = ""

state["current_tool_use"]["input"] += delta_content["toolUse"]["input"]
callback_handler(delta=delta_content, current_tool_use=state["current_tool_use"], **kwargs)
callback_event["callback"] = {"delta": delta_content, "current_tool_use": state["current_tool_use"]}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a temporary format to maintain backwards compatibility. We are planning on formalizing and strongly typing the construction of our agent events for consistency and ease of use.

#242

call(event={"contentBlockStop": {}}),
call(event={"contentBlockStart": {"start": {}}}),
call(event={"contentBlockDelta": {"delta": {"text": "value"}}}),
call(
Copy link
Member Author

Choose a reason for hiding this comment

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

There was a comment about duplicate events yielded while streaming. This is an example (813-814) and will be addressed as part of #242.

@pgrayy pgrayy requested review from zastrowm and Unshure June 18, 2025 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant