How to Configure Pydantic AI Supabase Guardrails
You set up pydantic ai supabase guardrails by pairing agent schema checks with custom database roles. This dual setup checks inputs in your app and limits what queries can run. Many teams start by giving their AI agent the default database admin password. That broad role lets a bad prompt drop tables or read private user rows. A safer setup uses strict agent rules and a locked-down database user. How pydantic ai supabase guardrails work in practice Your guardrails work in two places at once.
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You set up pydantic ai supabase guardrails by pairing agent schema checks with custom database roles. This dual setup checks inputs in your app and limits what queries can run. Many teams start by giving their AI agent the default database admin password. That broad role lets a bad prompt drop tables or read private user rows. A safer setup uses strict agent rules and a locked-down database user.
How pydantic ai supabase guardrails work in practice
Your guardrails work in two places at once. The first part lives inside your Python agent code. You define strict data types for every tool call the agent can make. The agent framework checks the model output before any database query runs. If the model invents a bad value, the check fails right away. The framework then asks the model to fix its mistake and try again.
The second part lives inside your database project. Every new project comes with a default postgres role that has admin rights. It is not a full superuser, because the platform blocks destructive system tasks. Still, the project password set at creation belongs to this powerful admin role. You should never give that admin password to an outside service or agent. Instead, the platform recommends that you create a new database user for every service.
You can make a dedicated role just for your AI agent. You then grant this role read rights on only the tables it needs. You also add built-in roles like anon and authenticated into your row rules. These rules filter which rows the agent role can read during a run.
How to set up agent validation and role limits
Start by writing down the exact actions your agent needs to do. Never give the agent a tool that runs raw SQL strings from the model. Instead, build small tool functions for single tasks like fetching an order status. Use data models to check every input field before the function runs. For example, check that an order number is a real integer and not text. You can also add custom check functions to inspect tool inputs and outputs.
Next, log in to your database dashboard to set up the database role. Remember that queries run in the dashboard run as the default postgres admin role. Use the dashboard to create a fresh database user for your AI app. Grant this user permission to read only the specific tables for those tools. Leave out write, update, and delete rights unless a tool truly needs them. Put this new user password into your agent app settings instead of the admin key.
AI Guardrails: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
What native roles solve and what keeps the session active
Native roles and schema checks solve the problem of broad database permissions. They stop an agent from running admin commands or calling tools with broken inputs. Your custom database user blocks the agent from touching tables outside its job.
Still, static database roles only decide who can log in at the start. They do not inspect what each query does while it runs in flight. If an agent has read rights on a customer table, it reads every column. Native role setup does not rewrite sensitive fields like emails or phone numbers in results. It also does not pause a risky tool call to ask a human first. Once the agent logs in, every allowed query runs at full speed.
Add query and result controls beside Postgres
Grants and pools govern who logs into PostgreSQL. Yet every query in an open session still runs with that role's full scope. In practice, hoop.dev runs beside your database as a sidecar for your Postgres. It reads plain and prepared queries on the wire with no change to your users or drivers.
When a tool or agent sends SQL, hoop.dev Postgres controls check each statement against your deny rules. An AI Analyzer you configure can also score intent or hold a risky query for human review. Matched sensitive columns in result rows are masked in memory before they reach the caller. Each query and decision is logged per listener, which generates evidence for SOC 2, HIPAA, and GDPR audits. View the binary on GitHub.
Frequently asked questions
Should I give my AI agent the default postgres role password?
No, you should never share the default postgres password with an agent or app. That role has admin rights, so you should make a separate user per service.
Can Pydantic AI stop SQL injection on its own?
It checks that tool inputs match your data types before the function runs. You still need parameterized queries and strict database roles to block bad SQL.
How do I limit which rows an AI agent can read?
Create a custom database role for the agent and turn on row rules. Write policies that limit that role to the specific rows it needs to read.
Related guides
In this post