How to Configure Pydantic AI MySQL Sandbox

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a pydantic ai mysql sandbox by linking an agent tool to a test database. You give the agent a restricted MySQL role with read or write rules. Most teams build this setup by cloning production schemas into an isolated database instance. They then pass a static database user and key to the agent runtime. If that role is too broad, a bad prompt can alter tables or leak private rows. How a pydantic ai mysql sandbox works Pydantic AI uses type hints to check the inputs and outputs of la

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 a pydantic ai mysql sandbox by linking an agent tool to a test database. You give the agent a restricted MySQL role with read or write rules. Most teams build this setup by cloning production schemas into an isolated database instance. They then pass a static database user and key to the agent runtime. If that role is too broad, a bad prompt can alter tables or leak private rows.

How a pydantic ai mysql sandbox works

Pydantic AI uses type hints to check the inputs and outputs of language model tools. You define a dependency object that holds your database driver and session state. When the model decides to fetch data, it calls your custom query tool. The framework passes the dependency object into that tool function at runtime. The tool opens a session with the MySQL sandbox and runs the SQL statement.

On the database side, the MySQL sandbox runs as a separate instance or container. Admins create a dedicated database user just for the AI agent. They grant this user permissions on specific test schemas and tables. This native setup keeps the agent away from live production tables. It also lets developers test how the model writes SQL against real table structures.

Type validation checks that the tool receives a string or structured object from the model. If the model sends the wrong data type, the framework asks the model to try again. Once the types match, the Python function sends the raw SQL text to MySQL. MySQL checks its internal grant tables to see if the user can run that command. The database then returns the matching rows back to the agent tool.

What native sandbox permissions solve and miss

Native MySQL roles do a good job of blocking access to whole tables. You can set the sandbox user to read-only so it never drops a table. You can also limit the user to one schema inside the test server. These controls decide who can log in and which objects they can touch.

Still, static database roles cannot check what individual queries do in flight. A read-only sandbox user can still run a huge join that locks the test server. Also, test databases often hold real customer data copied from production. If a table has emails, phone numbers, or card numbers, a select query pulls them all. Data masking rewrites matched fields in results so raw emails or card numbers do not flow into prompts.

AI Sandbox Environments + MySQL Access Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Static keys in app files create another problem for engineering teams. Developers often share one sandbox password across many local machines and test runners. When five agents use the same MySQL user, the database log shows one name. You cannot tell which agent or developer ran a slow or broken query. Teams need a way to check every statement and tie actions to real user identities.

Best practices for safe agent database testing

Start by stripping real customer records out of your MySQL sandbox before testing agents. Replace real emails, phone numbers, and national IDs with fake test values during the data load. If you must use real data snapshots, apply dynamic masking to rewrite sensitive columns in query results.

Next, split your agent tools into small, clear tasks instead of one open SQL runner. Give the model separate tools for looking up orders, checking stock, or reading user profiles. This limits the kinds of SQL statements the agent can build on its own. Combine narrow tools with query timeouts in MySQL so bad joins fail fast.

Inspect every MySQL query and response in flight

Static grants and network rules block bad logins. They cannot pause a risky query for review or mask emails in a result set. By design, hoop.dev is an open-source proxy that runs as a sidecar for your MySQL. It reads plain queries, prepared statements, and cursors on the wire.

First, hoop.dev guardrails block banned actions before MySQL runs them. An AI Analyzer you configure can score intent, and hoop.dev data masking rewrites sensitive string columns in results. Every query and verdict goes to the resource audit trail. Find the open-source binary on GitHub.

Frequently asked questions

How do I stop a Pydantic AI agent from dropping tables in MySQL?

Create a dedicated MySQL user for the agent and grant only select permissions on your target schema. Do not give the agent user drop, alter, or delete rights in the sandbox.

Can Pydantic AI validate SQL queries before MySQL runs them?

Pydantic AI validates the Python data types passed into a tool, such as checking for a text string. It does not parse SQL syntax or block risky table reads inside the query string itself.

How should teams handle sensitive data inside a MySQL sandbox?

Teams should scrub private records before loading data into the sandbox or use masking rules on results. Masking rewrites fields like emails, phone numbers, and card numbers before the agent reads the output.

The query runs. The emails stay hidden.