How to Configure Pydantic AI MariaDB Credentials

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up pydantic ai mariadb credentials by passing a database login into the agent dependency context. The agent tool reads these login details at runtime to open a database session and run queries. Most teams store a shared database user and password in local files or cloud secret stores. When that shared database role has broad permissions, one bad prompt can alter or expose production tables. How pydantic ai mariadb credentials work in practice Pydantic AI uses a dependency system to pa

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 credentials by passing a database login into the agent dependency context. The agent tool reads these login details at runtime to open a database session and run queries. Most teams store a shared database user and password in local files or cloud secret stores. When that shared database role has broad permissions, one bad prompt can alter or expose production tables.

How pydantic ai mariadb credentials work in practice

Pydantic AI uses a dependency system to pass external state into agent tools during a run. Instead of hardcoding passwords inside tool logic, engineers initialize a MariaDB client pool when the app starts. The app then injects that pool or session object into each agent run through the dependency context.

When the language model decides to call a database tool, the tool grabs the active MariaDB session. It runs the generated SQL statement using the permissions tied to those specific database credentials. MariaDB checks the user account host rules and table grants before running the query.

To keep this setup safe, teams create a dedicated MariaDB user just for the AI agent. They restrict this user to read permissions on a few approved tables or views. They also turn on TLS so data stays encrypted as it moves over the network.

What native MariaDB roles solve and where they fall short

Native MariaDB user accounts and table grants solve the basic problem of who can log in. They block unknown hosts and stop a read-only agent from dropping tables or deleting rows. Password rotation tools in the cloud also help teams refresh static passwords on a schedule.

Still, static database roles only check permissions at login and at the table level. Once the agent logs in, MariaDB trusts every query that passes the basic table grant check. A read grant on a customer table lets the agent pull every single row and column.

If a user table holds emails, phone numbers, or card numbers, the query returns all of them. Native grants cannot inspect the agent intent or filter out sensitive columns on the fly. All queries from the app also share one database user name in the MariaDB audit logs.

Ephemeral Credentials + AI Agent Security: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

This shared identity makes it hard to trace which end user or prompt triggered a heavy query. Teams need a way to inspect individual statements in flight and rewrite sensitive fields in results.

Steps to harden agent database sessions

First, stop using admin or app writer accounts for your AI agent tools. Create a distinct MariaDB role that only has select grants on specific business tables. Never give the agent access to system tables or user password hashes.

Second, set strict resource limits on that MariaDB account to stop runaway queries. You can cap the number of queries per hour and limit concurrent connections for that role. This prevents a looping agent from starving the main app of database memory and CPU.

Third, keep credentials out of source code and container images at all times. Load the MariaDB user and password from a secret store into memory only when the worker starts. Clear error logs so stack traces do not print raw connection strings when a query fails.

Add wire guardrails and masking beside MySQL

Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev runs beside the database as a sidecar for your MySQL. It keeps your login setup and drivers as they are while checking queries on the wire.

With hoop.dev MySQL support, guardrails refuse bad queries before they run and return a readable MySQL error. Data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.

Frequently asked questions

Where should I store MariaDB credentials for a Pydantic AI app?

Store the database user and password in a dedicated secret manager rather than in code files. Pass them into the app memory at startup and inject the session pool via agent dependencies.

Should my Pydantic AI agent use the same MariaDB user as my main web app?

No, the agent should always use a separate MariaDB user with strict read-only table grants. Your main web app needs write access, which is too risky for model-generated queries.

How do I keep an AI agent from reading sensitive columns in MariaDB?

You can create restricted SQL views in MariaDB that leave out sensitive columns. For dynamic protection, use data masking to rewrite emails, phone numbers, and card numbers in query results.

The query runs. The emails stay hidden.