test: type the new guardrail test helpers with litellm's tool call types instead of Any

This commit is contained in:
mateo-berri 2026-11-06 00:12:58 -08:00
parent 47fda7eece
commit 4f97d003a9
2 changed files with 30 additions and 9 deletions

View file

@ -14,10 +14,12 @@ from openai.types.responses import ResponseFunctionWebSearch
from litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.llms.base_llm.guardrail_translation.base_translation import StreamingScanKey
from litellm.llms.openai.chat.guardrail_translation.handler import (
OpenAIChatCompletionsHandler,
)
from litellm.types.llms.openai import ChatCompletionToolCallChunk
from litellm.types.responses.main import build_web_search_call
from litellm.types.utils import (
ChatCompletionMessageToolCall,
@ -150,7 +152,9 @@ def _server_tool_call(call_id: str, name: str, arguments: Mapping[str, object])
return {"id": call_id, "type": "function", "function": {"name": name, "arguments": json.dumps(arguments)}}
def _tool_call_ids_names_and_arguments(tool_calls: Iterable[Mapping[str, Any]]) -> list[tuple[str, str, str]]:
def _tool_call_ids_names_and_arguments(
tool_calls: Iterable[ChatCompletionToolCallChunk],
) -> list[tuple[str | None, str | None, str]]:
return [
(tool_call["id"], tool_call["function"]["name"], tool_call["function"]["arguments"])
for tool_call in tool_calls
@ -176,9 +180,9 @@ class QueryRedactingGuardrail(CustomGuardrail):
inputs: GenericGuardrailAPIInputs,
request_data: dict,
input_type: Literal["request", "response"],
logging_obj: Optional[Any] = None,
logging_obj: Optional[LiteLLMLoggingObj] = None,
) -> GenericGuardrailAPIInputs:
def redacted(tool_call: Mapping[str, Any]) -> Mapping[str, Any]:
def redacted(tool_call: ChatCompletionToolCallChunk) -> ChatCompletionToolCallChunk:
arguments: Final[dict[str, object]] = json.loads(tool_call["function"]["arguments"])
redacted_arguments: Final = {
key: "[REDACTED]" if key in ("query", "url") else value for key, value in arguments.items()

View file

@ -1,8 +1,8 @@
from collections.abc import Mapping
from typing import Any, Final
from typing import Final
from unittest.mock import AsyncMock, patch
from openai.types.responses.response_function_web_search import ResponseFunctionWebSearch
from typing_extensions import ReadOnly, TypedDict
import httpx
import pytest
@ -15,6 +15,7 @@ from litellm.proxy.guardrails.guardrail_hooks.pangea.pangea import (
)
from litellm.proxy.guardrails.init_guardrails import init_guardrails_v2
from litellm.types.guardrails import GuardrailEventHooks, LitellmParams
from litellm.types.llms.openai import ChatCompletionAssistantToolCall
from litellm.types.responses.main import build_web_search_call
from litellm.types.utils import Choices, Message, ModelResponse
@ -375,7 +376,23 @@ async def test_pangea_ai_guard_response_transformed(pangea_guardrail):
assert response.choices[0]["message"]["content"] == "Yes, here is an SSN: <US_SSN>"
def _claude_fetch_call(url: str) -> dict[str, object]:
class _GuardedMessage(TypedDict):
tool_calls: ReadOnly[list[ChatCompletionAssistantToolCall]]
class _GuardedChoice(TypedDict):
message: ReadOnly[_GuardedMessage]
class _GuardedOutput(TypedDict):
choices: ReadOnly[list[_GuardedChoice]]
class _AiGuardRequest(TypedDict):
input: ReadOnly[_GuardedOutput]
def _claude_fetch_call(url: str) -> ChatCompletionAssistantToolCall:
return {
"id": "srvtoolu_01",
"type": "function",
@ -383,7 +400,7 @@ def _claude_fetch_call(url: str) -> dict[str, object]:
}
def _with_masked_tool_calls(choice: Mapping[str, Any]) -> dict[str, object]:
def _with_masked_tool_calls(choice: _GuardedChoice) -> _GuardedChoice:
message: Final = choice["message"]
masked_calls: Final = [_claude_fetch_call("<URL>") for _ in message["tool_calls"]]
return {**choice, "message": {**message, "tool_calls": masked_calls}}
@ -393,10 +410,10 @@ def _with_masked_tool_calls(choice: Mapping[str, Any]) -> dict[str, object]:
async def test_pangea_ai_guard_response_masks_claude_server_tool_call_inputs(pangea_guardrail: PangeaHandler) -> None:
data: Final = {"messages": [{"role": "user", "content": "Read https://example.com/"}]}
fetch: Final = build_web_search_call("srvtoolu_01", {"url": "https://example.com/"}, None, tool_name="web_fetch")
sent_choices: Final = []
sent_choices: Final[list[_GuardedChoice]] = []
async def _mask_tool_call_urls(
_handler: object, url: str, json: Mapping[str, Any], **_kwargs: object
_handler: object, url: str, json: _AiGuardRequest, **_kwargs: object
) -> httpx.Response:
choices: Final = json["input"]["choices"]
sent_choices.extend(choices)