The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
import json
import uuid
from collections.abc import AsyncGenerator
from typing import Final, Literal
from unittest.mock import AsyncMock, MagicMock
import httpx
import pytest
import litellm
from litellm.exceptions import GuardrailRaisedException
from litellm.proxy.guardrails.guardrail_hooks.airia.airia import AiriaGuardrail
from litellm.proxy.guardrails.init_guardrails import (
init_guardrails_v2, # pyright: ignore[reportUnknownVariableType] # untyped upstream
)
from litellm.types.guardrails import GuardrailEventHooks
from litellm.types.proxy.guardrails.guardrail_hooks.airia import AiriaGuardrailConfigModel
from litellm.types.utils import GenericGuardrailAPIInputs
API_BASE = "https://gateway.airia.ai"
API_KEY = "ak-test-key"
def _make_guardrail(**kwargs: object) -> AiriaGuardrail:
"""Build a guardrail; construction defaults still exercise the real HTTP client."""
kwargs.setdefault("api_base", API_BASE)
kwargs.setdefault("api_key", API_KEY)
kwargs.setdefault("guardrail_name", "airia-guard")
return AiriaGuardrail(**kwargs)
def _make_guardrail_with_response(payload: object, status_code: int = 200, **kwargs: object) -> AiriaGuardrail:
"""Build a guardrail with a fake HTTP client injected at construction, per CLAUDE.md's
dependency-injection rule: no test mutates `async_handler` after the fact."""
handler = MagicMock()
handler.post = AsyncMock(return_value=_mock_response(payload, status_code))
return _make_guardrail(async_handler=handler, **kwargs)
def _mock_response(payload: object, status_code: int = 200) -> MagicMock:
response = MagicMock()
response.status_code = status_code
response.json.return_value = payload
response.raise_for_status = MagicMock()
return response
def _as_mock(value: object) -> MagicMock:
"""Forget the declared type, which is the real handler's, and recover the mock that was injected."""
assert isinstance(value, MagicMock)
return value
def _post_mock(guardrail: AiriaGuardrail) -> AsyncMock:
"""The `post` the test injected, typed as the mock it is so call inspection stays checked."""
post = _as_mock(guardrail.async_handler).post
assert isinstance(post, AsyncMock)
return post
def _timeout(guardrail: AiriaGuardrail) -> httpx.Timeout:
timeout = guardrail.async_handler.timeout # pyright: ignore[reportUnknownMemberType] # the handler's attribute is partially untyped
assert isinstance(timeout, httpx.Timeout)
return timeout
def _inputs(**overrides: object) -> GenericGuardrailAPIInputs:
return GenericGuardrailAPIInputs(**{"texts": ["hello"], "model": "gpt-4o", **overrides})
def test_airia_guardrail_config(monkeypatch: pytest.MonkeyPatch) -> None:
"""The guardrail registers through init_guardrails_v2 under the `airia` key."""
monkeypatch.setattr(litellm, "guardrail_name_config_map", {})
monkeypatch.setattr(litellm, "callbacks", [])
monkeypatch.setenv("AIRIA_GATEWAY_URL", API_BASE)
monkeypatch.setenv("AIRIA_API_KEY", API_KEY)
init_guardrails_v2(
all_guardrails=[
{
"guardrail_name": "airia-guard",
"litellm_params": {
"guardrail": "airia",
"mode": "pre_call",
"default_on": True,
"timeout": 45.0,
},
}
],
config_file_path="",
)
registered = [c for c in litellm.callbacks if isinstance(c, AiriaGuardrail)]
assert len(registered) == 1
assert registered[0].guardrail_name == "airia-guard"
assert registered[0].default_on is True
assert registered[0].event_hook == "pre_call"
assert _timeout(registered[0]).read == 45.0
def test_during_call_is_not_a_supported_event_hook() -> None:
"""during_call runs concurrently with the upstream call, so a block can land too late."""
hooks = _make_guardrail().supported_event_hooks
assert hooks is not None
assert GuardrailEventHooks.during_call not in hooks
assert list(hooks) == [GuardrailEventHooks.pre_call, GuardrailEventHooks.post_call]
@pytest.mark.parametrize("missing", ["api_base", "api_key"])
def test_missing_credentials_raise_at_construction(missing: str, monkeypatch: pytest.MonkeyPatch) -> None:
"""Fail at startup rather than on the first request."""
monkeypatch.delenv("AIRIA_GATEWAY_URL", raising=False)
monkeypatch.delenv("AIRIA_API_KEY", raising=False)
kwargs = {"api_base": API_BASE, "api_key": API_KEY}
kwargs[missing] = None
with pytest.raises(ValueError, match="AiriaGuardrail requires"):
_make_guardrail(**kwargs)
def test_credentials_fall_back_to_environment(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("AIRIA_GATEWAY_URL", API_BASE)
monkeypatch.setenv("AIRIA_API_KEY", API_KEY)
guardrail = _make_guardrail(api_base=None, api_key=None)
assert guardrail.api_base == API_BASE
assert guardrail.api_key == API_KEY
def test_trailing_slash_is_stripped_from_api_base() -> None:
guardrail = _make_guardrail(api_base=f"{API_BASE}/")
assert guardrail.api_base == API_BASE
def test_custom_timeout_from_kwargs() -> None:
guardrail = _make_guardrail(timeout=45.0)
assert _timeout(guardrail).read == 45.0
assert _timeout(guardrail).connect == 5.0
def test_timeout_falls_back_to_environment(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("AIRIA_TIMEOUT", "30")
guardrail = _make_guardrail()
assert _timeout(guardrail).read == 30.0
@pytest.mark.asyncio
async def test_request_payload_and_auth_header() -> None:
guardrail = _make_guardrail_with_response({"action": "NONE"})
inputs = _inputs(
images=["data:image/png;base64,AAAA"],
structured_messages=[{"role": "user", "content": "hello"}],
tools=[{"type": "function", "function": {"name": "f"}}],
tool_calls=[{"id": "c1", "function": {"name": "f", "arguments": "{}"}}],
)
await guardrail.apply_guardrail(inputs=inputs, request_data={"litellm_call_id": "call-123"}, input_type="request")
call = _post_mock(guardrail).call_args
assert call.args[0] == f"{API_BASE}/v1/guardrails/litellm"
assert call.kwargs["headers"] == {"Authorization": f"Bearer {API_KEY}"}
payload = call.kwargs["json"]
assert payload["input_type"] == "request"
assert payload["texts"] == ["hello"]
assert payload["structured_messages"] == [{"role": "user", "content": "hello"}]
assert payload["images"] == ["data:image/png;base64,AAAA"]
assert payload["tools"] == [{"type": "function", "function": {"name": "f"}}]
assert payload["tool_calls"] == [{"id": "c1", "function": {"name": "f", "arguments": "{}"}}]
assert payload["model"] == "gpt-4o"
assert payload["litellm_call_id"] == "call-123"
@pytest.mark.asyncio
@pytest.mark.parametrize("input_type", ["request", "response"])
async def test_input_type_is_forwarded_verbatim(input_type: Literal["request", "response"]) -> None:
guardrail = _make_guardrail_with_response({"action": "NONE"})
await guardrail.apply_guardrail(inputs=_inputs(), request_data={}, input_type=input_type)
assert _post_mock(guardrail).call_args.kwargs["json"]["input_type"] == input_type
@pytest.mark.asyncio
async def test_call_id_falls_back_to_a_generated_uuid() -> None:
guardrail = _make_guardrail_with_response({"action": "NONE"})
await guardrail.apply_guardrail(inputs=_inputs(), request_data={}, input_type="request")
call_id = _post_mock(guardrail).call_args.kwargs["json"]["litellm_call_id"]
assert uuid.UUID(call_id).version == 4
@pytest.mark.asyncio
async def test_action_none_returns_inputs_unchanged() -> None:
guardrail = _make_guardrail_with_response({"action": "NONE"})
inputs = _inputs(structured_messages=[{"role": "user", "content": "hello"}])
result = await guardrail.apply_guardrail(inputs=inputs, request_data={}, input_type="request")
assert result["texts"] == ["hello"]
assert result["structured_messages"] == [{"role": "user", "content": "hello"}]
@pytest.mark.asyncio
async def test_action_blocked_raises_with_blocked_content_set() -> None:
guardrail = _make_guardrail_with_response({"action": "BLOCKED", "blocked_reason": "Contains a secret"})
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(inputs=_inputs(), request_data={}, input_type="request")
assert excinfo.value.blocked_content is True
assert "Contains a secret" in str(excinfo.value)
@pytest.mark.asyncio
async def test_action_blocked_without_a_reason_uses_the_default_message() -> None:
guardrail = _make_guardrail_with_response({"action": "BLOCKED"})
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(inputs=_inputs(), request_data={}, input_type="request")
assert excinfo.value.blocked_content is True
assert "Blocked by your organization's content policy." in str(excinfo.value)
@pytest.mark.asyncio
async def test_intervened_replaces_both_text_bearing_fields() -> None:
"""Substituting only one field would leave the other still carrying the unredacted text."""
guardrail = _make_guardrail_with_response(
{
"action": "GUARDRAIL_INTERVENED",
"texts": ["my email is [EmailAddress1]"],
"structured_messages": [{"role": "user", "content": "my email is [EmailAddress1]"}],
}
)
inputs = _inputs(
texts=["my email is [email protected]"],
structured_messages=[{"role": "user", "content": "my email is [email protected]"}],
)
result = await guardrail.apply_guardrail(inputs=inputs, request_data={}, input_type="request")
assert result["texts"] == ["my email is [EmailAddress1]"]
assert result["structured_messages"] == [{"role": "user", "content": "my email is [EmailAddress1]"}]
assert "[email protected]" not in str(result)
@pytest.mark.asyncio
async def test_intervened_leaves_a_field_alone_when_airia_omits_it() -> None:
"""An omitted field means "not rewritten", not "rewritten to empty"."""
guardrail = _make_guardrail_with_response({"action": "GUARDRAIL_INTERVENED", "texts": ["redacted"]})
inputs = _inputs(texts=["original"], structured_messages=[{"role": "user", "content": "keep me"}])
result = await guardrail.apply_guardrail(inputs=inputs, request_data={}, input_type="request")
assert result["texts"] == ["redacted"]
assert result["structured_messages"] == [{"role": "user", "content": "keep me"}]
@pytest.mark.asyncio
async def test_intervened_with_a_non_list_field_is_treated_as_a_block() -> None:
"""A rewrite this version cannot apply must not let the original text through."""
guardrail = _make_guardrail_with_response({"action": "GUARDRAIL_INTERVENED", "texts": "not a list"})
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(inputs=_inputs(texts=["secret"]), request_data={}, input_type="request")
assert excinfo.value.blocked_content is True
@pytest.mark.asyncio
async def test_intervened_with_a_non_list_structured_messages_is_treated_as_a_block() -> None:
guardrail = _make_guardrail_with_response(
{"action": "GUARDRAIL_INTERVENED", "structured_messages": {"role": "user"}}
)
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(inputs=_inputs(), request_data={}, input_type="request")
assert excinfo.value.blocked_content is True
@pytest.mark.asyncio
async def test_intervened_without_any_rewritten_field_is_treated_as_a_block() -> None:
"""An intervention the proxy cannot apply must not let the original content through."""
guardrail = _make_guardrail_with_response({"action": "GUARDRAIL_INTERVENED", "tool_calls": []})
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(inputs=_inputs(texts=["secret"]), request_data={}, input_type="request")
assert excinfo.value.blocked_content is True
@pytest.mark.asyncio
@pytest.mark.parametrize("body", [["not", "an", "object"], "just a string", 42, None])
async def test_a_non_object_response_body_fails_closed_but_is_not_reported_as_a_verdict(body: object) -> None:
"""response.json() is not guaranteed to return a dict; body.get(...) on a list or scalar
would otherwise raise AttributeError and surface as an internal error, not a clean refusal."""
guardrail = _make_guardrail_with_response(body)
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(inputs=_inputs(), request_data={}, input_type="request")
assert excinfo.value.blocked_content is False
assert "could not evaluate" in str(excinfo.value)
@pytest.mark.asyncio
async def test_intervened_with_a_texts_rewrite_of_the_wrong_length_is_treated_as_a_block() -> None:
"""A rewrite with a different item count than what was sent would misalign the positional
write-back downstream; accepting it could drop content or leave other items unredacted."""
guardrail = _make_guardrail_with_response({"action": "GUARDRAIL_INTERVENED", "texts": ["only one redacted item"]})
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(
inputs=_inputs(texts=["first secret", "second secret"]), request_data={}, input_type="request"
)
assert excinfo.value.blocked_content is True
@pytest.mark.asyncio
@pytest.mark.parametrize("structured_messages", [[], [{"role": "user", "content": "[Redacted]"}]])
async def test_intervened_rewrite_for_a_field_that_was_not_sent_is_treated_as_a_block(
structured_messages: list[object],
) -> None:
"""post_call sends only `texts`. A `structured_messages` rewrite then has nothing to replace, and an
empty one must not count as "applied" while the `texts` that carried the content pass through."""
guardrail = _make_guardrail_with_response(
{"action": "GUARDRAIL_INTERVENED", "structured_messages": structured_messages}
)
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(inputs=_inputs(texts=["secret"]), request_data={}, input_type="response")
assert excinfo.value.blocked_content is True
@pytest.mark.asyncio
async def test_intervened_returns_a_copy_and_leaves_the_caller_object_untouched() -> None:
guardrail = _make_guardrail_with_response({"action": "GUARDRAIL_INTERVENED", "texts": ["redacted"]})
inputs = _inputs(texts=["original"])
result = await guardrail.apply_guardrail(inputs=inputs, request_data={}, input_type="request")
assert result["texts"] == ["redacted"]
assert inputs["texts"] == ["original"]
@pytest.mark.asyncio
async def test_streaming_redaction_actually_reaches_the_client() -> None:
"""block_only (the framework default) drops guardrail rewrites on the streaming path, so a
REDACT verdict would silently leak the original text. Runs the real UnifiedLLMGuardrails
streaming iterator end to end (not just this guardrail's config attributes) and checks what
the client would actually receive.
"""
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.guardrails.guardrail_hooks.unified_guardrail.unified_guardrail import (
UnifiedLLMGuardrails,
)
from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
guardrail: Final = _make_guardrail_with_response(
{"action": "GUARDRAIL_INTERVENED", "texts": ["Contact me at [EmailAddress1]"]},
event_hook="post_call",
default_on=True,
)
async def source_stream() -> AsyncGenerator[ModelResponseStream, None]:
for content in ("Contact ", "me at ", "[email protected]"):
yield ModelResponseStream(
model="gpt-4o-mini", choices=[StreamingChoices(index=0, delta=Delta(content=content))]
)
yield ModelResponseStream(
model="gpt-4o-mini",
choices=[StreamingChoices(index=0, delta=Delta(content=None), finish_reason="stop")],
)
assembled = ""
async for chunk in UnifiedLLMGuardrails().async_post_call_streaming_iterator_hook( # pyright: ignore[reportUnknownMemberType] # annotates request_data as a bare dict
user_api_key_dict=UserAPIKeyAuth(api_key="test", request_route="/chat/completions"),
response=source_stream(),
request_data={ # mutable-ok: one-shot request_data the framework requires as a plain dict
"messages": [{"role": "user", "content": "share your contact"}],
"stream": True,
},
guardrail_to_apply=guardrail,
):
for choice in chunk.choices:
if choice.delta.content:
assembled += choice.delta.content
assert "[EmailAddress1]" in assembled
assert "[email protected]" not in assembled
_post_mock(guardrail).assert_called_once()
def _streamed_request(route: str | None) -> dict[str, object]:
"""A proxy request_data as the streaming hook sees it: `stream` set, and the route the proxy
stamped into the metadata bucket (`litellm_metadata` for the non-chat routes under test)."""
request_data: dict[str, object] = {"stream": True} # mutable-ok: built once per test
if route is not None:
request_data["litellm_metadata"] = {"user_api_key_request_route": route}
return request_data
@pytest.mark.asyncio
@pytest.mark.parametrize("route", ["/v1/messages", "/v1/responses", "/responses", "/unknown/route", None])
async def test_streamed_redaction_on_a_route_without_stream_rewrites_is_enforced_as_a_block(
route: str | None,
) -> None:
"""incremental_diff exists only for chat completions; every other streamed route runs block_only,
which drops rewrites and replays the original text. There a REDACT verdict must block instead."""
guardrail = _make_guardrail_with_response({"action": "GUARDRAIL_INTERVENED", "texts": ["[Redacted]"]})
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(
inputs=_inputs(texts=["secret"]), request_data=_streamed_request(route), input_type="response"
)
assert excinfo.value.blocked_content is True
@pytest.mark.asyncio
@pytest.mark.parametrize("route", ["/chat/completions", "/v1/chat/completions"])
async def test_streamed_redaction_on_chat_completions_is_still_delivered(route: str) -> None:
guardrail = _make_guardrail_with_response({"action": "GUARDRAIL_INTERVENED", "texts": ["[Redacted]"]})
request_data: dict[str, object] = {"stream": True, "metadata": {"user_api_key_request_route": route}}
result = await guardrail.apply_guardrail(
inputs=_inputs(texts=["secret"]), request_data=request_data, input_type="response"
)
assert result["texts"] == ["[Redacted]"]
@pytest.mark.asyncio
async def test_unstreamed_redaction_on_a_non_chat_route_is_delivered() -> None:
"""Only the stream cannot carry a rewrite; a non-streamed Responses call is rewritten as usual."""
guardrail = _make_guardrail_with_response({"action": "GUARDRAIL_INTERVENED", "texts": ["[Redacted]"]})
request_data: dict[str, object] = {"litellm_metadata": {"user_api_key_request_route": "/v1/responses"}}
result = await guardrail.apply_guardrail(
inputs=_inputs(texts=["secret"]), request_data=request_data, input_type="response"
)
assert result["texts"] == ["[Redacted]"]
@pytest.mark.asyncio
async def test_streamed_request_side_redaction_is_unaffected() -> None:
"""A pre_call rewrite lands in the prompt before the model is called; streaming is irrelevant there."""
guardrail = _make_guardrail_with_response({"action": "GUARDRAIL_INTERVENED", "texts": ["[Redacted]"]})
result = await guardrail.apply_guardrail(
inputs=_inputs(texts=["secret"]), request_data=_streamed_request("/v1/messages"), input_type="request"
)
assert result["texts"] == ["[Redacted]"]
@pytest.mark.asyncio
async def test_stream_flag_is_also_read_from_the_proxied_request_body() -> None:
guardrail = _make_guardrail_with_response({"action": "GUARDRAIL_INTERVENED", "texts": ["[Redacted]"]})
request_data: dict[str, object] = {
"proxy_server_request": {"body": {"stream": True}},
"litellm_metadata": {"user_api_key_request_route": "/v1/messages"},
}
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(
inputs=_inputs(texts=["secret"]), request_data=request_data, input_type="response"
)
assert excinfo.value.blocked_content is True
def _anthropic_sse(event_type: str, data: dict[str, object]) -> bytes:
return f"event: {event_type}\ndata: {json.dumps(data)}\n\n".encode()
async def _anthropic_messages_stream() -> AsyncGenerator[bytes, None]:
"""A complete Anthropic /v1/messages SSE stream, the shape the proxy relays on that route."""
yield _anthropic_sse(
"message_start",
{
"type": "message_start",
"message": {
"id": "msg_orig",
"type": "message",
"role": "assistant",
"model": "claude-3-5-sonnet",
"content": [],
"stop_reason": None,
"usage": {"input_tokens": 1, "output_tokens": 0},
},
},
)
yield _anthropic_sse(
"content_block_start",
{"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}},
)
for text in ("Contact ", "me at ", "[email protected]"):
yield _anthropic_sse(
"content_block_delta",
{"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": text}},
)
yield _anthropic_sse("content_block_stop", {"type": "content_block_stop", "index": 0})
yield _anthropic_sse(
"message_delta",
{
"type": "message_delta",
"delta": {"stop_reason": "end_turn", "stop_sequence": None},
"usage": {"output_tokens": 5},
},
)
yield _anthropic_sse("message_stop", {"type": "message_stop"})
async def _drain_into(stream: AsyncGenerator[object, None], sink: list[object]) -> None:
"""Forward every chunk the client would have received into `sink`, keeping what leaked before a
raise (a comprehension would drop it). Lives outside the test so `pytest.raises` wraps one await."""
async for chunk in stream:
sink.append(chunk) # noqa: PERF401 # the partial list is the point when the stream is refused
@pytest.mark.asyncio
@pytest.mark.parametrize(
"verdict",
[
{"action": "BLOCKED", "blocked_reason": "policy"},
{"action": "GUARDRAIL_INTERVENED", "texts": ["Contact me at [EmailAddress1]"]},
],
)
async def test_non_chat_streamed_route_releases_nothing_before_the_verdict(verdict: dict[str, object]) -> None:
"""Drives the real UnifiedLLMGuardrails iterator on /v1/messages, which incremental_diff does not
support, so the framework takes its block_only fallback. streaming_buffer_until_moderated holds
the text until the end-of-stream verdict; a block, and a redaction block_only could not deliver,
both refuse the response without one original chunk reaching the client."""
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.guardrails.guardrail_hooks.unified_guardrail.unified_guardrail import (
UnifiedLLMGuardrails,
)
guardrail: Final = _make_guardrail_with_response(verdict, event_hook="post_call", default_on=True)
released: list[object] = [] # mutable-ok: what the client received before the stream was refused
hook: Final = UnifiedLLMGuardrails().async_post_call_streaming_iterator_hook( # pyright: ignore[reportUnknownMemberType] # annotates request_data as a bare dict
user_api_key_dict=UserAPIKeyAuth(api_key="test", request_route="/v1/messages"),
response=_anthropic_messages_stream(),
request_data={ # mutable-ok: one-shot request_data the framework requires as a plain dict
"messages": [{"role": "user", "content": "share your contact"}],
"stream": True,
},
guardrail_to_apply=guardrail,
)
with pytest.raises(GuardrailRaisedException) as excinfo:
await _drain_into(hook, released)
assert excinfo.value.blocked_content is True
assert "[email protected]" not in "".join(c.decode() if isinstance(c, bytes) else str(c) for c in released)
_post_mock(guardrail).assert_called_once()
def test_config_model_is_exposed() -> None:
assert AiriaGuardrail.get_config_model() is AiriaGuardrailConfigModel
assert AiriaGuardrailConfigModel.ui_friendly_name() == "Airia Guardrail"
@pytest.mark.asyncio
async def test_unrecognized_action_is_treated_as_a_block() -> None:
"""An action this version cannot interpret must not become an allow."""
guardrail = _make_guardrail_with_response({"action": "SOME_FUTURE_ACTION"})
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(inputs=_inputs(), request_data={}, input_type="request")
assert excinfo.value.blocked_content is True
@pytest.mark.asyncio
async def test_missing_action_is_treated_as_a_block() -> None:
"""A response with no action at all is not an allow either."""
guardrail = _make_guardrail_with_response({})
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(inputs=_inputs(), request_data={}, input_type="request")
assert excinfo.value.blocked_content is True
@pytest.mark.asyncio
async def test_transport_failure_fails_closed_but_is_not_reported_as_a_verdict() -> None:
"""Still refused, but blocked_content stays False: could not evaluate is not a verdict."""
handler: Final = MagicMock()
handler.post = AsyncMock(side_effect=httpx.ConnectError("connection refused"))
guardrail: Final = _make_guardrail(async_handler=handler)
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(inputs=_inputs(), request_data={}, input_type="request")
assert excinfo.value.blocked_content is False
assert "could not evaluate" in str(excinfo.value)
@pytest.mark.asyncio
async def test_non_2xx_fails_closed_but_is_not_reported_as_a_verdict() -> None:
response: Final = _mock_response({}, status_code=503)
response.raise_for_status.side_effect = httpx.HTTPStatusError("503", request=MagicMock(), response=MagicMock())
handler: Final = MagicMock()
handler.post = AsyncMock(return_value=response)
guardrail: Final = _make_guardrail(async_handler=handler)
with pytest.raises(GuardrailRaisedException) as excinfo:
await guardrail.apply_guardrail(inputs=_inputs(), request_data={}, input_type="request")
assert excinfo.value.blocked_content is False