Text-to-SQL
The SQL agent answers structured questions, such as counts and column lookups, by writing a query and running it against the database. This is one run of the agent:
Its PydanticAI agent is configured with the following arguments:
-
Structured output: the output is a function instead of a Pydantic model as in the other agents. This is a special use of tool calling that lets the agent query the database and finish in a single LLM call.
The usual way would be a regular tool plus a structured output model:
- The model calls the query tool.
- PydanticAI runs it, and the rows go back to the model.
- The model then produces the structured output, a
SqlOutcome, and has to copy the rows into it.
With an output function:
- The model calls the function, filling in its arguments as a Pydantic model
(
GeneratedSql, described below). - PydanticAI validates the arguments and runs the function.
- The function's return value, a
SqlOutcome, is the agent's final result and the run ends. The rows never go back through the model.
So there is one LLM call instead of two, the model cannot alter the rows, and it cannot finish without running a query.
-
GeneratedSql, the function's input: the Pydantic model the model fills in as the tool's arguments. The function's return value is a separate model,SqlOutcome: the query, the rows, and the reasoning.reasoning: the model's step-by-step plan for which tables and columns answer the question.sql: the query itself.- Field order matters:
reasoningcomes first, so the model plans before it writes the query. - Validation: a Pydantic validator on
sqlaccepts only a single read-only SELECT.
-
Dependencies: objects the agent's own code needs while it runs. They are handed to the agent when a run starts and are available to the function, but they are not part of the prompt. Here the dependency is the read-only database session, scoped to the current user. The other agents need no dependencies, because no code of ours runs during their runs: the model only returns its structured output. The session cannot be a global or part of the agent, because the agent is created once and shared by every request, while each request needs its own session. It cannot go in the prompt, because the model only handles text and must not control it.
-
Retries: two retries. PydanticAI sends an error back to the model, which rewrites the query, when the validator rejects it or the database raises an error. If it still fails, the turn falls back to retrieval (see Question Routing).
-
Database schema as a prompt variable: the model cannot see the database, so the system prompt includes the tables, columns, and enum values it may query. Without them it would have to guess column names. The schema text is generated from the SQLAlchemy models, with embedding columns hidden.
-
Three safety layers:
- Validation: the Pydantic check above.
- Read-only role: the query runs under a database role that can only read.
- Row-level security: the query runs in the user's security context, so even a query with no owner filter only sees that user's rows. See Row-level security.