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
Quick Start
Add two lines to your application startup. Every LLM call is now protected: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:- LangChain —
ChatOpenAI,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 raisingPromptGuardBlockedError:
Fail Open vs. Fail Closed
Controls behavior when the PromptGuard Guard API is unreachable: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
TheGuardClient 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
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
BothPromptGuard 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-Afterheader 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)
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.PromptGuard class provides an OpenAI-compatible client that routes requests through the PromptGuard proxy for security scanning:
Streaming
Context Manager
Async Client
ThePromptGuardAsync class provides full async API parity with PromptGuard. All resource namespaces are available:
Embeddings
Generate embeddings through the PromptGuard proxy:Legacy Completions
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