Repository navigation
feat(guardrails): add Compresr guardrail for query-aware context compression #33295
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
yucheng-berri
merged 10 commits into
litellm_internal_staging
from
litellm_compresr_guardrail
Jul 15, 2026
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4ca9092
feat(guardrails): add Compresr guardrail for query-aware context comp…
charafkamel 46009ef
fix(guardrails): cap Compresr recovery store total memory
charafkamel f016bb8
fix(guardrails): gate and bound Compresr recovery loop
charafkamel 8ef0205
fix(guardrails): warn when Compresr recovery is skipped without auth …
charafkamel bfd6ca8
style(guardrails): tighten Compresr guardrail comments
charafkamel c573c8b
fix(guardrails): keep injected tool on Responses API + bound recovery…
charafkamel 89417ef
refactor(guardrails): extract _existing_originals to keep apply_guard…
charafkamel 8ddf97f
fix(guardrails): harden Compresr SSRF blocklist, re-arm no-scope warn…
yucheng-berri 186ba04
fix(guardrails): rerun input guardrails on Compresr retrieval follow-up
yucheng-berri 9662a95
chore: remove unrelated deepkeep files committed by mistake
yucheng-berri 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
75 changes: 75 additions & 0 deletions
75
litellm/proxy/guardrails/guardrail_hooks/compresr/__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,75 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from litellm.types.guardrails import ( | ||
| GuardrailEventHooks, | ||
| Mode, | ||
| SupportedGuardrailIntegrations, | ||
| ) | ||
|
|
||
| from .compresr import CompresrGuardrail | ||
|
|
||
| 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 _get_optional_value(litellm_params: LitellmParams, optional_params: object | None, attribute_name: str) -> object: | ||
| if optional_params is not None: | ||
| value = getattr(optional_params, attribute_name, None) | ||
| if value is not None: | ||
| return value | ||
| return getattr(litellm_params, attribute_name, None) | ||
|
|
||
|
|
||
| def initialize_guardrail(litellm_params: LitellmParams, guardrail: Guardrail) -> CompresrGuardrail: | ||
| import litellm | ||
|
|
||
| optional_params = getattr(litellm_params, "optional_params", None) | ||
|
|
||
| _callback = CompresrGuardrail( | ||
| api_base=litellm_params.api_base, | ||
| api_key=litellm_params.api_key, | ||
| model=litellm_params.model, | ||
| target_compression_ratio=_get_optional_value(litellm_params, optional_params, "target_compression_ratio"), | ||
| coarse=_get_optional_value(litellm_params, optional_params, "coarse"), | ||
| min_chars_to_compress=_get_optional_value(litellm_params, optional_params, "min_chars_to_compress"), | ||
| compress_tool_outputs=_get_optional_value(litellm_params, optional_params, "compress_tool_outputs"), | ||
| compress_system=_get_optional_value(litellm_params, optional_params, "compress_system"), | ||
| compress_history=_get_optional_value(litellm_params, optional_params, "compress_history"), | ||
| compress_last_user=_get_optional_value(litellm_params, optional_params, "compress_last_user"), | ||
| enable_retrieval=_get_optional_value(litellm_params, optional_params, "enable_retrieval"), | ||
| max_bytes_per_call=_get_optional_value(litellm_params, optional_params, "max_bytes_per_call"), | ||
| allow_bypass_header=_get_optional_value(litellm_params, optional_params, "allow_bypass_header"), | ||
| dynamic=_get_optional_value(litellm_params, optional_params, "dynamic"), | ||
| dynamic_min_ratio=_get_optional_value(litellm_params, optional_params, "dynamic_min_ratio"), | ||
| dynamic_max_ratio=_get_optional_value(litellm_params, optional_params, "dynamic_max_ratio"), | ||
| compression_params=_get_optional_value(litellm_params, optional_params, "compression_params"), | ||
| guardrail_name=guardrail["guardrail_name"], | ||
| event_hook=_coerce_event_hook(litellm_params.mode), | ||
| default_on=litellm_params.default_on or False, | ||
| unreachable_fallback=litellm_params.unreachable_fallback, | ||
| ) | ||
| litellm.logging_callback_manager.add_litellm_callback( # pyright: ignore[reportUnknownMemberType] # callback manager is untyped | ||
| _callback | ||
| ) | ||
| return _callback | ||
|
|
||
|
|
||
| guardrail_initializer_registry = { | ||
| SupportedGuardrailIntegrations.COMPRESR.value: initialize_guardrail, | ||
| } | ||
|
|
||
| guardrail_class_registry = { | ||
| SupportedGuardrailIntegrations.COMPRESR.value: CompresrGuardrail, | ||
| } |
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.
Can I get more context on this? Why do we need to modify the base guardrail translation handler for this integration?
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.
I think this change the base handler because it is the code that rebuilds the tool list after a guardrail runs, and it currently drops the
compresr_retrievetool that Compresr adds.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.
do you have any concerns
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.
the potential blast radius for this PR now much higher.
Have you check if this would affect any other guardrail integration as well as our custom one?
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.
This is an existing pattern with /chat/completions and /messages, where the full tool list returned by the guardrail is preserved.