Skip to main content
The PromptGuard Python SDK provides auto-instrumentation that secures all your LLM calls — OpenAI, Anthropic, Google, Cohere, and AWS Bedrock — without changing any application code. It also works automatically with frameworks like LangChain, CrewAI, LlamaIndex, and AutoGen.

GitHub Repository

Open source - MIT license. Star the repo, report issues, or contribute.

Installation

Optional extras for framework-specific integrations:
Requires Python 3.8+.

Quick Start

Add two lines to your application startup. Every LLM call is now protected:
Set the PROMPTGUARD_API_KEY environment variable so you don’t need to pass api_key in code. You can also set PROMPTGUARD_BASE_URL to point to a custom deployment.

Auto-Instrumentation

promptguard.init() is the recommended way to use the SDK. It monkey-patches the create() methods on popular LLM SDKs so every call is scanned by the PromptGuard Guard API — before (and optionally after) the LLM is invoked.

promptguard.init()

Supported LLM SDKs

Auto-instrumentation patches these SDKs automatically — if the package is installed, it gets patched:
SDKs that are not installed are silently skipped. You only need to install the LLM SDKs you actually use.

Framework Compatibility

Because auto-instrumentation patches at the SDK level, it works transparently with any framework built on top of these SDKs:
  • LangChainChatOpenAI, ChatAnthropic, etc.
  • CrewAI — All agent LLM calls
  • LlamaIndex — All LLM integrations
  • AutoGen — Multi-agent conversations
  • Semantic Kernel — All LLM connectors
  • Any other framework that uses the supported SDKs

Modes

Enforce mode (default) — blocks requests that violate security policies by raising PromptGuardBlockedError:
Monitor mode — logs threats but never blocks. Useful for shadow deployment and testing:

Fail Open vs. Fail Closed

Controls behavior when the PromptGuard Guard API is unreachable:
Setting fail_open=False means your LLM calls will fail if the Guard API is unreachable. Only use this in high-security environments where blocking is preferable to unscanned requests.

Response Scanning

By default, only inputs (prompts) are scanned. Enable response scanning to also check LLM outputs:

promptguard.shutdown()

Removes all patches and closes the guard client. Call this during application shutdown:

Guard Client

The GuardClient lets you scan content directly without auto-instrumentation. Useful for custom scanning workflows or when you need fine-grained control.

Creating a Client

guard.scan()

Synchronous content scanning:

guard.scan_async()

Async version with the same interface:

GuardDecision

Both scan() and scan_async() return a GuardDecision object: Convenience properties:

Cleanup


Framework Integrations

In addition to auto-instrumentation, the SDK provides dedicated integrations for deeper framework support with richer context.

LangChain

CrewAI

LlamaIndex

Framework integrations provide richer context (chain names, tool calls, agent steps) to the Guard API, which improves detection accuracy. Use them when you want deeper observability alongside auto-instrumentation.

Error Handling

PromptGuardBlockedError

Raised when auto-instrumentation blocks a request in enforce mode. Contains the full GuardDecision:

GuardApiError

Raised when the Guard API is unreachable or returns an error. Only surfaced when fail_open=False — when fail_open=True (the default), API errors are caught internally and the request is allowed through.

PromptGuardError

Raised by the proxy client (PromptGuard class) for API-level errors:

Retry Configuration

Both PromptGuard and PromptGuardAsync automatically retry requests that fail with 429 (rate limited), 5xx (server error), or transient transport errors (connection resets, timeouts). Retries use exponential backoff with jitter.
Retry behavior:
  • 429 responses — retried after the Retry-After header value (if present), otherwise exponential backoff
  • 500, 502, 503, 504 responses — retried with exponential backoff
  • Transport errors (connection reset, DNS failure, timeout) — retried with exponential backoff
  • 4xx responses (other than 429) — not retried (these indicate client errors)
The GuardClient also supports retry configuration via the same max_retries and retry_delay parameters.

Proxy Mode (Legacy)

The PromptGuard proxy client is the original way to use the SDK. It still works, but auto-instrumentation via promptguard.init() is the recommended approach — it requires no code changes to your LLM calls.
The PromptGuard class provides an OpenAI-compatible client that routes requests through the PromptGuard proxy for security scanning:

Streaming

Context Manager

Async Client

The PromptGuardAsync class provides full async API parity with PromptGuard. All resource namespaces are available:

Embeddings

Generate embeddings through the PromptGuard proxy:
Batch embedding with a list of inputs:

Legacy Completions

The completions API is deprecated and provided only for backward compatibility. Use chat.completions.create() instead for all new code.

Complete Example


Environment Variables


Requirements

  • Python 3.8+
  • httpx >= 0.24.0 (installed automatically)
  • LLM SDKs you want to protect (e.g., openai, anthropic) — install separately