This release is a pre-release and may not be stable for production use.
langchain-sqlguardllm
A guarded SQL tool for LangChain / LangGraph agents, built on 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:
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
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
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
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:
GuardedSQLDatabaserefusessample_rows_in_table_infoother than0, because sample rows are read outside the guard. Don't give agents a schema tool that reads them.
Metadata
Release files for langchain-sqlguardllm 0.1.0a2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| langchain_sqlguardllm-0.1.0a2.tar.gz | 5.9 kB | With provenance | Details for langchain_sqlguardllm-0.1.0a2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| langchain_sqlguardllm-0.1.0a2-py3-none-any.whl | Python 3 | none | any | Details for langchain_sqlguardllm-0.1.0a2-py3-none-any.whl |
Total release size: 11.4 kB
Release files / langchain_sqlguardllm-0.1.0a2.tar.gz
| Download URL | langchain_sqlguardllm-0.1.0a2.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4bc80f49dafcb96daf695666d97991a8e9f252ac015b836ba5e02802c72e1e1e
|
|
BLAKE2b-256 checksum How to use checksums |
0f47bf284124f401e350156eba2b25afba0d4c77c9826d682b5c31f41fb051ed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logRelease files / langchain_sqlguardllm-0.1.0a2-py3-none-any.whl
| Download URL | langchain_sqlguardllm-0.1.0a2-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
affdbb260bbd9308e9ca60c782026a7c19d28617acb03f8ffc08f4f48708b433
|
|
BLAKE2b-256 checksum How to use checksums |
c5a5f1292399ffce1ee879cae0fc4eb7f549708d006d8853ce17d0c19cc11e43
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logSecurity for langchain-sqlguardllm 0.1.0a2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for langchain_sqlguardllm-0.1.0a2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.