Examples of how to use PydanticAI and what it can do.
Usage
These examples are distributed with pydantic-ai so you can run them either by cloning the pydantic-ai repo or by simply installing pydantic-ai from PyPI with pip or uv.
Installing required dependencies
Either way you'll need to install extra dependencies to run some examples, you just need to install the examples optional dependency group.
If you've installed pydantic-ai via pip/uv, you can install the extra dependencies with:
pipinstall"pydantic-ai[examples]"
uvadd"pydantic-ai[examples]"
If you clone the repo, you should instead use uv sync --extra examples to install extra dependencies.
Setting model environment variables
These examples will need you to set up authentication with one or more of the LLMs, see the model configuration docs for details on how to do this.
TL;DR: in most cases you'll need to set one of the following environment variables:
exportOPENAI_API_KEY=your-api-key
exportGEMINI_API_KEY=your-api-key
Running Examples
To run the examples (this will work whether you installed pydantic_ai, or cloned the repo), run: