mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-15 09:12:18 +00:00
fix(guardrails): warn only for routes with no call type, and scan compact input in Bedrock attachments and text guardrails
This commit is contained in:
parent
712f688c22
commit
8998cc434d
6 changed files with 86 additions and 9 deletions
|
|
@ -11,7 +11,7 @@ from litellm.types.proxy.guardrails.guardrail_hooks.bedrock_guardrails import (
|
|||
BedrockImageContent,
|
||||
BedrockImageSource,
|
||||
)
|
||||
from litellm.types.utils import CallTypes
|
||||
from litellm.types.utils import RESPONSES_INPUT_CALL_TYPES, CallTypes
|
||||
|
||||
BedrockImageFormat: TypeAlias = Literal["png", "jpeg"]
|
||||
|
||||
|
|
@ -53,7 +53,6 @@ _IMAGE_FORMAT_BY_MIME: Final[Mapping[str, BedrockImageFormat]] = MappingProxyTyp
|
|||
{"image/png": "png", "image/jpeg": "jpeg", "image/jpg": "jpeg"}
|
||||
)
|
||||
_CHAT_CALL_TYPES: Final = frozenset({CallTypes.completion.value, CallTypes.acompletion.value})
|
||||
_RESPONSES_CALL_TYPES: Final = frozenset({CallTypes.responses.value, CallTypes.aresponses.value})
|
||||
_OPENAI_IMAGE_TYPES: Final = frozenset({"image_url", "input_image", "computer_screenshot"})
|
||||
_OPENAI_UNSCANNABLE_TYPES: Final = frozenset(
|
||||
{"file", "input_file", "input_audio", "video_url", "audio_url", "document", "container_upload"}
|
||||
|
|
@ -138,7 +137,7 @@ def _messages_and_classifier(
|
|||
return _mappings(data.get("messages")), _classify_openai_block, _no_nested_blocks
|
||||
if call_type == CallTypes.anthropic_messages.value:
|
||||
return _mappings(data.get("messages")), _classify_anthropic_block, _anthropic_tool_result_blocks
|
||||
if call_type in _RESPONSES_CALL_TYPES:
|
||||
if call_type in RESPONSES_INPUT_CALL_TYPES:
|
||||
return _mappings(data.get("input")), _classify_openai_block, _no_nested_blocks
|
||||
if call_type == CallTypes.allm_passthrough_route.value and _is_bedrock_converse(data):
|
||||
body: Final = data.get("data")
|
||||
|
|
|
|||
|
|
@ -32,12 +32,15 @@ from litellm.responses.litellm_completion_transformation.compaction_items import
|
|||
#
|
||||
# /v1/chat/completions -> "acompletion"
|
||||
# /v1/responses -> "aresponses"
|
||||
# /v1/responses/compact -> "acompact_responses"
|
||||
#
|
||||
# ``"completion"`` is included for SDK / internal callers that invoke
|
||||
# ``pre_call_hook`` directly with the sync name. Embedding, moderation,
|
||||
# audio, and transcription endpoints are deliberately excluded — text
|
||||
# guardrails on those paths are a separate scope.
|
||||
TEXT_CONTENT_CALL_TYPES: Final[frozenset[str]] = frozenset({"completion", "acompletion", "aresponses"})
|
||||
TEXT_CONTENT_CALL_TYPES: Final[frozenset[str]] = frozenset(
|
||||
{"completion", "acompletion", "aresponses", "acompact_responses"}
|
||||
)
|
||||
|
||||
|
||||
def is_text_content_call_type(call_type: str) -> bool:
|
||||
|
|
|
|||
|
|
@ -102,12 +102,11 @@ def _as_endpoint_translation(translation: _EndpointTranslation) -> _EndpointTran
|
|||
return translation
|
||||
|
||||
|
||||
def _translated_call_type(call_type: object, mappings: Mapping[CallTypes, object]) -> CallTypes | None:
|
||||
def _known_call_type(call_type: object) -> CallTypes | None:
|
||||
try:
|
||||
known_call_type: Final = CallTypes(call_type)
|
||||
return CallTypes(call_type)
|
||||
except ValueError:
|
||||
return None
|
||||
return known_call_type if known_call_type in mappings else None
|
||||
|
||||
|
||||
def _warn_untranslated_call_type(
|
||||
|
|
@ -307,12 +306,14 @@ class UnifiedLLMGuardrails(CustomLogger):
|
|||
return data
|
||||
|
||||
mappings: Final = load_guardrail_translation_mappings()
|
||||
input_call_type: Final = _translated_call_type(call_type, mappings)
|
||||
input_call_type: Final = _known_call_type(call_type)
|
||||
if input_call_type is None:
|
||||
_warn_untranslated_call_type(
|
||||
guardrail_to_apply.guardrail_name, user_api_key_dict.request_route, call_type, "pre-call"
|
||||
)
|
||||
return data
|
||||
if input_call_type not in mappings:
|
||||
return data
|
||||
|
||||
endpoint_translation: Final = _as_endpoint_translation(mappings[input_call_type]())
|
||||
|
||||
|
|
@ -361,12 +362,14 @@ class UnifiedLLMGuardrails(CustomLogger):
|
|||
return data
|
||||
|
||||
mappings: Final = load_guardrail_translation_mappings()
|
||||
input_call_type: Final = _translated_call_type(call_type, mappings)
|
||||
input_call_type: Final = _known_call_type(call_type)
|
||||
if input_call_type is None:
|
||||
_warn_untranslated_call_type(
|
||||
guardrail_to_apply.guardrail_name, user_api_key_dict.request_route, call_type, "during-call"
|
||||
)
|
||||
return data
|
||||
if input_call_type not in mappings:
|
||||
return data
|
||||
|
||||
endpoint_translation: Final = _as_endpoint_translation(mappings[input_call_type]())
|
||||
|
||||
|
|
|
|||
|
|
@ -123,6 +123,23 @@ TEXT = {"type": "text", "text": "hello"}
|
|||
["image_url (remote URL or file id)"],
|
||||
id="responses-chat-shaped-image-url",
|
||||
),
|
||||
pytest.param(
|
||||
{
|
||||
"input": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{"type": "input_image", "image_url": f"data:image/png;base64,{PNG_B64}"},
|
||||
{"type": "input_file", "file_id": "file-1"},
|
||||
],
|
||||
}
|
||||
]
|
||||
},
|
||||
CallTypes.acompact_responses.value,
|
||||
[_png_item()],
|
||||
["input_file"],
|
||||
id="compact-input-image-and-file",
|
||||
),
|
||||
pytest.param(
|
||||
_converse({"audio": {"format": "mp3", "source": {"bytes": PDF_B64}}}),
|
||||
CallTypes.allm_passthrough_route.value,
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import anyio
|
|||
import pytest
|
||||
|
||||
import litellm
|
||||
from litellm._logging import verbose_proxy_logger
|
||||
from litellm.caching import DualCache
|
||||
from litellm.integrations.custom_guardrail import (
|
||||
CustomGuardrail,
|
||||
|
|
@ -3336,3 +3337,33 @@ async def test_a_route_without_a_call_type_passes_input_guardrails_unscanned(
|
|||
f"route '/v1/vector_stores' but call type '{route_call_type}' has no guardrail translation handler; "
|
||||
f"skipping {stage} scanning" in caplog.text
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("hook", ["async_pre_call_hook", "async_moderation_hook"])
|
||||
@pytest.mark.parametrize(
|
||||
("call_type", "request_route"),
|
||||
[("amoderation", "/v1/moderations"), ("aget_responses", "/v1/responses/resp_abc")],
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
async def test_a_call_type_without_an_input_handler_passes_input_guardrails_unscanned_without_a_warning(
|
||||
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, hook: str, call_type: str, request_route: str
|
||||
) -> None:
|
||||
_patch_translation_mappings(monkeypatch, load_guardrail_translation_mappings())
|
||||
guardrail: Final = RecordingGuardrail()
|
||||
data: Final = {"model": "gpt-4o", "input": "Remember forbiddenword", "guardrail_to_apply": guardrail}
|
||||
user_api_key_dict: Final = UserAPIKeyAuth(token="hashed-key", request_route=request_route)
|
||||
|
||||
with caplog.at_level(logging.WARNING, logger=verbose_proxy_logger.name):
|
||||
result: Final = (
|
||||
await UnifiedLLMGuardrails().async_pre_call_hook(
|
||||
user_api_key_dict=user_api_key_dict, cache=DualCache(), data=data, call_type=call_type
|
||||
)
|
||||
if hook == "async_pre_call_hook"
|
||||
else await UnifiedLLMGuardrails().async_moderation_hook(
|
||||
data=data, user_api_key_dict=user_api_key_dict, call_type=call_type
|
||||
)
|
||||
)
|
||||
|
||||
assert guardrail.apply_calls == []
|
||||
assert result == {"model": "gpt-4o", "input": "Remember forbiddenword"}
|
||||
assert [r.getMessage() for r in caplog.records if r.name == verbose_proxy_logger.name] == []
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ from dotenv import load_dotenv
|
|||
load_dotenv()
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
import litellm
|
||||
from litellm.proxy.enterprise.enterprise_hooks.banned_keywords import (
|
||||
ENTERPRISE_BannedKeywords,
|
||||
|
|
@ -58,3 +60,25 @@ async def test_banned_keywords_check():
|
|||
)
|
||||
except Exception as e:
|
||||
pytest.fail(f"An error occurred - {str(e)}")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("call_type", ["aresponses", "acompact_responses"])
|
||||
@pytest.mark.asyncio
|
||||
async def test_banned_keywords_block_responses_and_compact_input(
|
||||
call_type: str, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setattr(litellm, "banned_keywords_list", ["forbiddenword"])
|
||||
|
||||
with pytest.raises(HTTPException) as raised:
|
||||
await ENTERPRISE_BannedKeywords().async_pre_call_hook(
|
||||
user_api_key_dict=UserAPIKeyAuth(api_key=hash_token("sk-98765")),
|
||||
cache=DualCache(),
|
||||
call_type=call_type,
|
||||
data={
|
||||
"model": "gpt-4o",
|
||||
"input": [{"role": "user", "content": [{"type": "input_text", "text": "Remember forbiddenword"}]}],
|
||||
},
|
||||
)
|
||||
|
||||
assert raised.value.status_code == 400
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue