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
39 lines
1.1 KiB
Markdown
39 lines
1.1 KiB
Markdown
# SQL Generation
|
|
|
|
Example demonstrating how to use Pydantic AI to generate SQL queries based on user input.
|
|
|
|
Demonstrates:
|
|
|
|
- [dynamic system prompt](../agent.md#system-prompts)
|
|
- [structured `output_type`](../output.md#structured-output)
|
|
- [output validation](../output.md#output-validator-functions)
|
|
- [agent dependencies](../dependencies.md)
|
|
|
|
## Running the Example
|
|
|
|
The resulting SQL is validated by running it as an `EXPLAIN` query on PostgreSQL. To run the example, you first need to run PostgreSQL, e.g. via Docker:
|
|
|
|
```bash
|
|
docker run --rm -e POSTGRES_PASSWORD=postgres -p 54320:5432 postgres
|
|
```
|
|
|
|
_(we run postgres on port `54320` to avoid conflicts with any other postgres instances you may have running)_
|
|
|
|
With [dependencies installed and environment variables set](./setup.md#usage), run:
|
|
|
|
```bash
|
|
python/uv-run -m pydantic_ai_examples.sql_gen
|
|
```
|
|
|
|
or to use a custom prompt:
|
|
|
|
```bash
|
|
python/uv-run -m pydantic_ai_examples.sql_gen "find me errors"
|
|
```
|
|
|
|
This model uses `gemini-3-flash-preview` by default since Gemini is good at single shot queries of this kind.
|
|
|
|
## Example Code
|
|
|
|
```snippet {path="/examples/pydantic_ai_examples/sql_gen.py"}```
|