diff --git a/litellm/proxy/guardrails/guardrail_hooks/presidio.py b/litellm/proxy/guardrails/guardrail_hooks/presidio.py index cf5a9913deb..541b86c3ce2 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/presidio.py +++ b/litellm/proxy/guardrails/guardrail_hooks/presidio.py @@ -12,16 +12,25 @@ import asyncio import json import re import threading -from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator, Awaitable, Iterator, Mapping, Sequence +from collections.abc import ( + AsyncGenerator, + AsyncIterable, + AsyncIterator, + Awaitable, + Callable, + Iterator, + Mapping, + Sequence, +) from contextlib import asynccontextmanager from dataclasses import dataclass from datetime import datetime -from itertools import accumulate, chain, count, pairwise +from itertools import accumulate, chain, count, pairwise, repeat from operator import itemgetter from typing import TYPE_CHECKING, Any, Final, Literal, Optional, Protocol, TypeAlias, cast import aiohttp -from pydantic import ConfigDict, JsonValue, TypeAdapter, with_config +from pydantic import ConfigDict, JsonValue, TypeAdapter, ValidationError, with_config from typing_extensions import NotRequired, ReadOnly, TypedDict import litellm @@ -282,6 +291,37 @@ def _set_tool_call_arguments(tool_call: _ToolCall, arguments: str) -> None: pass +_JSON_VALUE: Final[TypeAdapter[JsonValue]] = TypeAdapter(JsonValue) + + +async def _rewritten_json_strings(value: JsonValue, rewrite: Callable[[str], Awaitable[str]]) -> JsonValue: + match value: + case str(): + return await rewrite(value) + case list(): + return [await _rewritten_json_strings(item, rewrite) for item in value] + case dict(): + return {key: await _rewritten_json_strings(item, rewrite) for key, item in value.items()} + case _: + return value + + +async def _rewritten_arguments(arguments: str, rewrite: Callable[[str], Awaitable[str]]) -> str: + """ + Rewrite each string of a tool call's JSON arguments on its own. An entity recognizer + finds a bare value that it misses inside JSON quoting, and a restored value with a + quote or a newline has to be escaped for the arguments to stay valid JSON. + """ + try: + parsed: Final = _JSON_VALUE.validate_json(arguments) + except ValidationError: + return await rewrite(arguments) + if not isinstance(parsed, (dict, list)): + return await rewrite(arguments) + rewritten: Final = await _rewritten_json_strings(parsed, rewrite) + return arguments if rewritten == parsed else json.dumps(rewritten, ensure_ascii=False) + + def _conversation_segments( texts: Sequence[str], arguments: Sequence[str | None], tool_call_text_offsets: Sequence[int] ) -> Iterator[tuple[_TextSlot | _ToolCallSlot, str]]: @@ -1870,25 +1910,37 @@ class _OPTIONAL_PresidioPIIMasking(CustomGuardrail): metadata: Final = (request_data.get("metadata") or {}) if request_data else {} pii_tokens: Final = metadata.get("pii_tokens", {}) - reserved: Final = accumulate(map(_literal_tokens, (source for _, source in segments)), frozenset[str].union) - rewritten: Final = ( - [self._unmask_pii_text(source, pii_tokens) for _, source in segments] - if input_type == "response" - and ( - self._callback_role == "restore" - or (self._callback_role is None and not self.apply_to_output and pii_tokens) - ) - else [ - await self.check_pii( - text=source, + async def restore(text: str) -> str: + return self._unmask_pii_text(text, pii_tokens) + + def mask_reserving(reserved_tokens: frozenset[str]) -> Callable[[str], Awaitable[str]]: + async def mask(text: str) -> str: + return await self.check_pii( + text=text, output_parse_pii=self.output_parse_pii, presidio_config=None, request_data=request_data or {}, reserved_tokens=reserved_tokens, ) - for (_, source), reserved_tokens in zip(segments, reserved) - ] + + return mask + + rewrites: Final = ( + repeat(restore) + if input_type == "response" + and ( + self._callback_role == "restore" + or (self._callback_role is None and not self.apply_to_output and pii_tokens) + ) + else map( + mask_reserving, + accumulate(map(_literal_tokens, (source for _, source in segments)), frozenset[str].union), + ) ) + rewritten: Final = [ + await (_rewritten_arguments(source, rewrite) if isinstance(slot, _ToolCallSlot) else rewrite(source)) + for (slot, source), rewrite in zip(segments, rewrites) + ] by_slot: Final = dict(zip((slot for slot, _ in segments), rewritten)) inputs["texts"] = [by_slot[_TextSlot(index)] for index in range(len(texts))] for index, tool_call in enumerate(tool_calls): diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_presidio.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_presidio.py index 95f1eaea0e8..b5063d99b44 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_presidio.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_presidio.py @@ -5104,9 +5104,31 @@ def _tool_call_response(arguments: str) -> ModelResponse: ) +_UNQUOTED_PERSON_NAME: Final = re.compile(r'(? web.Response: + payload = await request.json() + return web.json_response( + [ + {"entity_type": "PERSON", "start": match.start(), "end": match.end(), "score": 0.85} + for match in _UNQUOTED_PERSON_NAME.finditer(payload["text"]) + ] + ) + + +def _json_blind_presidio_app() -> web.Application: + app = web.Application() + app.router.add_post("/analyze", _analyze_names_outside_json_quotes) + app.router.add_post("/anonymize", _fake_anonymize) + return app + + @asynccontextmanager -async def _reversible_presidio_guardrail() -> AsyncIterator[OPTIONAL_PresidioPIIMasking]: - async with TestServer(_fake_presidio_app()) as server: +async def _reversible_presidio_guardrail( + presidio_app: web.Application | None = None, +) -> AsyncIterator[OPTIONAL_PresidioPIIMasking]: + async with TestServer(presidio_app or _fake_presidio_app()) as server: guardrail: Final = OPTIONAL_PresidioPIIMasking( presidio_analyzer_api_base=str(server.make_url("/")), presidio_anonymizer_api_base=str(server.make_url("/")), @@ -5143,6 +5165,48 @@ async def test_restore_writes_the_original_values_into_non_streaming_tool_call_a assert response.choices[0].message.tool_calls[0].function.arguments == '{"guest": "Jane Doe"}' +async def test_restore_escapes_original_values_so_tool_call_arguments_stay_valid_json() -> None: + guardrail: Final = OPTIONAL_PresidioPIIMasking( + event_hook="pre_call", default_on=True, output_parse_pii=True, mock_testing=True + ) + original: Final = 'Apt "B"\nFloor 2' + response: Final = _tool_call_response('{"address": ""}') + data: Final = {"metadata": {"pii_tokens": {"": original}}, "guardrail_to_apply": guardrail} + + await UnifiedLLMGuardrails().async_post_call_success_hook( + data, UserAPIKeyAuth(request_route="/v1/chat/completions"), response + ) + + assert json.loads(response.choices[0].message.tool_calls[0].function.arguments) == {"address": original} + + +async def test_names_inside_json_tool_call_arguments_are_masked_even_when_the_analyzer_needs_bare_text() -> None: + async with _reversible_presidio_guardrail(_json_blind_presidio_app()) as guardrail: + data: Final = await _masked_chat_request( + guardrail, + [{"role": "assistant", "content": None, "tool_calls": [_function_tool_call("call_1", '{"guest": "Jane Doe"}')]}], + ) + + masked: Final = json.loads(data["messages"][0]["tool_calls"][0]["function"]["arguments"]) + pii_tokens: Final = data.get("metadata", {}).get("pii_tokens", {}) + assert pii_tokens.get(masked["guest"]) == "Jane Doe", f"masked arguments {masked} against token map {pii_tokens}" + + +async def test_tool_call_arguments_without_pii_reach_the_model_byte_for_byte() -> None: + arguments: Final = '{"city":"Boston","days":[1,2]}' + async with _reversible_presidio_guardrail() as guardrail: + data: Final = await _masked_chat_request( + guardrail, + [ + {"role": "user", "content": "Plan a trip for Jane Doe."}, + {"role": "assistant", "content": None, "tool_calls": [_function_tool_call("call_1", arguments)]}, + ], + ) + + assert data["messages"][0]["content"] != "Plan a trip for Jane Doe.", data["messages"][0] + assert data["messages"][1]["tool_calls"][0]["function"]["arguments"] == arguments + + async def test_apply_guardrail_restores_the_arguments_of_tool_call_objects() -> None: guardrail: Final = OPTIONAL_PresidioPIIMasking( event_hook="pre_call", default_on=True, output_parse_pii=True, mock_testing=True