Skip to main content
Pre-release

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. This package is not on PyPI until the maintainer publishes it. Install it from a checkout of the repository for now:

pip install ./clients/langchain                # the tool (langchain-core)
pip install "./clients/langchain[agent]"       # plus langchain 1.x for create_agent
pip install "./clients/langchain[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: 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.

Metadata

Release files for langchain-sqlguardllm 0.1.0a1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-sqlguardllm 0.1.0a1
File Size Uploaded Provenance Actions
langchain_sqlguardllm-0.1.0a1.tar.gz 5.9 kB With provenance Details for langchain_sqlguardllm-0.1.0a1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-sqlguardllm 0.1.0a1
File Interpreter ABI Platform Actions
langchain_sqlguardllm-0.1.0a1-py3-none-any.whl Python 3 none any Details for langchain_sqlguardllm-0.1.0a1-py3-none-any.whl

Total release size: 11.4 kB

Release files / langchain_sqlguardllm-0.1.0a1.tar.gz

Download URL langchain_sqlguardllm-0.1.0a1.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
12f7ad40219d25ceddbfed00e822a2b2110a914a3699945a6610c9ef00d3e293
BLAKE2b-256 checksum
How to use checksums
338159f8a193ee93944405996fc01b05856724625f14c2e8ba1a899b7f73661e
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 log

Release files / langchain_sqlguardllm-0.1.0a1-py3-none-any.whl

Download URL langchain_sqlguardllm-0.1.0a1-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
19a718ff6b0fd56a4e87c161b1ac2160289df28d71220dd4da5afca75861ce99
BLAKE2b-256 checksum
How to use checksums
85c675229411dc213858d51ee2ab8532b5f721011c7fbb3883e66fbfc7d9f5ec
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 log

Security for langchain-sqlguardllm 0.1.0a1

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.

Select from 2 files

Attestation for langchain_sqlguardllm-0.1.0a1.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 log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page