Metadata-Version: 2.5
Name: llama-index-tools-sqlguardllm
Version: 0.1.0a1
Summary: LlamaIndex SQLDatabase and tool spec that only run SQL made safe by sqlguardllm.
Project-URL: Homepage, https://github.com/jmmana/sqlguardllm
Project-URL: Repository, https://github.com/jmmana/sqlguardllm
Project-URL: Issues, https://github.com/jmmana/sqlguardllm/issues
License-Expression: Apache-2.0
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Database
Classifier: Topic :: Security
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: llama-index-core<0.15,>=0.14
Requires-Dist: sqlalchemy<3,>=2.0
Requires-Dist: sqlguardllm>=0.5.0a8
Provides-Extra: test
Requires-Dist: pytest>=8; extra == 'test'
Description-Content-Type: text/markdown

# llama-index-tools-sqlguardllm

A guarded SQL database and tool spec for LlamaIndex, built on
[sqlguardllm](https://github.com/jmmana/sqlguardllm), a deterministic SQL firewall: the model
writes SQL, sqlguardllm checks and rewrites it against your policy, and only then does it run.

**Status: alpha.**

```bash
pip install --pre llama-index-tools-sqlguardllm
```

## Quick start

```python
from llama_index.core.query_engine import NLSQLTableQueryEngine
from llama_index.tools.sqlguardllm import GuardedSQLDatabase, SQLGuardToolSpec
from sqlalchemy import create_engine
from sqlguardllm import Guard, Policy

policy = Policy(
    dialect="sqlite",
    tables={"customers", "invoices"},
    hidden_columns={"customers.email"},
    row_filters={"invoices": "branch_id IN ({user.branches})"},
    max_rows=100,
)
db = GuardedSQLDatabase(
    create_engine("sqlite:///sales.db"),  # a read-only user in production
    guard=Guard(policy),
    context_fn=lambda: {"user": {"branches": [1, 2]}},  # from the session, never the model
)
engine = NLSQLTableQueryEngine(sql_database=db, tables=sorted(policy.tables), llm=llm)
tools = SQLGuardToolSpec(db).to_tool_list()  # run_sql, list_tables, describe_table for agents
```

`GuardedSQLDatabase` is a drop-in for `NLSQLTableQueryEngine` and
`SQLTableRetrieverQueryEngine`. A refused query raises `QueryRefused` (a `GuardViolation`) whose
message lists the violation codes, for example
`Query refused: [hidden_column] customers.email is a hidden column`; the engines answer with it
and the tool spec returns it as `Error: ...` so the model can rewrite the query.

## Safety notes

- Hidden columns never appear in `describe_table` or in the schema shown to the model; only
  tables in the policy are exposed.
- `sample_rows_in_table_info` must be `0` and `schema=` is not supported (both act outside the
  guard); use the policy's `default_schema`.
- Keep the layers under the guard: a **read-only database user** and a statement timeout.

More at https://github.com/jmmana/sqlguardllm.
