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

Add an API for streaming tool calls performed by HandleResponseNode #831

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

Conversation

dmontagu
Copy link
Contributor

This adds a run_stream method to HandleResponseNode in the Agent graph. That method is an AsyncContextManager yielding a stream (the pattern we use to ensure async-generator-wrapping spans get closed), and that stream yields events with information about tool calls being performed by the agent.

(We can eventually add other events if desired, including possibly a way for users to emit custom events in their tools through a method on the GraphContext, but I think this is more than enough to start with.)

Once we expose a way to call the Agent graph's next method, I believe this will be usable as a way to handle full agent event streaming — you'll use next to transition between nodes, and nodes with interesting events will have a run_stream (or similar) method for streaming those events.

I'm okay not merging this until we have a more fully-baked implementation of agent streaming (which I'll work on now), but I'm opening this PR now to get some early feedback.


I'll note that if we want to provide a way to inject control over the operation of the agent as part of the streaming (e.g., being able to see the list of tool calls and programmatically modify it before they are executed), instead of just providing a stream of events we could make use of a nested graph and expose its next call to the user. But I don't currently see a compelling use case for that (at least for pydantic_ai.Agent; I could imagine it being useful in more sophisticated graph-based applications), and I don't think it's worth the extra complexity.

@github-actions github-actions bot temporarily deployed to deploy-preview January 31, 2025 23:16 Inactive
@dmontagu dmontagu force-pushed the dmontagu/stream-tool-calls branch from 1443b49 to c4e9180 Compare February 1, 2025 01:09
@github-actions github-actions bot temporarily deployed to deploy-preview February 1, 2025 01:13 Inactive
@dmontagu
Copy link
Contributor Author

dmontagu commented Feb 1, 2025

@samuelcolvin I think we should discuss the ergonomics of next next week; I opened #833 as a follow-on to try to reduce the verbosity/cruftiness of the APIs, but I think that's following a pattern I wanted to before that you objected to. But I still think we should try to improve the streaming APIs.

@dmontagu
Copy link
Contributor Author

dmontagu commented Feb 6, 2025

Closing in favor of an approach based on #833, though I need to check if this has relevant implementation

@dmontagu dmontagu closed this Feb 6, 2025
@dmontagu
Copy link
Contributor Author

dmontagu commented Feb 6, 2025

Actually 833 was based on this, so I guess we should merge it first

@dmontagu dmontagu reopened this Feb 6, 2025
@dmontagu dmontagu marked this pull request as ready for review February 6, 2025 22:48
@github-actions github-actions bot temporarily deployed to deploy-preview February 6, 2025 22:53 Inactive
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