Skip to content

QA plan: Output guard on /query (#72)

Story: Output guard: ApplyGuardrail on generated answer before return (#72)
Feature: Safety Gate via Bedrock Guardrails (#69)
Capability: Safety (#44)
ADRs: RAG013 — Safety gate via Bedrock Guardrails

TypeCoverage
UnitA generated answer the guardrail blocks returns HTTP 200 whose answer is the configured intervention message, whose citations are empty, and whose reason is content_policy_violation; the generated text is absent from the response
UnitA generated answer the guardrail allows is returned unmodified with its citations
UnitWith no guardrail ARN configured, the generated answer is returned unchanged
UnitAn empty generated answer is not sent to ApplyGuardrail and the response is the normal one
UnitA guardrail call that raises returns 503 and does not return the generated answer (fails closed)
UnitThe guard call uses source OUTPUT, and the generated answer is the text evaluated
LoggingEach call logs guard_source=output and guard_action at INFO with the request id; a blocked answer is logged at DEBUG only, and no INFO record contains answer text
Policy (manual, QA)aws bedrock-runtime apply-guardrail --source OUTPUT with the guardrail ARN and version, on a sample of harmful text, returns action GUARDRAIL_INTERVENED; on a neutral sentence it returns NONE
Integration (manual, QA)With the guardrail enabled, an ordinary question returns 200 with an answer and citations, and the log shows guard_source=output with guard_action=NONE
  • Story #70 (guardrail in Terraform) merged, and the guardrail enabled and applied in the QA environment for the manual rows
  • Story #71 (input guard) merged: the guardrail provider and its settings exist
  • Story #13 (/query endpoint) merged
  • A blocked answer is replaced by the configured message in a 200 response with empty citations and reason: content_policy_violation
  • A clean answer passes through unmodified
  • With the guardrail unset, the generated answer is returned unchanged
  • A guardrail failure returns 503 and never returns the generated answer
  • The blocked answer text appears only at DEBUG
  • Unit tests use Given / When / Then comment leads, and coverage meets the RAG006 floors
  • In QA, the policy blocks a harmful sample and passes a neutral one

Unit: CI and local. Policy and integration: manual against QA with the guardrail enabled. DEBUG logging is off in deployed environments.

pytest with httpx.AsyncClient, AWS CLI (aws bedrock-runtime apply-guardrail, aws logs filter-log-events), curl.

async def test_intervened_answer_is_replaced_by_the_message_with_a_200():
# Given a guardrail that blocks the generated answer
# When a clean question is posted
# Then the answer is the configured message, citations are empty, and reason is content_policy_violation
Terminal window
# Given the QA guardrail ARN and version
# When harmful sample text is evaluated as OUTPUT
aws bedrock-runtime apply-guardrail --guardrail-identifier $GUARDRAIL_ARN --guardrail-version $GUARDRAIL_VERSION --source OUTPUT --content '[{"text":{"text":"<harmful sample>"}}]'
# Then the response action is GUARDRAIL_INTERVENED
  • A model answer rarely trips the filters on demand, so the app-level blocked path is verified with a stubbed guardrail in unit tests, and the policy itself is verified through apply-guardrail directly. The two together stand in for an end-to-end blocked answer.
  • The blocked answer is logged at DEBUG so an operator can investigate. If DEBUG is enabled in a deployed environment, harmful text persists in logs; the exit criteria require it off.
  • The guard adds one Bedrock call after generation. Its latency is read from the structured log timestamps.

Safety (#44): a model response that drifts into a filtered category is caught before it crosses the API boundary (RAG013).