-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
feat(guardrails): add headroom guardrail for message compression #31407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
krrish-berri-2
merged 23 commits into
litellm_internal_staging
from
litellm_headroom_guardrail
Jun 27, 2026
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a6943ed
feat(guardrails): add headroom guardrail for message compression
krrish-berri-2 4534060
fix(guardrails/headroom): add @log_guardrail_information to populate …
krrish-berri-2 1787704
style: fix ruff format violations
krrish-berri-2 47759cb
fix(lint): replace deprecated typing aliases with builtin generics (U…
krrish-berri-2 19b56b8
fix(guardrails): only write back structured_messages when guardrail a…
krrish-berri-2 3c528a1
fix(guardrails/headroom): raise 502 when compression returns empty me…
krrish-berri-2 633d1db
fix(guardrails/headroom): catch transport errors and fix stale debug log
krrish-berri-2 2ccd027
fix(guardrails/anthropic): strip system messages before anthropic_mes…
krrish-berri-2 f46bdcf
fix(guardrails/anthropic): strip cache_control from thinking blocks a…
krrish-berri-2 41a1f37
debug(headroom): add INFO logging to trace guardrail execution
krrish-berri-2 225a556
debug(headroom): use print() for immediate visibility
krrish-berri-2 c7c1d28
debug(headroom): print request_data keys to diagnose metadata dict mi…
krrish-berri-2 7563aac
fix(guardrails/anthropic): propagate guardrail info to logging_obj.me…
krrish-berri-2 b84db5a
fix: use model_call_details litellm_params metadata on Logging object
krrish-berri-2 75fbd64
fix(guardrails/anthropic): write guardrail info to litellm_params att…
krrish-berri-2 a1d5e1f
fix: read slg_info from litellm_metadata when metadata key absent
krrish-berri-2 2ff1f2f
fix: write slg_info to both litellm_params attr and model_call_detail…
krrish-berri-2 7d99315
chore: remove debug prints; fix now verified end-to-end
krrish-berri-2 caf42dc
refactor(guardrails): move spend-log sync to shared helper in custom_…
krrish-berri-2 581dde2
fix(lint): reduce _sync_guardrail_info_to_logging_obj complexity belo…
krrish-berri-2 f91ac7b
fix(lint): simplify _sync_guardrail_info_to_logging_obj to reduce McC…
krrish-berri-2 b315b27
fix(lint): extract _append_slg_to_litellm_params to reduce McCabe com…
krrish-berri-2 bc8c1bf
fix(lint): extract _write_back_structured_messages to reduce process_…
krrish-berri-2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
litellm/proxy/guardrails/guardrail_hooks/headroom/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from litellm.types.guardrails import ( | ||
| GuardrailEventHooks, | ||
| Mode, | ||
| SupportedGuardrailIntegrations, | ||
| ) | ||
|
|
||
| from .headroom import HeadroomGuardrail | ||
|
|
||
| if TYPE_CHECKING: | ||
| from litellm.types.guardrails import Guardrail, LitellmParams | ||
|
|
||
|
|
||
| def _coerce_event_hook( | ||
| mode: str | list[str] | Mode, | ||
| ) -> GuardrailEventHooks | list[GuardrailEventHooks] | Mode: | ||
| if isinstance(mode, Mode): | ||
| return mode | ||
| if isinstance(mode, list): | ||
| return [GuardrailEventHooks(item) for item in mode] | ||
| return GuardrailEventHooks(mode) | ||
|
|
||
|
|
||
| def initialize_guardrail( | ||
| litellm_params: LitellmParams, guardrail: Guardrail | ||
| ) -> HeadroomGuardrail: | ||
| import litellm | ||
|
|
||
| _callback = HeadroomGuardrail( | ||
| api_base=litellm_params.api_base, | ||
| api_key=litellm_params.api_key, | ||
| model=litellm_params.model, | ||
| guardrail_name=guardrail["guardrail_name"], | ||
| event_hook=_coerce_event_hook(litellm_params.mode), | ||
| default_on=litellm_params.default_on or False, | ||
| ) | ||
| litellm.logging_callback_manager.add_litellm_callback( # pyright: ignore[reportUnknownMemberType] | ||
| _callback | ||
| ) | ||
| return _callback | ||
|
|
||
|
|
||
| guardrail_initializer_registry = { | ||
| SupportedGuardrailIntegrations.HEADROOM.value: initialize_guardrail, | ||
| } | ||
|
|
||
| guardrail_class_registry = { | ||
| SupportedGuardrailIntegrations.HEADROOM.value: HeadroomGuardrail, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert this whitespace-only change.
This PR removes a blank line in
get_standard_logging_object_payload; it is unrelated to headroom or guardrail logging sync.Change: restore the blank line so this file has zero diff vs base:
) + # emit_standard_logging_payload(payload) - Moved to success_handler to prevent double emittingKeeps the PR scope clean and avoids noisy merge conflicts on an unrelated 6k-line file.