9201ef759e
Harness Compat / harness compat (push) Failing after 0s
CI / test on 3.12 (standard) (push) Has been cancelled
CI / test on 3.13 (standard) (push) Has been cancelled
CI / test on 3.14 (standard) (push) Has been cancelled
CI / test on 3.10 (all-extras) (push) Has been cancelled
CI / test on 3.11 (all-extras) (push) Has been cancelled
CI / test on 3.12 (all-extras) (push) Has been cancelled
CI / test on 3.14 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.10 (pydantic-evals) (push) Has been cancelled
CI / test on 3.11 (pydantic-evals) (push) Has been cancelled
CI / test on 3.12 (pydantic-evals) (push) Has been cancelled
CI / deploy-docs-preview (push) Has been cancelled
CI / build release artifacts (push) Has been cancelled
CI / publish to PyPI (push) Has been cancelled
CI / Send tweet (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / mypy (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / test on 3.10 (standard) (push) Has been cancelled
CI / test on 3.11 (standard) (push) Has been cancelled
CI / test on 3.13 (all-extras) (push) Has been cancelled
CI / test on 3.14 (all-extras) (push) Has been cancelled
CI / test on 3.10 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.11 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.12 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.13 (pydantic-ai-slim) (push) Has been cancelled
CI / test on 3.13 (pydantic-evals) (push) Has been cancelled
CI / test on 3.14 (pydantic-evals) (push) Has been cancelled
CI / test on 3.10 (lowest-versions) (push) Has been cancelled
CI / test on 3.11 (lowest-versions) (push) Has been cancelled
CI / test on 3.12 (lowest-versions) (push) Has been cancelled
CI / test on 3.13 (lowest-versions) (push) Has been cancelled
CI / test on 3.14 (lowest-versions) (push) Has been cancelled
CI / test examples on 3.11 (push) Has been cancelled
CI / test examples on 3.12 (push) Has been cancelled
CI / test examples on 3.13 (push) Has been cancelled
CI / test examples on 3.14 (push) Has been cancelled
CI / coverage (push) Has been cancelled
CI / check (push) Has been cancelled
CI / deploy-docs (push) Has been cancelled
68 lines
1.9 KiB
Markdown
68 lines
1.9 KiB
Markdown
# Examples
|
|
|
|
Here we include some examples of how to use Pydantic AI 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](https://github.com/pydantic/pydantic-ai) 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:
|
|
|
|
```bash
|
|
pip/uv-add "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](../models/overview.md) docs for details on how to do this.
|
|
|
|
TL;DR: in most cases you'll need to set one of the following environment variables:
|
|
|
|
=== "OpenAI"
|
|
|
|
```bash
|
|
export OPENAI_API_KEY=your-api-key
|
|
```
|
|
|
|
=== "Google Gemini"
|
|
|
|
```bash
|
|
export GEMINI_API_KEY=your-api-key
|
|
```
|
|
|
|
### Running Examples
|
|
|
|
To run the examples (this will work whether you installed `pydantic_ai`, or cloned the repo), run:
|
|
|
|
```bash
|
|
python/uv-run -m pydantic_ai_examples.<example_module_name>
|
|
```
|
|
|
|
For example, to run the very simple [`pydantic_model`](./pydantic-model.md) example:
|
|
|
|
```bash
|
|
python/uv-run -m pydantic_ai_examples.pydantic_model
|
|
```
|
|
|
|
If you like one-liners and you're using uv, you can run a pydantic-ai example with zero setup:
|
|
|
|
```bash
|
|
OPENAI_API_KEY='your-api-key' \
|
|
uv run --with "pydantic-ai[examples]" \
|
|
-m pydantic_ai_examples.pydantic_model
|
|
```
|
|
|
|
---
|
|
|
|
You'll probably want to edit examples in addition to just running them. You can copy the examples to a new directory with:
|
|
|
|
```bash
|
|
python/uv-run -m pydantic_ai_examples --copy-to examples/
|
|
```
|