15 minutesOffline providerValidated final output
1. Set up the project
Cargo.toml
Model through rydantic_ai::rydantic to use the same core model version as the agent. The offline example needs no HTTP transport.
2. Define the result you will accept
Triage contains a Department enum, a boolean urgent flag and a confidence between 0.0 and 1.0. Enum variants serialize to billing or technical; unknown result fields are rejected.
The agent’s type is Agent<(), Triage>. The first parameter is application dependency data; () means this example has none. The second is the model the final output must satisfy.
3. Add the local provider
Copy the complete source intosrc/main.rs. It is also available at crates/rydantic-ai/examples/typed_agent.rs.
crates/rydantic-ai/examples/typed_agent.rs
DemoProvider returns fixed example responses and simulated token counts. It performs no inference. Its first response uses confidence 1.2, which violates the model. The second uses 0.9.
4. Observe validation correction
max_validation_retries: 1 allows one correction, and max_model_calls: 2 caps the total calls. Usage includes both completed responses.
Change the provider to always return 1.2. The run terminates with an output-validation error after the retry budget is used. Restore the valid second response to continue.
5. Connect a real API
Enable the default HTTP feature by changing the dependency torydantic-ai = "=0.6.0". Keep Triage, and replace the demo provider and model name with your configured provider:
BASEURL, MODEL and optional APIKEY in your local environment. Remove the demo’s fixed-output and fixed-usage assertions when using a real model. Model capability and credentials determine whether that run succeeds.
Choose Api::Responses or Api::AnthropicMessages for the other supported protocols. The provider guide explains URL prefixes, authentication and schema capabilities.
Next: stream structured results
Add partial previews and validated list output.
Tools, graphs and multimodal inputs
Run focused agent examples.