Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ def _merge_tools_after_guardrail(
"""
Merge remapped guardrailed tools with original tools that were not sent
to the guardrail (e.g. web_search, web_search_preview), preserving order.
Tools a guardrail appended (``remapped`` longer than ``original_tools``)
have no original slot and are kept so an injected tool is not dropped.
"""
if not original_tools:
return remapped
Expand All @@ -262,6 +264,8 @@ def _merge_tools_after_guardrail(
if j < len(remapped):
result.append(remapped[j])
j += 1
# Keep guardrail-appended tools that matched no original slot above.
result.extend(remapped[j:])

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.

Can I get more context on this? Why do we need to modify the base guardrail translation handler for this integration?

Copy link
Copy Markdown
Contributor Author

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_retrieve tool that Compresr adds.

Copy link
Copy Markdown
Contributor Author

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

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.

the potential blast radius for this PR now much higher.

rebuilds the tool list after a guardrail runs

Have you check if this would affect any other guardrail integration as well as our custom one?

Copy link
Copy Markdown
Contributor Author

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.

  1. /v1/chat/completions — litellm/llms/openai/chat/guardrail_translation/handler.py:133-135
  2. /v1/messages (Anthropic) — litellm/llms/anthropic/chat/guardrail_translation/handler.py:332-342

return result

def _apply_guardrailed_tools_to_data(
Expand Down
75 changes: 75 additions & 0 deletions litellm/proxy/guardrails/guardrail_hooks/compresr/__init__.py
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,
}
Loading
Loading