Skip to main content

pydantic-ai-deepkeep

DeepKeep AI Firewall custom guardrails for Pydantic AI.

This package follows Pydantic AI's native custom capability extension point. Add DeepKeepGuardrail to an Agent with capabilities=[...] to check input before a model request and output after a model response.

Installation

pip install pydantic-ai-deepkeep

Set credentials:

export DEEPKEEP_API_KEY="dk_..."
export DEEPKEEP_BASE_URL="https://api.example.deepkeep.ai"

Input and output guardrails

from pydantic_ai import Agent
from pydantic_ai_deepkeep import DeepKeepGuardrail

agent = Agent(
    "openai:gpt-5.2",
    instructions="Answer user questions safely and concisely.",
    capabilities=[
        DeepKeepGuardrail(
            pre_model="input-firewall-id",
            post_model="output-firewall-id",
        )
    ],
)

result = agent.run_sync("Explain how to store API keys securely.")
print(result.output)

Agent specs

DeepKeepGuardrail supports Pydantic AI agent specs when registered as a custom capability type:

model: openai:gpt-5.2
instructions: Answer user questions safely and concisely.
capabilities:
  - DeepKeepGuardrail:
      pre_model: input-firewall-id
      post_model: output-firewall-id
from pydantic_ai import Agent
from pydantic_ai_deepkeep import DeepKeepGuardrail

agent = Agent.from_file("agent.yaml", custom_capability_types=[DeepKeepGuardrail])

Why a capability

Pydantic AI capabilities bundle hooks, tools, instructions, and settings. DeepKeep should run deterministically around model execution, so the correct integration surface is a custom AbstractCapability, not an agent-callable tool.

DeepKeepGuardrail uses:

  • before_model_request for DeepKeep pre-moderation before content reaches the model.
  • after_model_request for DeepKeep post-moderation before model output is returned.

DeepKeep endpoints

This package uses the same DeepKeep OpenAI-compatible moderation endpoints as the other DeepKeep integrations:

  • POST /api/v3/openai/moderations/pre with {"model", "input", "title", "chat"}.
  • POST /api/v3/openai/moderations/post with {"model", "output", "title", "chat"}.
  • X-API-Key for authentication.

pre_model and post_model are DeepKeep firewall IDs. Each value is sent to the matching moderation endpoint as the model field.

Guardrail actions

DeepKeep responses are interpreted from verbosity[].details.guardrail_action:

  • block: Raises DeepKeepBlockedError and aborts the run.
  • redact or modify: Replaces the latest text message or response part when DeepKeep returns modified content.
  • alert: Allows the run to continue.

If DeepKeep returns an unsupported action, the capability fails closed.

Configuration

  • api_key: DeepKeep API key. If omitted, reads DEEPKEEP_API_KEY.
  • base_url: DeepKeep base URL, without a trailing slash. If omitted, reads DEEPKEEP_BASE_URL.
  • pre_model: DeepKeep firewall ID for pre-moderation.
  • post_model: DeepKeep firewall ID for post-moderation.
  • title: Optional title value sent to DeepKeep.
  • chat: Optional chat value sent to DeepKeep.
  • timeout: Request timeout in seconds. Defaults to 30.0.
  • fail_closed: Raise when DeepKeep cannot be reached. Defaults to True.

Position the public integration as DeepKeep AI Firewall in Pydantic AI's integrations or custom capabilities area:

DeepKeep AI Firewall
Use DeepKeep AI Firewall as a custom Pydantic AI capability to enforce runtime guardrails before model requests and after model responses. Supports allow, block, redact, modify, and alert workflows for prompt injection, jailbreaks, sensitive data leakage, unsafe content, and other AI risks.

Metadata

Release files for pydantic-ai-deepkeep 0.1.0

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

Source distribution (sdist)

Source distribution for pydantic-ai-deepkeep 0.1.0
File Size Uploaded Provenance
pydantic_ai_deepkeep-0.1.0.tar.gz 9.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-ai-deepkeep 0.1.0
File Interpreter ABI Platform
pydantic_ai_deepkeep-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.7 kB

Release files / pydantic_ai_deepkeep-0.1.0.tar.gz

Download URL pydantic_ai_deepkeep-0.1.0.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
6d49ded72986d2280a4f94c8a1e2990b8fdc231e80697c324b3623b502e13e2f
BLAKE2b-256 checksum
How to use checksums
0adcf01c2657b1b747ecd323c80418e7fa1f87cc7842f55957d04beecdfb420f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.5

Release files / pydantic_ai_deepkeep-0.1.0-py3-none-any.whl

Download URL pydantic_ai_deepkeep-0.1.0-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e5fdb62e98d5909efa5d2b4b41051f5bee7dbe3e089e5ce2669af7addb9a2516
BLAKE2b-256 checksum
How to use checksums
9b1ae0e99138239f2e9c03229f06d8400b6d0a30058c32b5a41c39d253ed5c18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.5

Security for pydantic-ai-deepkeep 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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