Skip to content

fix(guardrails): read per-request extra_body from the proxy request in the generic guardrail api - #52438

Merged
narmstrong-berri merged 2 commits into
mainfrom
nate_fix_generic_guardrail_extra_body
Nov 19, 2026
Merged

narmstrong-berri merged 2 commits into
mainfrom
nate_fix_generic_guardrail_extra_body

Conversation

@narmstrong-berri

@narmstrong-berri narmstrong-berri commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

The helper for getting dynamic request body parameters expects the request_data, not the 'body' key, which will always be missing.

Details

Link to Devin session: https://app.devin.ai/sessions/adba21fa9e7a4483b813c98b2ac8349b
Open in Devin Desktop: https://app.devin.ai/desktop/session/adba21fa9e7a4483b813c98b2ac8349b?variant=devin
Requested by: @narmstrong-berri

TLDR

Problem this solves:

  • Per-request extra_body never reaches a generic guardrail service
  • It was looked up under a body key proxy requests lack

How it solves it:

  • Look the guardrail entry up in the request itself
  • Same lookup every other guardrail integration already does

User Flow

Before: a developer tuning a generic guardrail per request sees their settings ignored

  1. The admin configures a generic guardrail API guardrail, here named my-guard
  2. The developer sends POST https://litellm-domain/v1/chat/completions with "guardrails": [{"my-guard": {"extra_body": {"threshold": 0.7}}}]
  3. The guardrail service receives only the configured params, with no threshold

After: the guardrail service receives the per-request settings

  1. The admin configures the same guardrail
  2. The developer sends the same request
  3. The guardrail service receives threshold: 0.7 in additional_provider_specific_params, next to the configured params

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

E2E exception at e6ce763

Scope is one call in GenericGuardrailAPI.apply_guardrail (litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py). It passed request_data.get("body") or {} to get_guardrail_dynamic_request_body_params, but no caller in the repo puts a body key in request_data, so the lookup always saw an empty dict. It now passes request_data, which is what Lakera, Noma, Presidio and Gray Swan already pass to the same method. That method reads the guardrails list from the request root, else from metadata and litellm_metadata, so pre-call and post-call request shapes both resolve. The premium gate on dynamic extra_body is unchanged.

The bug was found by the Dec 1 integration contract tests (draft PR #50853). The test skipped with BUG for it is tests/integration/observability/test_dec1_guardrails_modes.py::test_a_dict_entry_passes_its_extra_body_to_the_guardrail_service.

The new regression test test_per_request_extra_body_from_a_proxy_request_reaches_the_guardrail_service calls apply_guardrail with a proxy-shaped request naming the guardrail with {"extra_body": {"success_threshold": 0.9}}, against a mocked guardrail endpoint, and checks the body sent carries it in additional_provider_specific_params. It failed before the fix (the service got {}) and passes at the tip.

PYTHONPATH=. python -m pytest tests/unit/proxy/guardrails/guardrail_hooks/test_generic_guardrail_api.py::test_per_request_extra_body_from_a_proxy_request_reaches_the_guardrail_service -q
# before the fix: 1 failed ({} != {'success_threshold': 0.9})
# at the tip: 1 passed

PYTHONPATH=. python -m pytest tests/unit/proxy/guardrails/guardrail_hooks/test_generic_guardrail_api.py -q
# at the tip: 127 passed

ruff format --check litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py   # already formatted
ruff check litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py   # all checks passed
ruff check --config .github/linters/ruff-tests.toml tests/unit/proxy/guardrails/guardrail_hooks/test_generic_guardrail_api.py   # all checks passed

Type

Bug Fix

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Generated by Claude Code

@veria-ai

veria-ai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

Copy link
Copy Markdown
Contributor Author

[AI] @greptileai review


Generated by Claude Code

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Devin Review: 1 flag

Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

@greptile-apps

greptile-apps Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change affects per-request settings in the generic guardrail workflow.

The code fix looks sound, but the test must meet the repository's dependency injection requirement before merging.

Findings

  1. P2 Test bypasses dependency injection ▶

Summary

The fix lets generic guardrails receive per-request settings from the proxy request.

  • Per-request guardrail settings reach the generic guardrail service.

The new test should use the existing constructor argument to meet the repository's dependency injection requirement.

Reviews (1) · Last reviewed commit: "fix(guardrails): read per-request extra_..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 45 untouched benchmarks


Comparing nate_fix_generic_guardrail_extra_body (6696d38) with main (bb29e9a)1

Open in CodSpeed

Footnotes

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

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...oks/generic_guardrail_api/generic_guardrail_api.py 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@narmstrong-berri
narmstrong-berri force-pushed the nate_fix_generic_guardrail_extra_body branch 2 times, most recently from e6ce763 to 6696d38 Compare November 18, 2026 21:13
@narmstrong-berri
narmstrong-berri force-pushed the nate_fix_generic_guardrail_extra_body branch from 6696d38 to 66b3a3d Compare November 19, 2026 01:01
@narmstrong-berri narmstrong-berri self-assigned this Nov 19, 2026
@narmstrong-berri
narmstrong-berri enabled auto-merge (squash) November 19, 2026 01:01
@devin-ai-integration

Copy link
Copy Markdown
Contributor

At 66b3a3d, generic live-proxy A/B passed with a local policy service and real LLM calls. DeepKeep fixture passed; vendor credentials unavailable. Evidence

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ narmstrong-berri
❌ nate


nate seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@narmstrong-berri
narmstrong-berri merged commit 8695a38 into main Nov 19, 2026
51 of 52 checks passed
@narmstrong-berri
narmstrong-berri deleted the nate_fix_generic_guardrail_extra_body branch November 19, 2026 21:00
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.

3 participants