How to Configure Pydantic AI SQL Server Guardrails
You set up pydantic ai sql server guardrails by combining Python rules with database roles. Your agent checks inputs and outputs while SQL Server restricts what the account can read. Many teams start by giving their AI agent a broad database account for testing. If that account has owner permissions, a bad prompt can alter tables or drop the database. How pydantic ai sql server guardrails work in native setups A safe setup starts inside the database engine. In SQL Server, a database user is t
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 sql server guardrails by combining Python rules with database roles. Your agent checks inputs and outputs while SQL Server restricts what the account can read. Many teams start by giving their AI agent a broad database account for testing. If that account has owner permissions, a bad prompt can alter tables or drop the database.
How pydantic ai sql server guardrails work in native setups
A safe setup starts inside the database engine. In SQL Server, a database user is the identity of a login when it connects. Admins have thirteen types of database users to pick from when they build accounts. If an agent needs many databases on an instance, teams create a login in master. If the agent only needs one database, a contained database user is an option. This contained user has a password and does not tie to a login in master. An admin must enable contained databases on the server and the database first.
Next, the team sets up database roles to limit what the agent can do. Every role, including the public role, has permission to connect to the database. You cannot change the permissions assigned to fixed database roles in SQL Server. Many teams put their AI agent into the fixed reader role. Members of this role can read all data from all user tables and views. To block changes, teams also add the agent to the deny writer role. That role stops the user from adding, changing, or deleting any table data. If you want to hide all user tables and views, the deny reader role blocks them. Meanwhile, the writer role lets a user add, delete, or change data across user tables. Teams must avoid the owner role for AI agents. An owner can run all maintenance tasks and even drop the database in SQL Server.
On the Python side, Pydantic AI validates the data structures that move through the agent. Developers define schemas to check the arguments that the model passes into database tools. They also write validation checks that inspect the model output before the app returns it. Together, these app checks and database roles form your basic safety net.
What native roles solve and what they leave open
Native SQL Server roles and Python schemas solve the problem of broad account permissions. They decide who can log in and whether that user can change table data. Still, a fixed reader role still lets the agent read every user table and view. It does not inspect individual SQL statements while they run in flight.
AI Guardrails + Kubernetes API Server Access: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
When an AI agent writes its own SQL queries, role checks are not enough. A read-only agent can still pull sensitive columns from customer tables during a session. It can fetch emails, phone numbers, card numbers, or national IDs in plain text. Python schema rules can check data shapes, but the agent already receives the raw rows. Also, native database roles do not require human approval when an agent runs a risky query. Teams need a way to inspect each query and rewrite matched fields in results.
Check SQL Server batches and results on the wire
SQL Server logins and roles decide who connects. They do not check what each TDS batch does or mask sensitive values in returned rows. With hoop.dev, you run an open-source proxy, a sidecar for your SQL Server. It reads plain batches and parameterized queries on the TDS wire while passing Windows login through untouched.
Before a batch hits the server, hoop.dev guardrails check the statement against your rules. Disallowed actions get a native TDS error with your message. On the way back, hoop.dev data masking rewrites sensitive values in supported result columns that match your rules. Every query and decision is logged. Explore the MIT-licensed project on GitHub.
Frequently asked questions
Can I change the permissions of fixed roles in SQL Server?
No, SQL Server does not let you change the permissions of fixed database roles. If the fixed reader role is too broad, you must create custom roles instead.
Should an AI agent ever use the database owner role?
No, you should never give an AI agent the database owner role. That role allows full maintenance actions and lets the user drop the entire database.
Do Python schema checks stop an agent from reading sensitive columns?
No, Python checks only validate data after the database runs the query and returns rows. To protect emails or card numbers in flight, you must filter results before they reach the agent.
Related guides
In this post