Overview
With the OpenLIT SDK, you can set up guardrails to keep your apps safe by handling tricky or risky prompts sent to AI models. We offer five main guardrails:All
Prompt Injection
Sensitive Topics
Topic Restriction
PII / Data Redaction
Guardrails
Prompt Injection
Detects and prevents attempts to manipulate AI behavior through malicious inputs, including injection and jailbreak attempts. Opt for advanced detection using a Language Model (LLM) by specifying a provider and API key, or choose regex-based detection by providing custom rules without an LLM.#### How to UseUsage
- LLM-Based Detection (Python)
- LLM-Based Detection (TypeScript)
- Regex-Based Detection (Python)
- Regex-Based Detection (TypeScript)
base_url with provider="openai" to use any provider that is compatible with the OpenAI SDK.Supported Providers and LLMs
OpenAI
OpenAI
Anthropic
Anthropic
Parameters
`openlit.guard.PromptInjection()` Class Parameters
`openlit.guard.PromptInjection()` Class Parameters
PromptInjection class:`detect` Method Parameters
`detect` Method Parameters
detect method to analyze a specific text:Classification Categories
Categories
Categories
How it Works
Explanation
Explanation
- Input Gathering: Takes the text, categories, and optional custom rules or categories.
-
Detection Choice:
- Regex Detection: Uses regex if no provider is specified but custom rules are provided.
- LLM Detection: Uses an LLM (OpenAI or Anthropic) if a provider is specified.
-
Evaluation:
- Regex: Applies custom rules to find prompt injections.
- LLM: Sends a structured prompt to the LLM for analysis.
- JSON Output: Returns results with a score, verdict (“yes” or “no”), guard type, classification, and a brief explanation.
JSON Output:
Output
Output
- Score: Reflects the likelihood of prompt injection.
- Verdict: “yes” if injection detected (score above threshold), “no” otherwise.
- Guard: Marks the type of detection (“prompt_injection”).
- Classification: Indicates the specific type of prompt injection detected.
- Explanation: Offers a brief reason for the classification.
Sensitive Topics
Detects and flags discussions on potentially controversial or harmful subjects. Choose advanced detection using a Language Model (LLM) or apply regex-based detection by specifying custom rules without an LLM.Usage
- LLM-Based Detection (Python)
- LLM-Based Detection (TypeScript)
- Regex-Based Detection (Python)
- Regex-Based Detection (TypeScript)
base_url with provider="openai" to use any provider compatible with the OpenAI SDK.Supported Providers and LLMs
OpenAI
OpenAI
Anthropic
Anthropic
Parameters
`openlit.guard.SensitiveTopic()` Class Parameters
`openlit.guard.SensitiveTopic()` Class Parameters
SensitiveTopic class:`detect` Method Parameters
`detect` Method Parameters
detect method to analyze a specific text:Classification Categories
Categories
Categories
How it Works
Explanation
Explanation
- Input Gathering: Collects text, categories, and optional custom rules or categories.
-
Detection Choice:
- Regex Detection: Uses regex if no provider is specified but custom rules are available.
- LLM Detection: Utilizes an LLM (OpenAI or Anthropic) if a provider is specified.
-
Evaluation:
- Regex: Applies custom rules to identify sensitive topics.
- LLM: Sends a structured prompt to the LLM for evaluation.
- JSON Output: Provides results with a score, verdict (“yes” or “no”), guard type, classification, and a brief explanation.
JSON Output:
Output
Output
- Score: Indicates the likelihood of a sensitive topic.
- Verdict: “yes” if a sensitive topic is detected (score above threshold), “no” otherwise.
- Guard: Identifies the type of detection (“sensitive_topic”).
- Classification: Displays the specific type of sensitive topic detected.
- Explanation: Provides a concise reason for the classification.
Topic Restriction
Ensures that prompts are focused solely on approved subjects by validating against lists of valid and invalid topics. This guardrail helps maintain conversations within desired boundaries in AI interactions.Usage
- LLM-Based Detection (Python)
- LLM-Based Detection (TypeScript)
Supported Providers and LLMs
OpenAI
OpenAI
Anthropic
Anthropic
Parameters
`openlit.guard.TopicRestriction()` Class Parameters
`openlit.guard.TopicRestriction()` Class Parameters
TopicRestriction class:`detect` Method Parameters
`detect` Method Parameters
detect method to analyze a specific text:Classification Categories
Categories
Categories
How it Works
Explanation
Explanation
- Input Gathering: Collects text and lists of valid and invalid topics.
- Prompt Creation: Constructs a system prompt that includes specified valid and invalid topics for the LLM to assess.
- LLM Evaluation: Utilizes the LLM (OpenAI or Anthropic) to evaluate the text against the provided topic constraints.
- JSON Output: Provides results with a score, verdict (“yes” or “no”), guard type, classification, and a brief explanation.
JSON Output:
Output
Output
- Score: Indicates the likelihood of the text being classified as an invalid topic.
- Verdict: “yes” if the text fits an invalid topic (score above threshold), “no” otherwise.
- Guard: Identifies the type of detection (“topic_restriction”).
- Classification: Displays whether the text is a “valid_topic” or “invalid_topic”.
- Explanation: Provides a concise reason for the classification.
All Detector
Detects issues related to prompt injections, ensures conversations stay on valid topics, and flags sensitive subjects. You can choose to use Language Model (LLM) detection with specified providers or apply regex-based detection using custom rules.Usage
- LLM-Based Detection
- Regex-Based Detection
base_url with provider="openai" to use any provider compatible with the OpenAI SDK.Supported Providers and LLMs
OpenAI
OpenAI
Anthropic
Anthropic
Parameters
`openlit.guard.All()` Class Parameters
`openlit.guard.All()` Class Parameters
All class:`detect` Method Parameters
`detect` Method Parameters
detect method to analyze a specific text:Classification Categories
Prompt Injection
Prompt Injection
Valid/Invalid Topics
Valid/Invalid Topics
Sensitive Topics
Sensitive Topics
How it Works
Explanation
Explanation
- Input Gathering: Collects text, categories, and optional custom rules or categories.
-
Detection Choice:
- Regex Detection: Uses regex if no provider is specified but custom rules are provided.
- LLM Detection: Utilizes an LLM (OpenAI or Anthropic) if a provider is specified.
-
Evaluation:
- Regex: Applies custom rules to detect prompt injections, topic restrictions, and sensitive topics.
- LLM: Sends a structured prompt to the LLM for thorough analysis.
- JSON Output: Provides results with a score, verdict (“yes” or “no”), guard type, classification, and a brief explanation.
JSON Output:
Output
Output
- Score: Indicates the likelihood of an issue being present.
- Verdict: “yes” if an issue is detected (score above threshold), “no” otherwise.
- Guard: Identifies the type of detection (“prompt_injection”, “topic_restriction”, or “sensitive_topic”).
- Classification: Displays the specific type of issue detected.
- Explanation: Provides a concise reason for the classification.
PII / Data Redaction
Detects and redacts secrets, credentials, and personal data - API keys (OpenAI, Anthropic, AWS, GCP, GitHub, Stripe, Slack, and more), emails, phone numbers, SSNs, credit card numbers, IP addresses, bearer/basic auth headers, private keys, connection strings, and genericpassword=/secret= patterns - using built-in regex patterns, with support for your own custom patterns.
Unlike the other guardrails, which return a yes/no verdict for you to act on, the PII guard is wired directly into openlit.init() and runs automatically around every instrumented LLM call - no separate detect() call needed.
Usage
action="redact", matched text is replaced with [REDACTED:<label>] (e.g. [REDACTED:email]) both in the outgoing request and in the captured telemetry - so sensitive values never leave your stack or land in a trace.
Parameters
`openlit.PII()` Class Parameters
`openlit.PII()` Class Parameters
How it works
Explanation
Explanation
- Preflight: Before an instrumented LLM call is made, the guard scans the outgoing prompt/messages. If
action="redact", matches are replaced before the request is sent - so the redacted text is what the model (and your telemetry) actually sees. - Postflight: After a non-streaming response returns, the guard scans response content (e.g.
choices[].message.content) the same way, on a best-effort basis. - Combine with other guardrails:
PIIcan be combined withPromptInjection,SensitiveTopic,TopicRestriction, andModerationby passing multiple guards in theguards=[...]list - each runs as part of the same pipeline.

