recusal-langgraph
The Recusal gate for
LangGraph: every tool call a ToolNode is
about to execute is judged by your policy first, and a refused call never runs.
New to Recusal? It checks each tool call an AI agent proposes against your policy and refuses the unsafe or unapproved ones before the tool runs. No model makes that decision. A policy is a plain Python function: it is given the tool's name and its arguments and returns a list of findings. An empty list lets the call run; a failing finding refuses it and gives the reason. Writing a policy covers the two postures, allowlist and deny-list.
This is a separate distribution from recusal. The kernel has zero runtime dependencies
and a frozen compatibility perimeter; this package carries the LangGraph dependency and
its own version, so a LangGraph release never forces a release of the kernel.
Install it from PyPI:
pip install recusal-langgraph
Requires Python 3.10+, recusal>=0.14.0, langgraph>=1.2.12 and langchain-core>=1.6.5.
It is tested on every pull request against exactly langgraph==1.2.12 with
langchain-core==1.6.5, and weekly against the newest releases.
The package ships inline types (py.typed) for your editor and type checker.
Use
from langchain_core.tools import tool
from langgraph.prebuilt import ToolNode
from recusal import Finding
from recusal_langgraph import gated_tool_node
@tool
def run_sql(sql: str) -> str:
"""Run a SQL statement."""
...
def policy(tool_name, tool_input):
sql = tool_input.get("sql", "")
if tool_name == "run_sql" and sql.lstrip().upper().startswith("DELETE"):
return [Finding.fail("no_deletes", severity="CRITICAL", message="DELETE is not allowed")]
return []
node = gated_tool_node([run_sql], policy)
Your @tool list goes into ToolNode unchanged. Every call in a model message crosses
the gate separately, immediately before execution. A refused call is answered with an
error ToolMessage carrying Recusal's reason, and its tool does not run. A policy that
raises refuses the call (fail closed).
gated_tool_node returns an ordinary ToolNode with the gate on both of its hook
slots, so the node refuses the same way on invoke, stream, ainvoke and astream. It
takes ToolNode's own options (name, handle_tool_errors, ...). To build the node
yourself, pass both hooks together:
from recusal_langgraph import is_gated, recusal_gates
node = ToolNode([run_sql], **recusal_gates(policy))
assert is_gated(node)
Why both. A ToolNode has a sync hook slot and an async one, and its sync route
(invoke, stream) looks only at the sync slot. A node given only awrap_tool_call runs
every tool there with no hook at all. wrap_tool_call=recusal_gate(policy) on its own is
also safe on every route, since a node with no async hook runs the sync one there too;
a tool that has only a coroutine is then refused on the async route.
A hook left alone in the async slot is the unsafe shape, and the package raises wherever it is able to:
recusal_agate(policy)raisesTypeErrorwhen you build it, unless you statehost_refuses_sync_runs=True. That statement is true of LangChain'screate_agentmiddleware and false of a ToolNode; making it for a ToolNode builds the unsafe shape.recusal_gate(policy)passed asawrap_tool_callraisesTypeErrorwhen the node runs on its async route (a node built withhandle_tool_errors=Trueturns that error into a refused call instead; the tool does not run either way).
What no hook can do: raise on the sync route of a node whose sync slot is empty. A
ToolNode calls no hook there, so a node built in one of the two ways above and only ever
run with invoke or stream runs its tools with no gate and no error. Build the node
with gated_tool_node, or check it:
is_gated(node) is True only for exactly a ToolNode (not a subclass) with nothing on
the instance replacing how it runs calls, a sync gate from this package in the sync
slot, and in the async slot either nothing or this package's async gate for the same
policy and the same audit log. A hook wrapped in another function, a hook from
elsewhere, and hooks for two different policies are answered False: some of those may be
fine, but it cannot tell. It reads the node as it is when you call it.
Before 0.2.0 this page told you to attach recusal_agate(policy) with awrap_tool_call
for an async graph. A node built that way and then run with invoke or stream
executed its tools ungated and unrecorded. If you have one, replace it with the form
above.
The policy judges the invocation the tool executes, not the dict the model wrote: the input is validated once, defaults the model omitted are completed, and those same objects are passed to the function. Arguments the host supplies (state, store, runtime) are left out of judgment. An argument counts as the host's only when ToolNode really replaced it for this call: a value the model sent that reaches the tool unchanged is judged like any other, whatever the tool's annotations say.
Put every decision on the record
from recusal.audit import AuditLog
from recusal_langgraph import audited_tool_node
log = AuditLog(path="decisions.jsonl")
node = audited_tool_node([run_sql], policy, log)
ToolNode(tools, **recusal_audited_gates(policy, log)) is the same pair for a node you
build yourself. Each call the gate sees, allowed or refused,
is one hash-chained entry labeled langgraph.toolnode with LangGraph's tool call id and
a SHA-256 fingerprint of the arguments (never the arguments). If the log cannot be
written the call is refused. Check the log with recusal audit verify decisions.jsonl.
The audited gates call the plain gate's own judging function, so there is one decision path.
What the gate refuses, and what it does not see
- Refused before the policy runs: a tool whose class or instance overrides the
standard execution lifecycle (it could run without reaching the gate), a
StructuredToolsubclass that replaces_runor_arun(it would execute something other than the function the gate inspects), input that does not validate against the tool's schema, and a tool whose callable declares a parameter the schema omits, since that is a value no proposal can carry and the policy would never see. - Refused, never run unjudged: a tool body reached without the gate's judging step. The gate judges inside the tool's parse step; the copy of the tool it hands to execution runs its function only once per allowance, and only with the argument objects that step judged, so a framework release that changed the lifecycle would be refused, not run.
- Not on the audit log: a call another
wrap_tool_callhook answered without reaching the gate. - Not seen: a
ToolNodeyou did not gate.is_gatedreads one node; a graph can hold others,create_react_agent(model, [tools])builds its own ungated one (pass it your gated node instead), and a tool that calls another tool directly, for example throughruntime.tools, bypasses every hook.
This package reads two private LangChain and LangGraph helpers to find the arguments the host injects. That is why its framework requirements have an upper bound, and why the weekly lane against the newest releases exists.
Compatibility
This package is outside the kernel's frozen perimeter (STABILITY.md, Integrations). Its
public names are the ones in recusal_langgraph.__all__. When a LangGraph release
changes what the hook is handed, this package gets a release and the kernel does not.
A runnable offline demonstration, with a scripted graph and no API key, is
examples/langgraph_gate.py
and
examples/langgraph_audit.py.
More
An arrow means "depends on". No arrow leaves the kernel, so a framework's breaking change is answered by a release of that framework's adapter, not by a release of the kernel.
- Framework notes for LangGraph: the detail behind this page.
- All eight adapter packages and the kernel, which has zero runtime dependencies.
- Verifying a release: each release is built in CI, signed with Sigstore and published to PyPI by trusted publishing.
- Changelog, issues, security policy and support.
Licensed under Apache-2.0.
Metadata
Release files for recusal-langgraph 0.2.0
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 |
|---|---|---|---|---|
| recusal_langgraph-0.2.0.tar.gz | 22.9 kB | With provenance | Details for recusal_langgraph-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| recusal_langgraph-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for recusal_langgraph-0.2.0-py3-none-any.whl |
Total release size: 45.8 kB
Release files / recusal_langgraph-0.2.0.tar.gz
| Download URL | recusal_langgraph-0.2.0.tar.gz |
|---|---|
| Size | 22.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
506baf56297aba671b120003c69bf2c094e97e1254acde7fefa466ac37c6646e
|
|
BLAKE2b-256 checksum How to use checksums |
dd93ff55733331d330fa60241d0c0f41be4107f04c838820fdcb62f7c99a2608
|
| 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 19, 2026.
Transparency logRelease files / recusal_langgraph-0.2.0-py3-none-any.whl
| Download URL | recusal_langgraph-0.2.0-py3-none-any.whl |
|---|---|
| Size | 22.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c68eb0eccae42ba85bd9ae8bac17bdca87b755bf2a1ee7d0ea312379dcffbf0c
|
|
BLAKE2b-256 checksum How to use checksums |
6bff3a271eab9088b8305e79679ff6828d8c12f32c626906a186d521b22ed671
|
| 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 19, 2026.
Transparency logSecurity for recusal-langgraph 0.2.0
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 recusal_langgraph-0.2.0.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 19, 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.