How to Configure Pydantic AI Cassandra Read Only Permissions

Published
Length
3 min read

Luis Felipe Dias

All posts

To set up pydantic ai cassandra read only permissions, create a restricted database role first. Next, pass that role into your agent tool setup so queries only read data. Many teams start by giving their agent a default admin account during early testing. That broad role lets a bad prompt alter tables, drop keyspaces, or wipe out data. How to set up pydantic ai cassandra read only permissions A safe setup starts inside your Cassandra cluster with native role rules. You must turn on role manag

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.

To set up pydantic ai cassandra read only permissions, create a restricted database role first. Next, pass that role into your agent tool setup so queries only read data. Many teams start by giving their agent a default admin account during early testing. That broad role lets a bad prompt alter tables, drop keyspaces, or wipe out data.

How to set up pydantic ai cassandra read only permissions

A safe setup starts inside your Cassandra cluster with native role rules. You must turn on role management in the cluster settings before creating new accounts. Then you make a dedicated role just for your AI agent to log in. You grant this new role select permissions on the exact keyspaces or tables needed. Do not grant modify, create, alter, or drop permissions to this specific agent role.

Next, you build the data tool inside your Pydantic AI app. Pydantic AI lets you pass a database session object through its dependency injection system. You initialize the Cassandra driver session at app startup using the restricted role credentials. When the model needs data, it calls your registered tool function with typed arguments. Your tool takes those checked inputs and runs a prepared read statement on the cluster. If the model tries to write data, the database rejects the command right away.

Why native database roles are only half the fix

Native database roles stop an agent from changing or deleting your stored records. They check who logs in and block write commands at the keyspace level. Still, a read role cannot inspect what happens inside an allowed select query. A user prompt can still trick the agent into reading sensitive customer tables. Once that query runs, raw emails, phone numbers, and card numbers enter the model context. Dynamic data masking rewrites matched fields in results before they reach the agent.

Read roles also fail to protect your cluster from bad query patterns. An agent might run a massive table scan without a partition key filter. In a distributed Cassandra cluster, unfiltered scans can overload nodes and slow down production apps. Static credentials stored in app config files create another big security risk for teams. If those secret keys leak, anyone can log in and read your whole keyspace.

AI Agent Permissions + Read-Only Root Filesystem: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Best practices for safe agent tool design

You can reduce query risks by keeping your Pydantic AI tools very strict. Never give the language model a tool that runs raw query strings. Instead, build narrow tools that represent one clear business question at a time. Use Pydantic models to check every input field before the tool runs any query. For example, require a valid user ID and a strict row limit every time.

Inside the tool code, bind those checked inputs to a fixed statement template. This simple step stops prompt injection from changing the structure of your database query. You should also set a short timeout on every session call from the agent. If a read request takes too long, the driver cancels it to save cluster resources. Combining strict code tools with narrow database roles gives you a solid baseline.

Govern statements and responses across databases and APIs

Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.

First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.

Frequently asked questions

Can Pydantic AI run raw queries on Cassandra by default?

No, Pydantic AI does not include a built-in database tool that runs raw queries. You write the Python tool functions yourself and decide how the driver runs statements.

What happens if the agent tries to write data with a read role?

Cassandra blocks the write request and returns an unauthorized error to the Python driver. Your Pydantic AI tool catches this error and tells the model the action failed.

How do I stop an agent from reading sensitive columns in a table?

Grant select rules only on safe tables, or list specific columns in your tool query. You can also apply data masking so sensitive fields like card numbers get rewritten in results.

The query runs. The emails stay hidden.