15 minutesPartial<T>Iterable<T>
1. Run the complete example
From the source checkout:Complete runnable streaming example
Complete runnable streaming example
crates/rydantic-ai/examples/structured.rs
2. Display an object preview
stream_partial starts a bounded background task and returns an output-event receiver. Delta events contain the current partial JSON snapshot. Fields can be missing until more input arrives.
Person after Complete: only that event contains the accepted, fully validated result. Propagate terminal errors rather than keeping an old preview as a result.
3. Collect a validated list
stream_iterable expects an object containing an items array. In the complete source, the demo provider produces this envelope:
Vec<Person>. To render each item as it arrives, consume OutputEvent::Delta and read value.index and value.value instead. Each item has passed its model’s validation, while the attempt still awaits final envelope validation.
4. Handle attempts and cancellation
Validation correction and tool continuation can reset an attempt.
collect() automatically clears reset attempts and returns the accepted result. Call it on a fresh stream before manually consuming events; consumed items cannot be recovered.
Dropping a stream cancels its background task. You can also call stream.cancel(). Use RunContext::with_timeout when the entire operation has a deadline.
Try a change
Change Grace’s age to17 in the demo response. The list cannot complete successfully because the Person model requires age >= 18. Observe the reset/error behavior, then restore 40 and run it again.
For a live provider, keep the same consumer and replace the demo provider with an HTTP provider from the previous tutorial. The streaming reference covers backpressure, resource limits and normalized provider events.
Browse runnable examples
Choose a focused recipe for the next integration.
Add images and PDFs
Build multimodal prompts with typed results.