Skip to main content

Tiny library for separating trusted prompts from untrusted RAG/tool content

Project description

bulkhead-ai (Python)

Tiny library for separating trusted prompts from untrusted RAG/tool content.

It stops external content (web pages, RAG results, tool outputs) from ever being treated as instructions, by separating the trusted USER bucket from the untrusted RETRIEVED bucket before anything reaches the model.

Install

pip install bulkhead-ai            # core, zero dependencies
pip install bulkhead-ai[openai]    # + OpenAI wrapper
pip install bulkhead-ai[anthropic] # + Anthropic wrapper
pip install bulkhead-ai[all]       # everything

The problem

# The "soup": instructions and untrusted data concatenated into one string.
# A web page that says "ignore previous instructions" has full instruction authority.
messages = [{"role": "user", "content": f"{user_prompt}\n\n{web_content}"}]

The fix

from bulkhead import seal
from bulkhead.wrappers.openai_wrapper import create_completion

# Recommended: the wrapper builds the correct messages=[...] shape for you.
response = create_completion(
    client,
    user=user_prompt,        # USER bucket — passed through untouched
    retrieved=web_content,   # RETRIEVED bucket — scored and sealed as JSON
    model="gpt-4o",
)

Or seal once and shape it for your SDK — each SDK expects a different shape:

sealed = seal(user=user_prompt, retrieved=web_content)

# OpenAI — guard in system; JSON payload in a user/data-position message
client.chat.completions.create(
    model="gpt-4o",
    messages=sealed.to_messages(),
)

# Anthropic — guard in the top-level system; JSON payload in the user turn
client.messages.create(
    model="claude-haiku-4-5-20251001",
    **sealed.to_anthropic_params(),
)

Where each bucket goes. The untrusted retrieved content is always placed in untrusted_inputs inside a user-role JSON message, never system. system holds only a trusted guard preamble, and your instruction stays authoritative as trusted_instruction.

**seal() unpacks to {"messages": [...]} (OpenAI shape), so client.chat.completions.create(model=..., **sealed) works directly. For Anthropic use to_anthropic_params() (its API can't take consecutive user messages).

API

seal(user, retrieved, config=None) -> SealOutput

  • user: str — the instruction. Untouched.
  • retrieved: str | list[str] — untrusted external content. Scored and sealed.
  • config: BulkheadConfig | None
  • Returns a SealOutput with fields instruction, data (the JSON payload, "" if none), guard, plus .to_messages() (OpenAI), .to_anthropic_params() (Anthropic), and ** unpacking (→ OpenAI messages). .prompt is a back-compat alias of instruction.

Bulkhead(config).seal(...) / Bulkhead(config).session()

A session accumulates risk_history across turns for observability. Each turn is scored and gated independently (track-only).

from bulkhead import Bulkhead, BulkheadConfig

session = Bulkhead(BulkheadConfig(policy="strict")).session()
for turn in agent_loop:
    sealed = session.seal(user=turn.prompt, retrieved=turn.external_content)
    ...  # session.risk_history grows each turn

Policy modes

mode behavior at score >= threshold (default 0.7)
strict raises BulkheadInjectionError, call blocked
warn (default) emits UserWarning, call proceeds
permissive annotates only, never blocks
BulkheadConfig(policy="strict", scorer=ScorerConfig(threshold=0.7, check_unicode=True))

The built-in scorer adds 0.3 per matched pattern (capped at 0.9), so a single textbook match scores 0.3 — it raises a flag but does not cross the default 0.7 block threshold. Treat the block as a coarse safety net; for real detection, pass your own scorer= (e.g. an LLM judge).

Wrappers

from bulkhead.wrappers.openai_wrapper import create_completion
from bulkhead.wrappers.anthropic_wrapper import create_message

Legacy single-string LangChain chain.run() is intentionally unsupported because it flattens system, trusted instruction, and untrusted data into one string.

How it works

Every call generates cryptographic IDs (secrets.token_hex), scores each retrieved source with a local regex/heuristic scorer (no ML, no network), applies the policy gate, then emits a JSON user message:

{
  "trusted_instruction": "Summarise this article.",
  "untrusted_inputs": [
    {"id": "...-1", "risk": 0.3, "flags": ["injection_pattern"], "content": "..."}
  ]
}

The system guard states that only trusted_instruction is authoritative and that untrusted_inputs is source material only.

Zero core dependencies. Pure Python standard library. Python 3.9+.

Defense-in-depth, not a silver bullet. The JSON structure is a strong hint, not an enforced wall — LLMs have no hard system/data boundary, and the built-in regex scorer is a heuristic pre-filter, not a detector. Swap in your own scorer (Bulkhead(config, scorer=my_detector)) and see the root threat model.

License

MIT © Hamza Jawad

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bulkhead_ai-0.1.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bulkhead_ai-0.1.1-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file bulkhead_ai-0.1.1.tar.gz.

File metadata

  • Download URL: bulkhead_ai-0.1.1.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bulkhead_ai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e0cfcd952d1feec230a312ff009dc3d7115a8c75fe0e917795576e1ed545e4b0
MD5 fc97c1e162aa90310e45fcdbd70d914e
BLAKE2b-256 b2a41198ebf62a1db99838650f3c89c02c331cdaf7357bb28f70223fb7d1b7f1

See more details on using hashes here.

Provenance

The following attestation bundles were made for bulkhead_ai-0.1.1.tar.gz:

Publisher: release.yml on hamj20k/bulkhead-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bulkhead_ai-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: bulkhead_ai-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bulkhead_ai-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 93b297aaf10718337379dd63593bc9b283d36eca9c8799b64df55d059c11e0cd
MD5 6f0c13f583723e74a495888542ec488a
BLAKE2b-256 5016e5a2bf42ec3bb33930d4a9ef86d9bb32c2b8e51619d921004b9f3e4c59cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for bulkhead_ai-0.1.1-py3-none-any.whl:

Publisher: release.yml on hamj20k/bulkhead-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page