Secure every LLM call in your Python application with one line of code
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.
Add two lines to your application startup. Every LLM call is now protected:
Copy
import promptguardpromptguard.init(api_key="pg_xxx") # or set PROMPTGUARD_API_KEY env var# Your existing code works exactly as before -- now with security scanningfrom openai import OpenAIclient = OpenAI()response = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "Hello!"}])# PromptGuard scans the input before it reaches OpenAI.# If a threat is detected in enforce mode, a PromptGuardBlockedError is raised.print(response.choices[0].message.content)
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.
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.
import promptguardpromptguard.init( api_key="pg_xxx", # PromptGuard API key mode="enforce", # "enforce" or "monitor" fail_open=True, # Allow requests if Guard API is unreachable scan_responses=False, # Also scan LLM responses timeout=10.0, # Timeout for Guard API calls (seconds))
Parameter
Type
Default
Description
api_key
str
None
PromptGuard API key. Falls back to PROMPTGUARD_API_KEY env var
base_url
str
None
API base URL. Falls back to PROMPTGUARD_BASE_URL, then https://api.promptguard.co/api/v1
mode
str
"enforce"
"enforce" blocks threats. "monitor" logs threats but never blocks
fail_open
bool
True
If True, allow LLM calls when the Guard API is unreachable. Set to False to fail closed
scan_responses
bool
False
If True, also scan LLM responses with direction="output"
Controls behavior when the PromptGuard Guard API is unreachable:
Copy
# Fail open (default): allow LLM calls if Guard API is downpromptguard.init(api_key="pg_xxx", fail_open=True)# Fail closed: block LLM calls if Guard API is downpromptguard.init(api_key="pg_xxx", fail_open=False)
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.
The GuardClient lets you scan content directly without auto-instrumentation. Useful for custom scanning workflows or when you need fine-grained control.
from promptguard.integrations.langchain import PromptGuardCallbackHandlerfrom langchain_openai import ChatOpenAIhandler = PromptGuardCallbackHandler(api_key="pg_xxx")# Attach to a single LLMllm = ChatOpenAI(model="gpt-4o", callbacks=[handler])# Or use globally with any chain or agentchain.invoke({"input": "..."}, config={"callbacks": [handler]})
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.
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.
Copy
from promptguard import GuardApiErrortry: decision = guard.scan( messages=[{"role": "user", "content": "Hello"}], direction="input", )except GuardApiError as e: print(f"API error: {e}") print(f"Status code: {e.status_code}") # int or None
Attribute
Type
Description
status_code
int | None
HTTP status code from the Guard API (if available)
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.
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:
Copy
from promptguard import PromptGuardpg = PromptGuard(api_key="pg_xxx")response = pg.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"} ], temperature=0.7, max_tokens=500,)print(response["choices"][0]["message"]["content"])
Parameter
Type
Default
Description
api_key
str
None
PromptGuard API key. Falls back to PROMPTGUARD_API_KEY env var
base_url
str
None
API base URL. Defaults to https://api.promptguard.co/api/v1/proxy
Generate embeddings through the PromptGuard proxy:
Copy
pg = PromptGuard(api_key="pg_xxx")response = pg.embeddings.create( model="text-embedding-3-small", input="The quick brown fox jumps over the lazy dog",)print(response["data"][0]["embedding"][:5]) # First 5 dimensions