Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:BerriAI Litellm Aporia AI Guardrail

From Leeroopedia
Revision as of 12:08, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/BerriAI_Litellm_Aporia_AI_Guardrail.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Attribute Value
Sources enterprise/enterprise_hooks/aporia_ai.py
Domains Guardrails, Content Safety, Enterprise Hooks
Last Updated 2026-02-15 16:00 GMT

Overview

AporiaGuardrail is a guardrail integration that validates LLM request prompts and responses against Aporia AI's content safety policies, blocking requests or responses that violate configured guardrail rules.

Description

The AporiaGuardrail class extends CustomGuardrail and integrates with the Aporia AI validation API. It provides two hook points in the LLM request lifecycle:

  • During-Call Moderation (async_moderation_hook) -- Validates the prompt messages before the LLM call. Transforms messages to Aporia-compatible roles (system, user, assistant, or "other" for unsupported roles like "tool").
  • Post-Call Validation (async_post_call_success_hook) -- Validates the LLM response alongside the original prompt. Sets validation_target to "both" when both messages and response are present.

When Aporia returns an action of "block", the guardrail raises an HTTPException with status 400 and the full Aporia response.

The guardrail name is "aporia". It respects the should_run_guardrail event hook system and legacy should_proceed_based_on_metadata for backwards compatibility.

Requires either constructor parameters or environment variables: APORIO_API_KEY and APORIO_API_BASE.

Usage

Register AporiaGuardrail as a guardrail callback in the LiteLLM proxy to enforce Aporia AI content safety policies on prompts and responses.

Code Reference

Source Location

enterprise/enterprise_hooks/aporia_ai.py

Signature

class AporiaGuardrail(CustomGuardrail):
    def __init__(self, api_key: Optional[str] = None, api_base: Optional[str] = None, **kwargs): ...
    def transform_messages(self, messages: List[dict]) -> List[dict]: ...
    async def prepare_aporia_request(self, new_messages: List[dict], response_string: Optional[str] = None) -> dict: ...
    async def make_aporia_api_request(self, new_messages: List[dict], response_string: Optional[str] = None): ...
    async def async_post_call_success_hook(self, data: dict, user_api_key_dict: UserAPIKeyAuth, response): ...
    async def async_moderation_hook(self, data: dict, user_api_key_dict: UserAPIKeyAuth, call_type: CallTypesLiteral): ...

Import

from enterprise.enterprise_hooks.aporia_ai import AporiaGuardrail

I/O Contract

Inputs

Parameter Type Description
api_key Optional[str] Aporia API key. Falls back to APORIO_API_KEY env var.
api_base Optional[str] Aporia API base URL. Falls back to APORIO_API_BASE env var.
data dict Request data containing messages list.
response LLM response object The LLM response for post-call validation.

Outputs

Output Type Description
Pass-through None Request proceeds if Aporia action is "passthrough", "modify", or "rephrase".
Block HTTPException(400) Raised when Aporia action is "block", with aporia_ai_response in detail.

Usage Examples

# In proxy config YAML
litellm_settings:
  guardrails:
    - guardrail_name: "aporia"
      litellm_params:
        guardrail: aporia
        api_key: "os.environ/APORIO_API_KEY"
        api_base: "os.environ/APORIO_API_BASE"
        mode: "during_call"
# Programmatic instantiation
from enterprise.enterprise_hooks.aporia_ai import AporiaGuardrail

guardrail = AporiaGuardrail(
    api_key="your-aporia-api-key",
    api_base="https://gr-prd-trial.aporia.com/your-id",
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment