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.

Recommended Pydantic AI docs positioning

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.

Download files

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

Source Distribution

pydantic_ai_deepkeep-0.1.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

pydantic_ai_deepkeep-0.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_ai_deepkeep-0.1.0.tar.gz.

File metadata

  • Download URL: pydantic_ai_deepkeep-0.1.0.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.5

File hashes

Hashes for pydantic_ai_deepkeep-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6d49ded72986d2280a4f94c8a1e2990b8fdc231e80697c324b3623b502e13e2f
MD5 fb7c16e6c95946a725ff0742d99709ab
BLAKE2b-256 0adcf01c2657b1b747ecd323c80418e7fa1f87cc7842f55957d04beecdfb420f

See more details on using hashes here.

File details

Details for the file pydantic_ai_deepkeep-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_ai_deepkeep-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5fdb62e98d5909efa5d2b4b41051f5bee7dbe3e089e5ce2669af7addb9a2516
MD5 bab359163495dcb59918e96717a281cc
BLAKE2b-256 9b1ae0e99138239f2e9c03229f06d8400b6d0a30058c32b5a41c39d253ed5c18

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

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