Files
litellm/litellm/llms/pass_through/guardrail_translation/README.md
Ishaan Jaff d612d71ef4 [Feat] Add guardrails for pass through endpoints (#17221)
* add PassThroughGuardrailsConfig

* init JsonPathExtractor

* feat PassthroughGuardrailHandler

* feat pt guardrails

* pt guardrails

* add Pass-Through Endpoint Guardrail Translation

* add PassThroughEndpointHandler

* execute simple guardrail config and dict settings

* TestPassthroughGuardrailHandlerNormalizeConfig

* add passthrough_guardrails_config on litellm logging obj

* add LiteLLMLoggingObj to base trasaltino

* cleaner _get_guardrail_settings

* update guardrails settings

* docs pt guardrail

* docs Guardrails on Pass-Through Endpoints

* fix typing

* fix typing

* test_no_fields_set_sends_full_body

* fix typing

* Potential fix for code scanning alert no. 3834: Clear-text logging of sensitive information

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
2025-11-27 12:06:53 -08:00

1.7 KiB

Pass-Through Endpoint Guardrail Translation

Why This Exists Here

This module is located under litellm/llms/ (instead of with the main passthrough code) because:

  1. Auto-discovery: The load_guardrail_translation_mappings() function in litellm/llms/__init__.py scans for guardrail_translation/ directories under litellm/llms/
  2. Consistency: All other guardrail translation handlers follow this pattern (e.g., openai/chat/guardrail_translation/, anthropic/chat/guardrail_translation/)

Main Passthrough Implementation

The main passthrough endpoint implementation is in:

litellm/proxy/pass_through_endpoints/
├── pass_through_endpoints.py      # Core passthrough routing logic
├── passthrough_guardrails.py      # Guardrail collection and field targeting
├── jsonpath_extractor.py          # JSONPath field extraction utility
└── ...

What This Handler Does

The PassThroughEndpointHandler enables guardrails to run on passthrough endpoint requests by:

  1. Field Targeting: Extracts specific fields from the request/response using JSONPath expressions configured in request_fields / response_fields
  2. Full Payload Fallback: If no field targeting is configured, processes the entire payload
  3. Config Access: Uses get_passthrough_guardrails_config() / set_passthrough_guardrails_config() helpers to access the passthrough guardrails configuration stored in request metadata

Example Config

passthrough_endpoints:
  - path: "/v1/rerank"
    target: "https://api.cohere.com/v1/rerank"
    guardrails:
      bedrock-pre-guard:
        request_fields: ["query", "documents[*].text"]
        response_fields: ["results[*].text"]