Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
e77024f
fix(grayswan): send request conversation and tool calls to post-call …
yucheng-berri Sep 29, 2026
cd0e9ce
refactor(grayswan): tighten post-call context typing and wire test he…
yucheng-berri Sep 29, 2026
e5fac49
fix(grayswan): resolve post-call surface from request route before ca…
yucheng-berri Sep 29, 2026
5b74a03
fix(grayswan): omit tools from post-call monitor when request context…
yucheng-berri Sep 29, 2026
8e93f09
style(grayswan): apply ruff format to post-call context changes
yucheng-berri Sep 29, 2026
8265510
fix(grayswan): merge response text and tool calls into one assistant …
yucheng-berri Sep 29, 2026
11b61ea
fix(grayswan): only merge tool calls into the response text for singl…
yucheng-berri Sep 29, 2026
437a9f2
test(grayswan): audit post-call context across endpoints, modes and o…
yucheng-berri Sep 29, 2026
ce933b3
test(grayswan): share the upstream model probe reply across audit res…
yucheng-berri Sep 30, 2026
1cfac8e
test(grayswan): assert the full generic guardrail body and kill a rea…
yucheng-berri Sep 30, 2026
428cb58
test(grayswan): normalize the client user agent in the generic body a…
yucheng-berri Sep 30, 2026
c681622
test(grayswan): normalize accept-encoding in generic body assertion
yucheng-berri Sep 30, 2026
9888871
test(grayswan): keep volatile header placeholders only when the heade…
yucheng-berri Sep 30, 2026
b53a862
test(grayswan): capture monitor calls immutably in the unit test client
yucheng-berri Sep 30, 2026
ad27fc4
test(grayswan): type the test helper parameters
yucheng-berri Sep 30, 2026
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
122 changes: 115 additions & 7 deletions litellm/proxy/guardrails/guardrail_hooks/grayswan/grayswan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import os
import time
from typing import TYPE_CHECKING, Final, Literal, Optional, Protocol
from collections.abc import Mapping
from typing import TYPE_CHECKING, Final, Literal, Optional, Protocol, cast

from fastapi import HTTPException
from pydantic import BaseModel, TypeAdapter
from typing_extensions import NotRequired, ReadOnly, TypedDict, Unpack

from litellm._logging import verbose_proxy_logger
Expand All @@ -15,12 +17,18 @@
)
from litellm.litellm_core_utils.safe_json_dumps import safe_dumps
from litellm.litellm_core_utils.safe_json_loads import safe_json_loads
from litellm.llms.base_llm.guardrail_translation.utils import (
effective_scan_only_tool_results_for_guardrail,
effective_skip_system_message_for_guardrail,
effective_skip_tool_message_for_guardrail,
scoped_structured_message_indices,
)
from litellm.llms.custom_httpx.http_handler import (
get_async_httpx_client,
httpxSpecialProvider,
)
from litellm.types.guardrails import GuardrailEventHooks
from litellm.types.utils import GenericGuardrailAPIInputs
from litellm.types.utils import CallTypes, GenericGuardrailAPIInputs

if TYPE_CHECKING:
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
Expand Down Expand Up @@ -59,6 +67,20 @@ async def post(
) -> _GraySwanMonitorHTTPResponse: ...


class _MonitorMessage(TypedDict):
role: ReadOnly[str]
content: ReadOnly[NotRequired[str]]
tool_calls: ReadOnly[NotRequired[tuple[Mapping[str, object], ...]]]


def _as_plain_dict(item: object) -> Mapping[str, object]:
if isinstance(item, Mapping):
return item
if isinstance(item, BaseModel):
return TypeAdapter(dict[str, object]).validate_python(item.model_dump(mode="json"))
return cast("Mapping[str, object]", item) # cast-ok: wire rows are message/tool-call dicts


class GraySwanGuardrailMissingSecrets(Exception):
"""Raised when the Gray Swan API key is missing."""

Expand Down Expand Up @@ -208,7 +230,7 @@ async def apply_guardrail(
inputs: Dictionary containing:
- texts: List of texts to scan
- images: Optional list of images (not currently used by GraySwan)
- tool_calls: Optional list of tool calls (not currently used)
- tool_calls: Optional list of tool calls sent back by the model
request_data: The original request data
input_type: "request" for pre-call, "response" for post-call
logging_obj: Optional logging object
Expand All @@ -228,7 +250,12 @@ async def apply_guardrail(
)

texts: Final = inputs.get("texts", [])
if not texts:
response_tool_calls: Final = (
tuple(_as_plain_dict(call) for call in (inputs.get("tool_calls") or ()))
if input_type == "response" and inputs.get("tool_calls")
else ()
)
if not texts and not response_tool_calls:
verbose_proxy_logger.debug("Gray Swan Guardrail: No texts to scan")
return inputs

Expand All @@ -238,18 +265,39 @@ async def apply_guardrail(
input_type,
)

scan_only_tool_results: Final = effective_scan_only_tool_results_for_guardrail(self)
context, tools = (
self._post_call_context(request_data, logging_obj, scan_only_tool_results)
if input_type == "response"
else ((), None)
)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

# Convert texts to messages format for GraySwan API
# Use "user" role for request content, "assistant" for response content
role: Final = "assistant" if input_type == "response" else "user"
messages: Final = [{"role": role, "content": text} for text in texts]
merged_tail: Final = (
_MonitorMessage(role="assistant", content=texts[-1], tool_calls=response_tool_calls)
if len(texts) == 1 and response_tool_calls
else None
)
messages: Final = (
*context,
*(_MonitorMessage(role=role, content=text) for text in (texts[:-1] if merged_tail else texts)),
*((merged_tail,) if merged_tail else ()),
*(
(_MonitorMessage(role="assistant", tool_calls=response_tool_calls),)
if response_tool_calls and not merged_tail
else ()
),
)

# Get dynamic params from request metadata
dynamic_body: Final = self.get_guardrail_dynamic_request_body_params(request_data) or {}
if dynamic_body:
verbose_proxy_logger.debug("Gray Swan Guardrail: dynamic extra_body=%s", safe_dumps(dynamic_body))

# Prepare and send payload
payload: Final = self._prepare_payload(messages, dynamic_body, request_data, logging_obj)
payload: Final = self._prepare_payload(messages, dynamic_body, request_data, logging_obj, tools=tools)
if payload is None:
return inputs

Expand Down Expand Up @@ -562,14 +610,74 @@ def _extract_inbound_headers(
forwarded_headers[str(key)] = str(value)
return forwarded_headers or None

def _post_call_context(
self,
request_data: dict,
logging_obj: Optional["LiteLLMLoggingObj"],
scan_only_tool_results: bool,
) -> tuple[tuple[Mapping[str, object], ...], tuple[object, ...] | None]:
"""Request conversation in OpenAI shape, scoped like the pre-call path.

Returns the scoped context messages plus the request's tool definitions,
or ``((), None)`` when the request surface cannot be resolved.
"""
from litellm.litellm_core_utils.api_route_to_call_types import get_call_types_for_route
from litellm.llms import load_guardrail_translation_mappings

litellm_metadata: Final = request_data.get("litellm_metadata")
request_route: Final = (
litellm_metadata.get("user_api_key_request_route") if isinstance(litellm_metadata, Mapping) else None
)
route_call_types: Final = get_call_types_for_route(request_route) if isinstance(request_route, str) else None
call_type: Final = (
(route_call_types[0].value if route_call_types else None)
or (logging_obj.call_type if logging_obj is not None else None)
or getattr(request_data.get("litellm_logging_obj"), "call_type", None)
)
if not isinstance(call_type, str):
return (), None
try:
mapped: Final = CallTypes(call_type)
except ValueError:
return (), None
handler_cls: Final = load_guardrail_translation_mappings().get(mapped)
if handler_cls is None:
return (), None
try:
structured: Final = handler_cls().get_structured_messages(request_data) or ()
except Exception as exc:
verbose_proxy_logger.debug(
"Gray Swan Guardrail: could not resolve request context for call_type %s: %s",
call_type,
exc,
)
return (), None
indices: Final = scoped_structured_message_indices(
structured,
scan_only_tool_results=scan_only_tool_results,
skip_system=effective_skip_system_message_for_guardrail(self),
skip_tool=effective_skip_tool_message_for_guardrail(self),
)
if not indices:
return (), None
raw_tools: Final = request_data.get("tools")
tools: Final = (
tuple(raw_tools) if not scan_only_tool_results and isinstance(raw_tools, list) and raw_tools else None
)
return tuple(_as_plain_dict(structured[index]) for index in indices), tools

def _prepare_payload(
self,
messages: list[dict[str, str]],
messages: tuple[Mapping[str, object], ...],
dynamic_body: dict,
request_data: dict,
logging_obj: Optional["LiteLLMLoggingObj"] = None,
*,
tools: tuple[object, ...] | None = None,
) -> dict[str, object] | None:
payload: Final[dict[str, object]] = {"messages": messages}
if tools:
payload["tools"] = tools

categories: Final = dynamic_body.get("categories") or self.categories
if categories:
Expand Down
Loading
Loading