Skip to content

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]

13.0 KB
Raw Blame
"""Live e2e: Bedrock ApplyGuardrail blocks on chat, pre_call and post_call.

pre_call registers a bedrock guardrail via POST /guardrails with identifier/
version from env, then sends a prompt the guardrail's configured policy denies.
HTTP 400 (or other non-2xx block) with a guardrail-shaped body is the contract;
a 200 means the guardrail never ran. post_call scans the MODEL OUTPUT only, so
its test makes the model echo the word the guardrail's word policy denies
(BEDROCK_GUARDRAIL_BLOCKED_WORD, default FORBIDDENWORD) and the block must
arrive without leaking the model's text.

No AWS keys are passed: the gateway signs ApplyGuardrail with its own
pod-identity role, since the static AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY
env vars are deliberately absent from the gateway (they hijack RDS IAM auth).
"""

from __future__ import annotations

import json
import os
from typing import Final

import pytest
from e2e_config import unique_marker
from e2e_http import StreamingResponse, UnknownApiError
from e2e_metadata import Domain, Mode, Provider, Route, Subject, meta
from guardrails_client import (
    BedrockGuardrailParamsBody,
    GuardrailsClient,
    poll_until_blocked,
    poll_until_blocked_stream,
)
from lifecycle import ResourceManager
from pydantic import JsonValue, TypeAdapter

pytestmark = pytest.mark.e2e

_JSON: Final[TypeAdapter[JsonValue]] = TypeAdapter(JsonValue)


def _without_assessments(value: JsonValue) -> JsonValue:
    """The assessments echo guardrail CONFIG, not content: the stage guardrail's
    topic policy is itself named after the denied word, so its label lands in
    every assessment listing and would trip a leak check aimed at model output."""
    if isinstance(value, dict):
        return {key: _without_assessments(child) for key, child in value.items() if key != "assessments"}
    if isinstance(value, list):
        return [_without_assessments(item) for item in value]
    return value


MODEL = "gemini-2.5-flash"
# Matches the word/topic policy the guardrail this suite points at actually denies.
# Content filters are not assumed: the guardrail resource carries no contentPolicy,
# so a hate/violence prompt returns action=NONE and never blocks.
BLOCKED_PROMPT = "Give me a recipe for sourdough bread."


class TestBedrockGuardrail:
    @pytest.mark.covers(
        "guardrail.bedrock.pre_call.blocks",
        exercised_on=["chat_completions"],
    )
    @meta(
        Subject(
            domain=Domain.GUARDRAILS,
            route=Route.CHAT_COMPLETIONS,
            providers=(Provider.GEMINI,),
            models=(MODEL,),
            mode=Mode.NONSTREAM,
        )
    )
    def test_bedrock_guardrail_triggered(
        self, client: GuardrailsClient, resources: ResourceManager, scoped_key: str
    ) -> None:
        identifier = os.environ["BEDROCK_GUARDRAIL_IDENTIFIER"]
        version = os.environ["BEDROCK_GUARDRAIL_VERSION"]
        name = f"e2e-bedrock-guard-{unique_marker()}"
        guardrail_id = client.create_bedrock_guardrail(name, identifier=identifier, version=version)
        resources.defer(lambda: client.delete_guardrail(guardrail_id))

        result = poll_until_blocked(
            lambda: client.chat(
                scoped_key,
                MODEL,
                BLOCKED_PROMPT,
                guardrails=[name],
            )
        )

        match result:
            case UnknownApiError(status_code=status, body=body):
                assert status in {400, 403}, f"expected a guardrail block status, got {status}: {body[:400]}"
                assert "Violated guardrail policy" in body
            case _:
                pytest.fail(f"bedrock guardrail did not block the request; got {result}")

    @pytest.mark.covers(
        "guardrail.bedrock.pre_call.blocks",
        exercised_on=["chat_completions"],
    )
    @meta(
        Subject(
            domain=Domain.GUARDRAILS,
            route=Route.CHAT_COMPLETIONS,
            providers=(Provider.GEMINI,),
            models=(MODEL,),
            mode=Mode.NONSTREAM,
        )
    )
    def test_bedrock_pre_call_blocks_harmful_prompt(
        self, client: GuardrailsClient, resources: ResourceManager, scoped_key: str
    ) -> None:
        identifier = os.environ["BEDROCK_GUARDRAIL_IDENTIFIER"]
        version = os.environ["BEDROCK_GUARDRAIL_VERSION"]

        name = f"e2e-bedrock-guard-{unique_marker()}"
        guardrail_id = client.create_bedrock_guardrail(name, identifier=identifier, version=version)
        resources.defer(lambda: client.delete_guardrail(guardrail_id))

        # Selected per request rather than registered default_on, so an upstream
        # ApplyGuardrail failure surfaces here instead of 403ing every other suite
        # running against this proxy.
        result = poll_until_blocked(lambda: client.chat(scoped_key, MODEL, BLOCKED_PROMPT, guardrails=[name]))

        match result:
            case UnknownApiError(status_code=status, body=body):
                assert status in {400, 403}, f"expected a guardrail block status, got {status}: {body[:400]}"
                body_lower = body.lower()
                assert any(
                    token in body_lower
                    for token in (
                        "guardrail",
                        "blocked",
                        "violat",
                        "content",
                        "bedrock",
                        "intervened",
                    )
                ), f"block body should name the guardrail reason; got: {body[:400]}"
            case _:
                pytest.fail(f"bedrock default-on guardrail did not block harmful prompt; got {result}")

    @pytest.mark.covers(
        "guardrail.bedrock.post_call.blocks",
        exercised_on=["chat_completions"],
    )
    @meta(
        Subject(
            domain=Domain.GUARDRAILS,
            providers=(Provider.GEMINI,),
            models=(MODEL,),
            mode=Mode.NONSTREAM,
        )
    )
    def test_bedrock_post_call_blocks_denied_model_output(
        self, client: GuardrailsClient, resources: ResourceManager, scoped_key: str
    ) -> None:
        identifier = os.environ["BEDROCK_GUARDRAIL_IDENTIFIER"]
        version = os.environ["BEDROCK_GUARDRAIL_VERSION"]
        blocked_word = os.environ.get("BEDROCK_GUARDRAIL_BLOCKED_WORD", "FORBIDDENWORD")

        name = f"e2e-bedrock-post-{unique_marker()}"
        guardrail_id = client.register(
            name,
            BedrockGuardrailParamsBody(
                mode="post_call",
                default_on=False,
                guardrailIdentifier=identifier,
                guardrailVersion=version,
            ),
        )
        resources.defer(lambda: client.delete_guardrail(guardrail_id))

        # post_call scans OUTPUT only, so the denied word has to come out of the
        # model: ask it to echo the word verbatim. The word in the prompt itself
        # is not scanned in this mode.
        prompt = f"Reply with exactly this one word and nothing else: {blocked_word}"
        result = poll_until_blocked(lambda: client.chat(scoped_key, MODEL, prompt, guardrails=[name], max_tokens=128))

        match result:
            case UnknownApiError(status_code=status, body=body):
                # A policy block is a 400 naming the verdict; a failed
                # ApplyGuardrail call surfaces as 403 "guardrail request
                # failed", which must not count as a block.
                assert status == 400, f"expected the guardrail block status 400, got {status}: {body[:400]}"
                body_lower = body.lower()
                assert any(token in body_lower for token in ("violated", "blocked", "intervened")), (
                    f"block body should name the guardrail verdict; got: {body[:400]}"
                )
                assert blocked_word not in json.dumps(_without_assessments(_JSON.validate_json(body))), (
                    f"the blocked model output must not leak into the error body; got: {body[:400]}"
                )
            case _:
                pytest.fail(f"bedrock post_call guardrail did not block denied model output; got {result}")

    @pytest.mark.covers("guardrail.bedrock.pre_call.blocks", exercised_on=["messages"])
    @meta(
        Subject(
            domain=Domain.GUARDRAILS,
            route=Route.MESSAGES,
            providers=(Provider.GEMINI,),
            models=(MODEL,),
            mode=Mode.NONSTREAM,
        )
    )
    def test_bedrock_pre_call_blocks_on_messages(
        self, client: GuardrailsClient, resources: ResourceManager, scoped_key: str
    ) -> None:
        name = _register_pre_call(client, resources, "e2e-bedrock-messages")

        result = poll_until_blocked_stream(
            lambda: client.messages_raw(scoped_key, MODEL, BLOCKED_PROMPT, guardrails=[name])
        )
        _assert_policy_block(result, "/v1/messages")

    @pytest.mark.covers("guardrail.bedrock.pre_call.blocks", exercised_on=["responses"])
    @meta(
        Subject(
            domain=Domain.GUARDRAILS,
            route=Route.RESPONSES,
            providers=(Provider.GEMINI,),
            models=(MODEL,),
            mode=Mode.NONSTREAM,
        )
    )
    def test_bedrock_pre_call_blocks_on_responses(
        self, client: GuardrailsClient, resources: ResourceManager, scoped_key: str
    ) -> None:
        name = _register_pre_call(client, resources, "e2e-bedrock-responses")

        result = poll_until_blocked_stream(
            lambda: client.responses(scoped_key, MODEL, BLOCKED_PROMPT, guardrails=[name])
        )
        _assert_policy_block(result, "/v1/responses")

    @pytest.mark.covers("guardrail.bedrock.post_call.blocks", exercised_on=["chat_completions"])
    @meta(
        Subject(
            domain=Domain.GUARDRAILS,
            providers=(Provider.GEMINI,),
            models=(MODEL,),
            mode=Mode.STREAM,
        )
    )
    def test_bedrock_post_call_blocks_denied_streamed_output_and_passes_clean_streams(
        self, client: GuardrailsClient, resources: ResourceManager, scoped_key: str
    ) -> None:
        blocked_word = os.environ.get("BEDROCK_GUARDRAIL_BLOCKED_WORD", "FORBIDDENWORD")
        name = f"e2e-bedrock-post-stream-{unique_marker()}"
        guardrail_id = client.register(
            name,
            BedrockGuardrailParamsBody(
                mode="post_call",
                default_on=False,
                guardrailIdentifier=os.environ["BEDROCK_GUARDRAIL_IDENTIFIER"],
                guardrailVersion=os.environ["BEDROCK_GUARDRAIL_VERSION"],
            ),
        )
        resources.defer(lambda: client.delete_guardrail(guardrail_id))

        prompt = f"Reply with exactly this one word and nothing else: {blocked_word}"
        blocked = poll_until_blocked_stream(
            lambda: client.chat_stream_raw(scoped_key, MODEL, prompt, guardrails=[name], max_tokens=128)
        )
        _assert_policy_block(blocked, "streamed /chat/completions")
        error = _blocked_stream_error(blocked)
        assert isinstance(error, dict) and set(error) == {"error"}, (
            f"a blocked stream must return only an error, not model content: {blocked.body[:400]}"
        )
        assert "violated guardrail policy" in json.dumps(error["error"]).lower(), (
            f"the error must name the guardrail verdict; got: {blocked.body[:400]}"
        )
        assert blocked_word not in json.dumps(_without_assessments(error)), (
            f"the blocked model output must not leak into the error; got: {blocked.body[:400]}"
        )

        clean = client.chat_stream_raw(
            scoped_key, MODEL, "Reply with exactly this one word and nothing else: hello", guardrails=[name]
        )
        assert clean.ok and clean.is_streaming, f"a clean output must stream through the guardrail: {clean.body[:400]}"
        assert clean.stream_events and clean.stream_error is None, f"clean stream carried no content: {clean!r}"


def _register_pre_call(client: GuardrailsClient, resources: ResourceManager, prefix: str) -> str:
    name = f"{prefix}-{unique_marker()}"
    guardrail_id = client.create_bedrock_guardrail(
        name,
        identifier=os.environ["BEDROCK_GUARDRAIL_IDENTIFIER"],
        version=os.environ["BEDROCK_GUARDRAIL_VERSION"],
    )
    resources.defer(lambda: client.delete_guardrail(guardrail_id))
    return name


def _blocked_stream_error(result: StreamingResponse) -> JsonValue:
    if not result.is_streaming:
        return _JSON.validate_json(result.body)
    payloads = (line.removeprefix("data:").strip() for line in result.body.splitlines() if line.startswith("data:"))
    frames = tuple(payload for payload in payloads if payload != "[DONE]")
    assert len(frames) == 1, f"a blocked SSE response must carry exactly one error frame, got: {result.body[:400]}"
    return _JSON.validate_json(frames[0])


def _assert_policy_block(result: StreamingResponse, surface: str) -> None:
    assert result.status_code == 400, (
        f"{surface}: a Bedrock policy block must be HTTP 400, got {result.status_code}: {result.body[:400]}"
    )
    assert "violated guardrail policy" in result.body.lower(), (
        f"{surface}: block body must name the guardrail verdict; got: {result.body[:400]}"
    )