Skip to content

feat(guardrails): forward metadata through apply_guardrail endpoint - #33060

Closed
Isaac4real wants to merge 2 commits into
BerriAI:litellm_oss_daily_2026_07_10from
Isaac4real:feat/33059-parameterized-apply-guardrail-metadata
Closed

Isaac4real wants to merge 2 commits into
BerriAI:litellm_oss_daily_2026_07_10from
Isaac4real:feat/33059-parameterized-apply-guardrail-metadata

Conversation

@Isaac4real

@Isaac4real Isaac4real commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

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

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally:
image
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🆕 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 fields

This PR adds optional metadata to ApplyGuardrailRequest. Caller metadata enters the existing common request-processing pipeline before reaching the guardrail, which strips untrusted user_api_key_* values and adds authenticated identity fields. The endpoint only includes metadata in request_data when the client provided it, preserving the previous behavior when omitted. Explicitly empty messages lists are also preserved

The 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_guardrail are responsible for reading request_data["metadata"]

@greptile-apps

greptile-apps Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds an optional metadata field to ApplyGuardrailRequest and forwards it into request_data when calling apply_guardrail, enabling parameterized guardrails (e.g. topic-based guardrails with forbidden_topics) to receive per-request configuration through the POST /guardrails/apply_guardrail endpoint.

  • litellm/types/guardrails.py: Adds metadata: Optional[Dict[str, Any]] = None to ApplyGuardrailRequest; fully backward-compatible since it defaults to None.
  • litellm/proxy/guardrails/guardrail_endpoints.py: Refactors request_data construction to include metadata when the client provides it; existing messages handling is preserved.
  • tests/enterprise/.../test_apply_guardrail_endpoint.py: Adds a mock-only test verifying that a metadata payload is correctly forwarded into the request_data argument of apply_guardrail.

Confidence Score: 4/5

Safe 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 messages is checked with a truthiness test while metadata uses an identity check, meaning an explicitly-sent empty messages list would be silently dropped while an empty metadata dict would be forwarded — a subtle behavioral asymmetry that could confuse future callers.

The request_data construction block in guardrail_endpoints.py around lines 2241–2245 deserves a second look for the null-check inconsistency.

Important Files Changed

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

codecov Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@veria-ai

veria-ai Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

@codspeed

codspeed Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing Isaac4real:feat/33059-parameterized-apply-guardrail-metadata (bd84a85) with main (10d5804)1

Open in CodSpeed

Footnotes

  1. No successful run was found on litellm_oss_daily_2026_07_10 (3d63eda) during the generation of this report, so main (10d5804) was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

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
@Isaac4real
Isaac4real force-pushed the feat/33059-parameterized-apply-guardrail-metadata branch from d19d455 to bd84a85 Compare July 13, 2026 09:17
@Isaac4real
Isaac4real changed the base branch from main to litellm_oss_daily_2026_07_10 July 13, 2026 09:17
@Isaac4real

Copy link
Copy Markdown
Contributor Author

closed in #33067

@Isaac4real Isaac4real closed this Jul 16, 2026
@Isaac4real
Isaac4real deleted the feat/33059-parameterized-apply-guardrail-metadata branch July 16, 2026 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant