Skip to main content
Extend the typed-agent tutorial with two output modes: one for an incomplete object preview and one for a sequence of validated objects. This tutorial uses the checked-in offline provider so it runs without API credentials.
15 minutesPartial<T>Iterable<T>

1. Run the complete example

From the source checkout:
The program first streams one person in two text fragments. It then collects a list of two people. Alongside the preview messages, expect:
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.
Use previews to update a loading UI. Apply actions that require a valid 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:
The accepted output is a 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 to 17 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.