Python SDK for Silmaril Firewall: self-healing prompt injection defense for AI applications.
Silmaril evaluates agent execution as it unfolds, helping applications block harmful outcomes before injected instructions can manipulate tools, context, or data access. This package is the Python client for calling the Silmaril /classify API from application code.
Language SDK repositories follow the sdk- naming pattern. The Python SDK is published to PyPI as silmaril-security-sdk and is imported from silmaril_security.sdk .
This repository is public and source-available for Silmaril customers and integrators. It is not permissive open source; use, redistribution, and competitive-use restrictions are defined in LICENSE .
This SDK provides the low-level Python interface for that workflow:
Create a tenant-specific firewall client.
Classify user input, tool calls, tool responses, model output, or system prompt content.
Preserve hook and tool-name context for more accurate decisions.
Honor backend threat and governance decisions while applying Shadow, Warn, or Block behavior.
Send each complete sanitized event in one request.
On individual classify() calls, preserve exact metadata.conversationId as sequence identity and add one event ID.
Retry transient API Gateway and model-serving failures.
Optionally classify in parallel with the model call it protects.
Optionally attach the firewall to LangChain callback flows.
This SDK is distributed as a Python package on PyPI.
For reproducible installs, pin a tagged release:
Use a GitHub branch install only when you intentionally want the current branch tip:
Requires Python 3.10 or later.
Python 3.10 remains the package minimum and retains a compatibility lane even though the CPython project has ended upstream support. Removing that lane or raising requires-python requires a separately announced compatibility decision; upstream end-of-life alone does not remove SDK support.
The core classifiers and CI cover every core runtime in the table. New CPython releases are added after the core, async, installed-wheel, and applicable adapter checks can run. Optional integrations may have a narrower range: their support begins only when their dependencies install and their adapter tests pass. Deep Agents is currently qualified on Python 3.11 through 3.15.
The distribution name is silmaril-security-sdk . The SDK import path is silmaril_security.sdk , so call sites use Firewall , HookLabel , and FirewallBlockedException from that package.
Optional LangChain support. That extra installs the safe retained ranges langchain-core>=0.3.85, =1.3.3, =0.25.0 . Package metadata expresses that union as >=0.3.85, =0.7.30, =1.3.3, =0.25.0 . The supported runtime range is Python 3.11 through 3.15. On Python 3.10 the environment marker skips the deepagents package.
Native async support installs httpx>=0.25.0 , which is the module AsyncFirewall imports. The LangChain and Deep Agents extras install that same httpx floor.
Every Firewall client needs two required options:
api_key : your Silmaril API key.
api_url : the /classify endpoint for your tenant, stage, and region (for example, https:// .execute-api. .amazonaws.com/ /classify ).
Both are typically read from environment variables:
AsyncFirewall has the same classification options, results, modes, callbacks, and blocking exceptions as Firewall . It keeps one pooled httpx.AsyncClient for concurrent calls on independent events or different conversations:
One AsyncFirewall can be shared safely by concurrent tasks on one event loop when those tasks classify independent events or different conversations. For one conversation, await ordered individual classify() calls and finish each call before sending the event. The client binds to the first running loop that uses it and rejects use from another loop or after aclose() .
aclose() shuts down gracefully: new classifications are rejected immediately, requests that are already sending or waiting to retry are allowed to finish, and only then is an SDK-owned pool closed. Exiting the async with block calls it for you, repeated calls are idempotent, and concurrent callers all return once the pool is actually closed. Cancelling a task that is awaiting aclose() cancels only that waiter: shutdown continues, and an SDK-owned pool still closes once in-flight work finishes. If http_client= supplies an httpx.AsyncClient , the caller retains ownership and must close it.
Closing from inside your own in-flight classification raises RuntimeError instead of tearing the pool out from under that request. An on_classify callback runs after its request finishes, so closing from a callback works. AsyncFirewall accepts a synchronous or coroutine on_classify and awaits a coroutine. Firewall calls a synchronous callback. Both log callback exceptions and keep the classification verdict.
For synchronous off-thread work, create one Firewall per worker thread and use those threads for independent events or different conversations. Neither client promises sharing across threads or event loops.
Both clients take keyword-only arguments.
classify() and classify_batch() return the server's prediction, score, backend threshold, and BlockResult.mode . Current Firewall responses omit mode , so omitting a client and per-call mode does not select a backend-configured mode: BlockResult.mode remains None , while direct SDK calls retain their pre-0.6 Block enforcement fallback internally. Set mode="shadow" or mode="warn" explicitly for observation without a blocking exception. A malicious prediction, or a governance action of "block" , raises a typed blocking exception only when the locally effective mode is "block" . Per-call governance , review , and request_id are described under Governance, Review, and Request Metadata. None for review means that layer makes no selection. A per-call review=None inherits an explicit client setting and still sends it. The wire field is omitted only when neither the client nor the call has an explicit selection. None does not send JSON null .
When a custom requests.Session is provided, the SDK sends its x-api-key and content-type headers on each Firewall request without modifying the session's default headers. Sharing that session with other clients does not make the Firewall API key a default for their requests.
Use Shadow or Warn when you want direct classify() calls to return a malicious result for application routing instead of raising:
benign : no harmful firewall outcome detected.
information_disclosure : private data, documents, internal context, logs, traces, customer data, SQL rows, topology, or similar non-secret sensitive information.
secret_exposure : credentials, tokens, API keys, cookies, passwords, signing keys, OAuth secrets, session material, or webhook secrets.
control_abuse : misuse of authorized tools or user privileges to send, change, approve, delete, operate, or bypass policy/RBAC without a stronger outcome.
system_compromise : privilege escalation, account takeover, hostile integration/plugin takeover, persistence, lateral movement, attacker webhook registration, or code/plugin execution.
service_disruption : downtime, lockout, degradation, alert suppression, destructive loops, resource exhaustion, cost spikes, or hidden outage evidence.
code_generation : generation or material modification of executable code, scripts, workflows, or configuration.
story_script_generation : generation of narrative prose, dialogue, scripts, or story artifacts.
game_generation : generation of a game, quest, level, mechanic, or playable experience.
website_generation : generation of a website, landing page, storefront, or web experience.
abuse : content or actions that violate the tenant's acceptable-use policy.
traditional_ai_abuse : unsafe AI assistance outside the concrete security outcome classes.
Customers do not tune score thresholds in the SDK. The SDK does not send threshold in request payloads. The Firewall backend owns the threat decision and threshold policy. The current Cascade backend resolves decision thresholds from a tenant default or a hook-specific override; it does not raise them as text length, token-window count, batch size, or conversation length grows.
Returned threshold fields on BlockResult and blocking exceptions are backend diagnostic metadata. Disabled and observe policy paths can retain the compatibility threshold, so those fields are not always the applied tenant threshold.
Set "shadow" , "warn" , or "block" on the client, an individual call, or an adapter to select enforcement behavior explicitly. Shadow and Warn preserve the caller flow; Block raises FirewallBlockedException or BatchFirewallBlockedException for a malicious decision. Current Firewall services do not return an effective mode, so callers must not rely on omitted mode to use backend mode configuration.
An explicit client, per-call, or adapter mode is locally authoritative if a response omits or conflicts with it. When both the request and response omit mode , BlockResult.mode remains None ; direct Python calls retain their pre-0.6 Block enforcement fallback without reporting a backend Block mode. The bundled LangChain handlers and Deep Agents middleware also currently fall back to Block when neither side supplies a mode. Configure Shadow or Warn explicitly for observation instead of depending on an implicit mode. A future compatible service that returns mode can supply it when no explicit override was requested; this is not current Firewall behavior.
Per-call overrides let you select one surface without changing the client default:
Legacy shadow_mode=True maps to Shadow and shadow_mode=False maps to Block; explicit mode takes precedence. ClassifyEvent includes hook , tool_name , text , result , blocked , mode , and shadow_mode . blocked is true for a malicious prediction or a governance action of "block" . Only effective Block mode raises.
Review selection is available starting with Python SDK 0.7.5. Deploy compatible Firewall support before using an explicit selection.
review selects the optional large-judge stage for one /classify request. The only valid values are the exact strings "standard" and "deep" . "standard" disables that stage for the request. "deep" requests the existing second opinion on eligible Qwen threat candidates and does not force large-model execution on every event. None means that layer makes no selection. A per-call review=None inherits an explicit client setting and still sends it. The SDK omits the wire field only when neither the client nor the call has an explicit selection, so the selected tenant/surface AppConfig threat_judge.enabled remains authoritative. When a judge stays configured, enabled: false is the overridable server default, and an authenticated client may still request "deep" . Removing the judge configuration prevents deep requests. The SDK never fills an implicit review default. Invalid values, including other strings, whitespace, case variants, and non-strings, raise ValueError locally before HTTP.
This option requires a backend that acknowledges explicit review on successful /classify responses. Deploy backend support before relying on standard or deep selection from this SDK. Deep review on an unsupported runtime, an inactive Cascade, or a surface without a configured judge returns HTTP 400 before inference. Standard review is valid without a judge. review is independent of mode .
The wire field is omitted only when the client and the call both have no explicit selection, and that omission keeps the legacy request and response shape. An explicit client review is inherited by classify() , classify_batch() , and existing LangChain and Deep Agents adapters, including when a call passes review=None . A per-call "standard" or "deep" overrides that default for one request without mutating the client. Successful explicit selection returns the same exact string on the response ( BlockResult.review for a single result, and the batch envelope copied onto each BlockResult ). That field reports the applied setting, not whether the judge ran.
If an explicit selection is missing from or does not exactly equal the top-level acknowledgement, the SDK raises ReviewCompatibilityError before callbacks or blocking exceptions. It does not retry a successful response with a missing or mismatched acknowledgement. Transient transport and retryable HTTP failures can still be retried before that response arrives. Do not infer support from echoed metadata.
prepend_hook() and prepend_tool_name() are legacy helpers for manual text-prefix integrations. classify() and classify_batch() send hook and tool metadata as structured JSON fields, so normal callers should use the hook , tool_name , hooks , and tool_names parameters.
Use metadata to forward application or integration identifiers to the classification API without embedding them in the classified text:
The SDK preserves caller metadata and adds a reserved metadata.silmaril namespace to every request. SDK-controlled fields are sdk_language , sdk_version , and request_id . classify() and classify_batch() accept request_id= ; otherwise each call generates one id. A batch writes that same id on every item and sets zero-based input_index . On an individual classify() call, exact metadata.conversationId is preserved as the backend sequence identity, and metadata.silmaril.request_id is the event identity. No aliases are inspected. If callers provide metadata["silmaril"] , it must be an object and SDK-reserved keys are overwritten by the SDK.
Batch calls accept one metadata object per text and preserve that per-item metadata, including metadata.conversationId . The metadata list must match the number of texts; use None for entries without metadata:
Current Cascade treats each batch input independently and neither reads nor updates conversation history. Giving items the same metadata.conversationId does not connect them into a sequence. For conversation-aware checks, send complete events through ordered individual classify() calls with the same metadata.conversationId , and wait for each call before sending the event for that conversation.
SilmarilApiError : raised when the firewall API responds with status 300 or higher, including redirects ( allow_redirects=False / follow_redirects=False ). Status 408, 429, 500, 502, 503, and 504 are retried first and raise this error once retries are exhausted. Carries status , status_text , and a 64 KiB-capped body ; the default exception message omits the body.
FirewallBlockedException : raised by classify() and by LangChain handlers when a malicious prediction or a governance "block" has effective Block mode. Carries score , threshold , prompt_text , hook , tool_name , and result . LangChain handlers set run_id ; direct client calls leave it None .
BatchFirewallBlockedException : raised by classify_batch() when one or more items are blocked under effective Block mode. Carries blocked (index, text, hook, tool name, and result for each blocked item) and results (the full batch).
ReviewCompatibilityError : raised when an explicit "standard" or "deep" selection is missing from or does not exactly equal the top-level /classify review acknowledgement. The SDK does not retry a successful response with that missing or mismatched acknowledgement. Transient transport and retryable HTTP failures can still be retried before that response arrives. Carries requested and acknowledged .
PromptBlockedException and BatchPromptBlockedException remain deprecated exports in 0.7.0. At runtime they are the same objects as FirewallBlockedException and BatchFirewallBlockedException .
All SDK exception types are regular Python exceptions and can be handled with except clauses.
classify() removes unpaired Unicode surrogates and sends the full logical event once. For those individual calls, the backend owns token-window processing. Sequence ordering applies when those events metadata.conversationId . Wait for each classify() call to finish before sending the event for that conversation.
classify_batch() sends each text as an independent input in one request. Batch items still preserve per-item metadata, including metadata.conversationId , but current Cascade neither reads nor updates conversation history for those items. The same metadata.conversationId on multiple items does not connect them into a sequence.
Use classify_batch() to classify multiple independent texts in one round-trip:
Batch requests carry one SDK metadata object per item so the backend can apply tenant-owned thresholding. Each item preserves its metadata, including metadata.conversationId . Current Cascade treats each input independently and neither reads nor updates conversation history. Giving items the same metadata.conversationId does not connect them into a sequence. Hook, tool-name, and metadata arrays must match the number of texts. Thresholds are not accepted as a client option or per-call batch override.
For conversation-aware checks, send complete events through ordered individual classify() calls with the same metadata.conversationId . Wait for each call before sending the event for that conversation. Concurrent classify() calls are appropriate for independent events or different conversations.
Parallel Classification
protect() runs your call while its input is classified and returns your call's result. Both Firewall.protect() and AsyncFirewall.protect() accept hook , tool_name , metadata , governance , mode , review , and request_id as classification options. Use mode to choose enforcement for this call. The legacy shadow_mode keyword is available on classify() .
In Block mode the result is withheld until the verdict arrives, and a block raises FirewallBlockedException . AsyncFirewall also cancels the call when the block lands first; a synchronous call cannot be interrupted, so its result is discarded. In Shadow and Warn the result is returned immediately and the classification finishes in the background. The optional output extractor classifies the result as llm_output before it is returned.
Use protect() only for calls whose effect can be discarded. Classify the arguments of a tool that writes with classify() before running it.
Classifications that metadata.conversationId are sent in the order they were started, so parallel checks keep the backend sequence intact. That includes the Block-mode output check, which takes its turn in the same queue. protect() copies metadata when it is called, so later changes to that object do not reach a classification that is still waiting. max_concurrent bounds how many run at once. A background classification retries once. It is dropped with a warning when its conversation already has max_queued unjoined checks, or when max_concurrent * max_queued background classifications are waiting across all conversations, including those without a metadata.conversationId . If the operation raises, or the task awaiting AsyncFirewall.protect() is cancelled, a Block-mode check that has not been sent is withdrawn and the async operation is cancelled and awaited. A withdrawn check keeps its place in line, so later checks for that conversation still wait for earlier ones. fw.flush() waits for background classifications to finish, for example before a serverless handler returns; AsyncFirewall.aclose() does the same before it closes, and still closes the pool if the task awaiting it is cancelled. Calling aclose() from on_classify does not wait for classifications queued behind the one that invoked the callback. on_classify runs on a worker thread for a classification started by the synchronous protect() .
Version 0.10.0 classifies model output by default in the LangChain handlers: on_llm_end joins on_llm_start , on_chat_model_start , on_tool_start , and on_tool_end in DEFAULT_HOOKS . Pass hooks= without FirewallHook.LLM_END to keep the set. Shadow-mode handlers and Deep Agents middleware now classify in the background unless fail_open=False , so on_classify can run after the hook returns.
Version 0.8.0 removes the conversation_id option from framework handlers and middleware. Set only the framework’s native configurable.thread_id when invoking your graph or agent. Silmaril derives metadata.conversationId for each execution; no per-conversation handler or agent construction is needed. Calls without an available native thread ID omit conversation identity. Direct classify() and batch metadata APIs are unchanged. Version 0.8.1 preserves configured native routing through post-protection composition and supersedes 0.8.0 for this integration.
Version 0.4.1 contains the public 0.4.x SDK changes and supersedes the unpublished 0.4.0 package. The v0.4.0 Git tag exists, but PyPI publishing failed before the package was created, so 0.4.1 is the installable release line.
The 0.4.x line moves all threshold decisions to Firewall tenant/backend config, adds SDK reconstruction metadata, and renames blocking exceptions to FirewallBlockedException and BatchFirewallBlockedException .
Install the optional extra:
Protect the model or tool once at its invocation boundary:
The LangChain handler is fail-open by default: infrastructure errors are logged and the LLM call proceeds. Set fail_open=False to make API errors bubble up. Default hooks are on_llm_start , on_chat_model_start , on_tool_start , on_tool_end , and on_llm_end . on_retriever_start and on_retriever_end run only when hooks= includes them. With streaming, on_llm_end runs after tokens are emitted: a block stops the run and cannot recall them. include_tool=False skips tool start and end even if those hooks are enabled. include_system is stored and unused when the handler selects text. Model start classifies the latest user message and leaves earlier tool messages for the tool hooks. Each callback gets a distinct metadata.silmaril.request_id ; its LangChain run ID is sent as metadata.langgraph.run_id . protect_langchain_runnable() reads native configurable.thread_id at each invocation before calling the wrapped model or tool, then automatically sends it as metadata.conversationId . Normal invoke , ainvoke , batch , and streaming behavior and caller callbacks are preserved. Bind model-specific tools with model.bind_tools(...) before wrapping; the returned object exposes the standard Runnable interface. Post-protection configuration, argument binding, retry, listener and type composition preserve original configured layers and dynamic factories. Deep Agents users should use its protected constructor below. If no native ID is available, identity is omitted and checks continue independently. Handlers classify without a GovernanceContext . A governance "block" on the response still raises FirewallBlockedException in Block mode.
Pass parallel=True to a handler or to protect_langchain_runnable() to run checks beside the model call:
The user_input and tool_response checks then start without waiting and are joined when the model call ends, before a tool starts, and when the run ends. The tool_call and llm_output checks always wait for their verdict. A block that has already arrived is raised at the callback, including the streamed token. Callbacks cannot cancel a model call, so a late block discards its output. A deferred block stays visible to every callback of that run until the run ends, so concurrent branches all stop on it. In Shadow mode every check runs in the background and nothing is joined, unless fail_open=False requires each classification to succeed. fw.flush() waits for those checks; an async handler created from a synchronous Firewall tracks its own, so await handler.flush() there.
Existing callback-only integrations can still use fw.as_langchain_handler() or fw.as_async_langchain_handler() . They use exposed native thread metadata and active graph config. Ambiguous nested standalone callbacks omit identity. Framework versions can hide a direct model/tool invocation’s native config from ordinary callbacks, including a child override inside an existing thread. Use protect_langchain_runnable() at that model/tool boundary when native identity must be exact. Do not attach a second Silmaril handler to the same protected boundary. Callback bookkeeping is bounded to 4096 outstanding runs; when frameworks omit termination signals, oldest root/subtree entries are evicted and late completions omit unavailable identity. Protected stream / astream calls clear their own tracking on close.
Calling as_async_langchain_handler() on an AsyncFirewall shares its persistent pool. Calling it on a synchronous Firewall remains supported and uses a temporary async client for each handler classification. In both cases, handler fail_open , hook, run ID, blocking, and callback behavior is unchanged.
The same agent can handle many native threads. Every model/tool boundary uses the current execution’s native thread ID, including nested subagents that inherit their parent thread. Separately executed graphs retain their own native IDs. No thread ID means no conversation identity.
The constructor installs checks on the root, general-purpose, and declarative subagents. The released Deep Agents 0.7.x profile fields enabled , description , and system_prompt control the protected auto-added general-purpose subagent. enabled=False creates no replacement; an explicit named child remains explicit. Parent tools and skills are forwarded to that protected default before Deep Agents applies profile tool overrides and exclusions. String model specs are left for Deep Agents to construct exactly once; runtime-registered string-only profiles that cannot be recovered from the resolved model may customize only the released general-purpose fields above. Root-level prompt, tool, or middleware settings on such a profile fail with a compatibility error rather than silently changing the child. The released API has no profile rename field.
Two other released-API combinations also fail explicitly because an exact protected representation is unavailable: a profile that combines base_system_prompt with a general-purpose system_prompt , and a profile middleware factory combined with custom root middleware. Existing Silmaril middleware is reused only when its Firewall and mode , fail_open , max_blocked_attempts , and on_classify settings exactly match the requested factory options; duplicate, foreign, or conflicting boundaries are rejected.
Create a compiled subagent with create_protected_compiled_subagent(fw, name="review", description="Review", model=model, tools=tools, mode="isolated") and pass its returned spec through protected_compiled_subagents . That factory installs middleware before compilation. The parent constructor verifies the exact graph and Firewall client; it rejects arbitrary compiled runnables. mode may be "isolated" or "fork" and is omitted when unset; fork mode remains beta in Deep Agents. Root custom middleware is not automatically inherited by every subagent. Use AsyncFirewall with async graph execution; Firewall supports sync and async graph execution.
The middleware checks user input before model use, tool calls before execution, tool results before the model call, and model output returned from wrap_model_call / awrap_model_call . Model streaming has no wrapper in this middleware.
In Block mode, a denied tool call or tool result becomes a fixed safe ToolMessage that keeps the original tool call ID, so the agent can choose an allowed alternative. After max_blocked_attempts denied tool interactions in the current user turn (default 3), the Block-mode model call returns a fixed safe final response instead of calling the model. Denied model output from the non-streaming wrapper is replaced. Shadow and Warn report decisions through on_classify without replacing content or applying that cap. Classification errors allow model and tool execution by default; set fail_open=False in middleware_options to require a successful classification. The middleware classifies with governance=None . A governance "block" on the response is still enforced in Block mode.
Set middleware_options={"parallel": True} to classify user input while the model call runs. A blocked input replaces the response, and async execution cancels the model call. Tool calls and tool results still wait for their verdict, because a denied result is replaced before it enters agent state, and the input check waits again once max_blocked_attempts is reached. Shadow mode classifies in the background unless fail_open=False .
Remote support requires deepagents>=0.7.30, =26.1 .
The release workflow uses Python 3.12 and the qualified current framework tuple above. It runs pip check , ruff check src tests , then python -m pytest -q -m "not integration" , followed by python -m build and python -m twine check dist/* . Local setup and the live-endpoint rule are in CONTRIBUTING.md .
Publishing is handled by .github/workflows/release.yml when a version bump lands on main . Before merging a release PR, maintainers must confirm the PyPI trusted publisher for silmaril-security-sdk is configured for repository Silmaril-Security/sdk-python , workflow .github/workflows/release.yml , and environment pypi . The workflow builds and publishes before creating the Git tag so a PyPI authentication failure does not leave another stale release tag.
This SDK is source-available under the Silmaril SDK Source-Available License. It is not permissive open source. See LICENSE .
Governance resource requests
Typed governance resources are sent in the classify request's top-level resource field or aligned nullable resources array for batch calls. Resource IDs must be non-empty; MCP tools also require their canonical server parent ID. The compatibility metadata copy remains attribution only. Caller agent metadata does not supply an authenticated principal.
Published versions are listed on PyPI and GitHub Releases . Version 0.9.1 retains the safe LangChain 0.3.85 and 1.3.3+ ranges while excluding affected 1.0–1.2 and early 1.3 releases, and preserves released Deep Agents profile and compiled-mode semantics. The Deep Agents extra requires Python 3.11 or newer.
The full story
This article is one source in a clustered incident — the cluster page carries the summary, timeline and every other outlet covering it.
