Metadata-Version: 2.5
Name: langchain-sqlguardllm
Version: 0.1.0a2
Summary: LangChain tools and wrappers 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: langchain-core<2,>=1.0
Requires-Dist: sqlalchemy<3,>=2.0
Requires-Dist: sqlguardllm>=0.5.0a8
Provides-Extra: agent
Requires-Dist: langchain<2,>=1.0; extra == 'agent'
Provides-Extra: community
Requires-Dist: langchain-community<0.5,>=0.4; extra == 'community'
Provides-Extra: test
Requires-Dist: langchain-community<0.5,>=0.4; extra == 'test'
Requires-Dist: langchain<2,>=1.0; extra == 'test'
Requires-Dist: pytest>=8; extra == 'test'
Description-Content-Type: text/markdown

# langchain-sqlguardllm

A guarded SQL tool for LangChain / LangGraph agents, 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.** Install it from [PyPI](https://pypi.org/project/langchain-sqlguardllm/):

```bash
pip install --pre langchain-sqlguardllm                # the tool (langchain-core)
pip install --pre "langchain-sqlguardllm[agent]"       # plus langchain 1.x for create_agent
pip install --pre "langchain-sqlguardllm[community]"   # plus langchain-community for GuardedSQLDatabase
```

## Quick start

```python
import sqlite3
from contextlib import closing

from langchain_sqlguardllm import sql_query_tool
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,
)
guard = Guard(policy)


def execute(sql: str) -> list[tuple]:
    with closing(sqlite3.connect("file:sales.db?mode=ro", uri=True)) as conn:
        return conn.execute(sql).fetchall()


tool = sql_query_tool(guard, execute, lambda: {"user": {"branches": [1, 2]}})
print(tool.invoke({"query": "SELECT SUM(total) FROM invoices"}))
```

For SQLAlchemy, `sqlalchemy_executor(engine)` returns an `execute` that opens a connection
per call and always rolls back.

## In an agent

```python
from langchain.agents import create_agent

agent = create_agent(model, [sql_query_tool(guard, execute, current_user_context)])
```

A rejected query comes back to the model as `Error: <reasons>` (for example
`customers.email is a hidden column`), so it can rewrite the query. Any other failure of
`execute` is returned as a generic error without internals.

## langchain-community variant

```python
from langchain_community.agent_toolkits import SQLDatabaseToolkit
from langchain_sqlguardllm import GuardedSQLDatabase

db = GuardedSQLDatabase.from_uri(
    DATABASE_URL,
    guard=guard,
    context_fn=current_user_context,
    include_tables=sorted(policy.tables),
    sample_rows_in_table_info=0,
)
tools = SQLDatabaseToolkit(db=db, llm=model).get_tools()
```

langchain-community 0.4 was archived; prefer `sql_query_tool` for new code.

## Safety notes

- Keep the layers under the guard: connect with a **read-only database user** and set a
  statement timeout.
- The **context comes from the authenticated session**, never from the model.
- **Sample rows**: `GuardedSQLDatabase` refuses `sample_rows_in_table_info` other than `0`,
  because sample rows are read outside the guard. Don't give agents a schema tool that reads them.

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