Repository navigation
feat(guardrails): forward metadata through apply_guardrail endpoint - #33060
Isaac4real wants to merge 2 commits into
Conversation
Greptile SummaryThis PR adds an optional
Confidence Score: 4/5Safe to merge; the change is small, additive, and fully backward-compatible. The logic change is minimal and well-tested. The only notable inconsistency is that The
|
| Filename | Overview |
|---|---|
| litellm/types/guardrails.py | Adds optional metadata: Optional[Dict[str, Any]] = None field to ApplyGuardrailRequest; clean, backward-compatible addition |
| litellm/proxy/guardrails/guardrail_endpoints.py | Refactors request_data construction to separately set messages and metadata keys; minor inconsistency in null-checking style between the two fields |
| tests/enterprise/litellm_enterprise/proxy/guardrails/test_apply_guardrail_endpoint.py | Adds a well-structured mock test that verifies metadata forwarding end-to-end through the endpoint; all network calls are mocked |
Reviews (1): Last reviewed commit: "feat(guardrails): forward metadata throu..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 1 · PR risk: 0/10 |
Add optional metadata to ApplyGuardrailRequest so parameterized custom guardrails can receive per-request configuration via POST /guardrails/apply_guardrail. Fixes BerriAI#33059
Route caller metadata through request processing so authenticated fields take precedence, and preserve explicitly empty message lists
d19d455 to
bd84a85
Compare
|
closed in #33067 |
Title
Support parameterized guardrails on POST /guardrails/apply_guardrail via optional metadata
Relevant issues
Fixes #33059
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unitType
🆕 New Feature
Changes
Parameterized custom guardrails need per-request configuration when invoked through
POST /guardrails/apply_guardrail, outside the normal LLM call path. The endpoint previously had no request field for guardrail-specific parameters, and Pydantic drops unknown JSON fieldsThis PR adds optional
metadatatoApplyGuardrailRequest. Caller metadata enters the existing common request-processing pipeline before reaching the guardrail, which strips untrusteduser_api_key_*values and adds authenticated identity fields. The endpoint only includes metadata inrequest_datawhen the client provided it, preserving the previous behavior when omitted. Explicitly emptymessageslists are also preservedThe endpoint tests verify metadata forwarding after server processing, authenticated metadata precedence, backward-compatible omission, and empty-message preservation
Example request:
{ "guardrail_name": "my-topic-guardrail", "text": "What are tax loopholes?", "metadata": { "forbidden_topics": ["taxes", "finance"] } }Custom guardrail implementations that override
apply_guardrailare responsible for readingrequest_data["metadata"]