How to Configure Pydantic AI MariaDB Guardrails

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up pydantic ai mariadb guardrails by combining strict Python type schemas with scoped database roles. These rules check every model tool call before the query reaches your database engine. Many teams start by giving an AI agent a broad database account for testing. If that account stays in production, one bad prompt can read or change live tables. How pydantic ai mariadb guardrails work in practice Pydantic AI lets developers build agents that call structured Python functions as tools

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 mariadb guardrails by combining strict Python type schemas with scoped database roles. These rules check every model tool call before the query reaches your database engine. Many teams start by giving an AI agent a broad database account for testing. If that account stays in production, one bad prompt can read or change live tables.

How pydantic ai mariadb guardrails work in practice

Pydantic AI lets developers build agents that call structured Python functions as tools. Each tool uses a schema to check inputs before the function runs any logic. You define the exact data types and value limits that the model must supply. If the language model sends the wrong format, the framework rejects the call right away. It then asks the model to fix the input and try the tool call again.

Inside the tool function, the app opens a session to your MariaDB server. Native database rules rely on user accounts, host limits, and table grants. You create a dedicated database role for the agent with read-only permissions on specific tables. You can also restrict access to certain columns or create filtered SQL views. This setup stops the agent from seeing tables outside its assigned scope. Your team can also set resource limits on the database account to cap query time.

What native checks solve and what they miss

Combining Python input checks with MariaDB grants stops many common mistakes. The framework blocks malformed inputs, and the database blocks unauthorized table changes. If an agent tries to drop a table using a read-only role, MariaDB denies it. This basic layer is essential for any team running automated agents against relational data.

Still, native roles only check who logs in and which tables they can read. They do not inspect what individual SQL statements do while in flight. When an agent has a free-form SQL tool, schema checks only verify that the query is text. A valid read query can still pull thousands of rows of customer data at once. If a table holds emails, phone numbers, or card numbers, a select query returns them in full. Native MariaDB roles return the whole column or block the query completely. Also, static database keys stored in app settings are hard to rotate and audit per request.

AI Guardrails: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Managing sensitive data and free-form queries

Teams often struggle when agents need to run custom queries across shared tables. Creating a new SQL view for every single agent task takes too much engineering time. Developers end up granting read permissions on the base tables to keep projects moving forward. Once the agent has base table access, every prompt can pull sensitive columns into model memory.

To reduce risk, teams write custom regex filters inside their Python tool functions. Writing SQL parsers in app code is brittle and easy for a model to bypass. A small change in query syntax can slip past a simple text check. Teams need a way to inspect queries and mask fields like national IDs in results.

Check MySQL queries and results on the wire

MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL. It runs next to MySQL, RDS, or Aurora and reads the MySQL wire protocol.

Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails. When a rule refuses a query, MySQL returns a native error packet with your message. On the response path, hoop.dev data masking rewrites string values in returned rows that match your rules. Every query and decision is logged per resource. See the MIT-licensed project on GitHub.

Frequently asked questions

Can Pydantic AI stop SQL injection on its own?

Pydantic AI validates input types, which helps when you pass values into parameterized queries. If your tool lets the model write raw SQL strings, type checks alone will not stop unsafe queries.

How should we restrict MariaDB accounts for AI agents?

Create a separate MariaDB user for each agent and grant only select permissions on required tables. Set query timeout limits on that user so heavy joins cannot slow down your database server.

How does data masking help with AI database tools?

Data masking rewrites matched fields like emails, phone numbers, or card numbers in query results. The agent gets the row structure it needs to answer questions without receiving raw sensitive values.

The query runs. The emails stay hidden.