AIP-C01 Input and Output Safety: Bedrock Guardrails, Prompt Injection Defence and Hallucination Checks
Input and output safety controls are the layers that stop harmful, manipulative or malformed content from reaching a foundation model and stop harmful, invented or badly formed responses from reaching users. Task 3.1 of AIP-C01 covers building them on AWS: Amazon Bedrock Guardrails for content, topics, prompt attacks and grounding; custom moderation workflows with Step Functions and Lambda; validation at the API layer with API Gateway and AWS WAF; accuracy verification with Knowledge Bases, contextual grounding, Automated Reasoning, confidence scoring and JSON Schema structured outputs; text-to-SQL for exact figures; Amazon Comprehend classifiers in a defense-in-depth design; and safety evaluations and adversarial testing. This lesson explains how each control behaves, where its limits are, and how to choose between workable designs.
On this page10 sections
- Input safety with Amazon Bedrock Guardrails: content filters, denied topics and word filters
- Real-time validation at the request boundary: API Gateway request validators and AWS WAF
- Custom moderation workflows with Step Functions and Lambda
- Filtering model output: ApplyGuardrail, blocked messages, images and streaming
- Prompt injection and jailbreak detection, including indirect injection
- Grounding answers with Knowledge Bases, citations and confidence scoring
- Contextual grounding checks versus Automated Reasoning checks
- Deterministic outputs: JSON Schema structured outputs and text-to-SQL
- Defense in depth: layering Comprehend, guardrails, Lambda and the API layer
- Testing safety: model evaluations for toxicity and automated adversarial testing
- Configure Bedrock Guardrails content filters, denied topics, word filters and blocked messages, tuning strength per direction and choosing the Classic or Standard tier.
- Reject malformed, oversized and flooding requests at the API layer with API Gateway request validators and AWS WAF, including the body inspection limit.
- Choose Step Functions Standard or Express workflows for custom moderation, including long human review with .waitForTaskToken.
- Filter responses for any model with ApplyGuardrail, and choose the right streaming mode, image modality and output pattern filters.
- Defend against direct and indirect prompt injection with the prompt attack filter, random input-tag suffixes and screening of tool results.
- Reduce hallucinations with Knowledge Bases citations, retrieval-score abstention, self-consistency, contextual grounding (grounding vs relevance) and Automated Reasoning checks.
- Make outputs deterministic with JSON Schema structured outputs and text-to-SQL, and test safety with Bedrock evaluations and ApplyGuardrail CI gates.
Input safety with Amazon Bedrock Guardrails: content filters, denied topics and word filters
The managed way to stop harmful or out-of-scope user input is an Amazon Bedrock guardrail: a reusable configuration of policies that evaluates the prompt before the model runs and, if a policy blocks it, returns a configured message without invoking the model. You attach it to a request with guardrailConfig (Converse/ConverseStream) or the guardrail ID and version (InvokeModel); when it intervenes on Converse, the response carries stopReason guardrail_intervened. Because it is configuration, not code, it beats a custom classifier, a Comprehend-plus-Lambda pipeline or a system-prompt instruction (which the model may simply ignore) whenever the requirement is "managed" or "least operational overhead".
A guardrail combines several policy types, and each answers a different question:
| Policy | What it detects | Use it when |
|---|---|---|
| Content filters | Harm categories: Hate, Insults, Sexual, Violence, Misconduct, plus the Prompt Attack category | Harmful content in prompts or responses, whatever the wording |
| Denied topics | A subject you describe in natural language (with optional sample phrases), detected semantically | Business scope: "never give investment advice", however the user phrases it |
| Word filters | Exact custom words or phrases, plus a managed profanity list | Specific strings such as competitor names; exact matching both over- and under-blocks a subject |
| Sensitive information filters | Built-in PII entity types and your own regular expressions; action block or mask | Patterns that must never appear, such as an organisation's own account-number format (PII as a privacy control is Task 3.2) |
| Contextual grounding and Automated Reasoning checks | Unsupported or rule-violating statements in responses | Hallucination control (see the grounding sections below) |
Strength is set per direction. Each content filter has an input strength (prompts) and an output strength (responses): NONE, LOW, MEDIUM or HIGH. Strength decides which confidence levels are blocked: HIGH blocks content classified as harmful with high, medium or low confidence, while LOW blocks only high-confidence content. So if one category over-blocks on one side only (for example, legitimate first-aid questions tripping a harm category on prompts), lower that category's input strength and leave its output strength and the other categories alone. LOW is not off: clear, high-confidence harm is still blocked.
Each filter also has an action: BLOCK, or NONE (shown in the console as Detect (no action)), which evaluates and reports in the trace but lets the content through.
Tiers and languages. Content filters, denied topics and some other policies come in a Classic and a Standard tier. Classic supports English, French and Spanish (word filters are limited to the same three). Standard supports a much longer language list (Japanese and German among the optimised ones), adds capabilities such as prompt leakage detection, and requires the guardrail to use cross-Region inference through a guardrail profile, so data may be processed in other Regions within the same geography. If a scenario needs non-English filtering and accepts cross-Region processing, Standard tier is the managed answer; translating everything to English first works but adds a service and latency.
Real-time validation at the request boundary: API Gateway request validators and AWS WAF
The cheapest place to reject a bad request is before any compute runs, at the API layer. Two managed controls do this for a GenAI endpoint: API Gateway request validation for the request's shape, and AWS WAF for size and volume abuse.
Request validators (REST APIs only). On an API Gateway REST API method you attach a request validator and a model, which is a JSON Schema for the body (required properties, types, maxLength and so on). A request that fails gets a 400 and the integration (the Lambda function that calls Bedrock) never runs, with no validation code to write. A Lambda authorizer is the wrong tool for this: it receives headers, query strings and context, not the request body, and it is custom code anyway.
| Capability | REST API | HTTP API |
|---|---|---|
| Request validation against a JSON Schema model | Yes | No |
| AWS WAF web ACL association | Yes | No |
| Usage plans and API keys | Yes | No |
| Cost and simplicity | Higher | Lower |
So a team that moved to an HTTP API to save money has also given up declarative body validation and WAF; getting them back at the API layer means a REST API.
AWS WAF on the stage. A web ACL associated with a REST API stage blocks requests before they reach the integration. A size constraint rule rejects oversized bodies, and a rate-based rule blocks request floods, aggregating by source IP by default. A usage plan, by contrast, throttles per API key, which does nothing against an anonymous flood from one address.
The body inspection limit is a trap. WAF only inspects the body up to the web ACL's body inspection limit, which defaults to 16 KB for API Gateway (and CloudFront, Cognito and similar resources) and can be raised in 16 KB steps up to 64 KB. Anything beyond the limit is handled by the rule's oversize handling: Continue (inspect only what was read), Match (treat the request as matching the rule) or No match. So a size rule whose threshold is above the inspection limit can never match while oversize handling is Continue: WAF never reads that far into the body. A size rule only works when the inspection limit covers its threshold, and oversize handling decides what happens to everything beyond what WAF read.
Custom moderation workflows with Step Functions and Lambda
When moderation needs more than a guardrail, for example your own scoring rules, several services or a human decision, orchestrate it with AWS Step Functions: Lambda functions score the prompt (often with Amazon Comprehend), a Choice state routes on the score, and borderline content goes to review. The workflow type decides what is possible.
| Standard workflow | Express workflow | |
|---|---|---|
| Maximum duration | Up to one year | Five minutes |
Callback (.waitForTaskToken) and .sync patterns | Supported | Not supported |
| Start and get the result in one call | No (StartExecution is asynchronous) | Yes, synchronous Express with StartSyncExecution |
| Billing | Per state transition | Per execution, duration and memory: far cheaper at high volume |
| Execution semantics | Exactly once | At least once (asynchronous) or at most once (synchronous) |
Human review that can take hours or days is a Standard workflow with a .waitForTaskToken task: the task sends a token to your moderation queue or UI, the execution pauses, and it resumes when the reviewer's decision comes back through SendTaskSuccess or SendTaskFailure. A Lambda function cannot wait that long (15-minute limit), and polling a table from Lambda wastes money. Amazon SageMaker Augmented AI (A2I) is no longer open to new customers, so do not design new human-review loops around it.
High-volume, sub-second checks the API must wait for are a synchronous Express workflow, which API Gateway can start directly with StartSyncExecution and which returns the verdict to the caller. At-most-once semantics are fine when re-running a check is harmless.
Both at once. A fast verdict for clear prompts plus a day-long review for borderline ones needs two workflows: a synchronous Express workflow scores and returns the verdict, and for borderline prompts it hands off (a fire-and-forget StartExecution) to a Standard workflow that waits on a task token, notifying the user later. The Express workflow cannot pause on a token itself, cannot use .sync to wait for the child, and a Standard workflow cannot be started synchronously.
Filtering model output: ApplyGuardrail, blocked messages, images and streaming
A guardrail filters responses with the same policies it applies to prompts, using each policy's output-side settings. If a response is blocked, the user receives the guardrail's blocked-response message; a blocked prompt returns the separate blocked-prompt message (blockedOutputsMessaging and blockedInputMessaging). Setting them to different texts lets you ask users to rephrase their own message but tell them an answer can't be shared, with configuration only. A Converse request carries one guardrail.
Guardrails for any model: ApplyGuardrail. The ApplyGuardrail API evaluates text against a guardrail without invoking a foundation model. You pass the content with source INPUT (a user prompt) or OUTPUT (a model response) and get back an action of GUARDRAIL_INTERVENED or NONE, plus the masked text or blocked message. That reuses one set of policies for a model on a SageMaker AI endpoint, a third-party model, or any pipeline stage, with no change to where the model is hosted. SageMaker's InvokeEndpoint has no guardrail parameter of its own.
Images. Content filters have input and output modalities. Text is on by default; the image modality must be selected explicitly. A guardrail created with default options will block a violent text prompt but let an image model's violent output through until you enable the IMAGE output modality on those filters. Text-only policies such as denied topics do not inspect images.
Custom patterns in output. A sensitive information filter with a custom regex can mask or block patterns such as an organisation's own reference-number formats in responses, with no Lambda code. Comprehend's PII detection does not know an organisation's own ID formats.
Streaming. With ConverseStream or InvokeModelWithResponseStream, the guardrail has a stream processing mode:
| Mode | Behaviour | Trade-off |
|---|---|---|
| Synchronous (default) | Buffers and scans chunks before sending them | Small added latency; masking works and nothing harmful is sent |
| Asynchronous | Sends chunks immediately and scans in the background | No added latency, but chunks may reach the user before a violation is found, and masking of sensitive information is not supported |
If a stream must never show unmasked or harmful content, use synchronous mode. Switching a mask action to block in asynchronous mode still leaks the chunk that triggered detection.
Prompt injection and jailbreak detection, including indirect injection
A prompt attack is an attempt to override the application's instructions: a jailbreak (bypass safety behaviour), a prompt injection ("ignore your previous instructions and...") or prompt leakage (extract the system prompt). The guardrail's prompt attack filter detects these on user input. It detects jailbreaks and injection on both tiers; prompt leakage detection is Standard tier only, which in turn needs cross-Region inference. Raising a Classic-tier strength cannot add a category, and word filters for phrases like "system prompt" are trivially evaded.
Input tags tell the guardrail which text is the user's. A developer's own system prompt often reads like an instruction override, so the filter must evaluate only user-supplied text:
- With InvokeModel / InvokeModelWithResponseStream, wrap the user's text in
<amazon-bedrock-guardrails-guardContent_suffix>tags and pass the matchingtagSuffix. Without input tags, prompt attacks are not filtered on these APIs, although the other filters still evaluate the whole prompt. That is why hate content can be blocked while "ignore your instructions" sails through. - The
tagSuffix(1 to 20 alphanumeric characters) should be a new random value for every request. A static suffix lets a user type the closing tag and append instructions outside the tagged region that the guardrail does not evaluate but the model still reads. - With Converse, the equivalent is a
guardContentblock around the user content to be evaluated.
Indirect prompt injection arrives through content the application feeds the model rather than through the user: web pages, retrieved documents, emails, tool output. In the Converse API a guardrail does not evaluate tool results (toolResult), tool definitions or the model's tool-call arguments, and the prompt attack filter documentation says the same. To catch instructions hidden in fetched content, screen it yourself before it reaches the model: call ApplyGuardrail with source INPUT on each tool result and return an error result (or drop the content) when it intervenes. System-prompt instructions to "ignore embedded instructions" reduce risk but detect nothing, and logging or tracing only tells you afterwards.
Input sanitisation complements detection: strip or escape markup and tag-like strings from untrusted text, enforce length limits, and keep untrusted content clearly delimited from instructions. Least privilege for the tools an agent can call limits the damage when an injection does get through.
Grounding answers with Knowledge Bases, citations and confidence scoring
The most effective way to reduce hallucination about your own data is to ground the model in retrieved documents and show where each statement came from. Amazon Bedrock Knowledge Bases ingests documents from Amazon S3 (and other sources), creates embeddings, stores them in a vector store, and with RetrieveAndGenerate retrieves relevant chunks, generates the answer from them and returns citations linking statements to source documents. That is grounding plus a fact-checking trail with no embedding pipeline to manage. Fine-tuning does not ground answers, and stuffing whole documents into the prompt is costly and unreliable.
Three operational details matter:
- Custom prompt templates. RetrieveAndGenerate builds its generation prompt from a template. The
$output_format_instructions$placeholder injects the formatting instructions that produce citations; a custom template that drops it keeps retrieval working but returns answers with no citations. ($search_results$is where retrieved chunks go.) - Freshness. New or changed S3 objects are only picked up by an ingestion job (a data source sync,
StartIngestionJob). Schedule it, for example with Amazon EventBridge Scheduler, rather than syncing by hand. - Which versions are retrieved. If superseded documents must stay in the source, attach metadata through a
.metadata.jsonsidecar file per document (for example astatusattribute) and apply a metadata filter such asequals status currentin the retrieval configuration.
Confidence scoring before generation. Retrieve returns a relevance score for each result. When many questions fall outside the corpus, calibrate a threshold on labelled answerable and unanswerable questions and abstain (hand off to a human, or say "I don't know") when the top score is below it. This saves the generation cost entirely, unlike checks that run on the generated answer. Asking the model to report its own confidence is not a reliable signal, and retrieving more passages does not create missing coverage.
Self-consistency when there is no reference. If answers come from the model's own knowledge, with no corpus or rule set to compare against, sample several answers at a non-zero temperature, embed them (for example with Amazon Titan Text Embeddings) and measure their semantic similarity. Answers that agree are more likely to be correct; divergent answers get flagged for review. Decoding at temperature 0 hides uncertainty instead of measuring it.
Contextual grounding checks versus Automated Reasoning checks
Guardrails offer two response-side checks for accuracy, and they answer different questions. A contextual grounding check scores whether a response is supported by the source text you supplied and whether it answers the user's query. An Automated Reasoning check validates a response against formal rules extracted from a policy document and explains which rules it satisfies or breaks.
Contextual grounding produces two scores, each with its own threshold from 0 to 0.99 (a threshold of 1 is not valid); a response scoring below a threshold is blocked:
- Grounding: is the response factually supported by the grounding source? Catches invented facts.
- Relevance: does the response address the query? Catches a response that faithfully restates the source but answers a different question than the one asked. Raising the grounding threshold cannot catch that.
The check runs on model responses and only when it has all three parts: a grounding source, a query and the response. With Converse or ApplyGuardrail you mark them with the grounding_source and query qualifiers on guardContent blocks; with InvokeModel you use the corresponding tags. Passages and questions sent as ordinary text produce no score, so no threshold change helps until they are qualified. On the Classic tier grounding is strict: any information not stated in the source, including reasonable inferences and friendly conversational phrasing, lowers the score. The Standard tier is designed for conversational and reasoning content (it needs cross-Region inference, and Standard-tier grounding is available in only some of the Regions that offer safeguard tiers). If correct answers are being blocked, move to Standard and lower the threshold, calibrating it on a labelled set of grounded and ungrounded answers so real hallucinations stay blocked.
| Contextual grounding check | Automated Reasoning check | |
|---|---|---|
| Reference | Source passages supplied with each request | A policy built in advance from a rules document |
| Output | Grounding and relevance scores against thresholds | Findings such as VALID or INVALID with the specific rules and variable assignments, plus unstated assumptions |
| Action | Blocks below the threshold | Detect only: your code must rewrite, withhold or ask for clarification |
| Streaming | Supported, with a caveat: relevance is judged per chunk, so a streamed response can reach the user before it is flagged | Not supported: validate the complete response |
| Best for | RAG answers that must stay within the retrieved text | Answers where interacting rules (eligibility, policies) must be provably followed and explained |
Because Automated Reasoning checks are detect-only and do not support streaming APIs, an assistant using ConverseStream gets answers with no findings, and INVALID answers still reach users unless code acts on them. The fix is to generate the complete answer (Converse), read the findings in the guardrail trace, and correct or withhold anything not VALID before sending it. Neither check helps when there is no reference at all, which is where self-consistency (previous section) fits.
Deterministic outputs: JSON Schema structured outputs and text-to-SQL
Two techniques remove whole classes of unreliable output: structured outputs force the response's shape, and text-to-SQL moves exact computation out of the model and into a database.
Structured outputs. For models that support it, Bedrock constrains generation to a JSON Schema so every response parses and contains the required fields. With Converse you set outputConfig.textFormat to a JSON schema; with InvokeModel it is output_config.format for Anthropic Claude models or response_format for open-weight models; for tool calls, strict tool use enforces the tool's input schema. This replaces prompt examples, temperature 0 and validate-and-retry loops, which reduce or detect malformed JSON but do not prevent it. Bedrock validates the schema first and returns a 400 ValidationException for unsupported features. Rules to know:
- Every object in the schema must set
"additionalProperties": false. - Numeric constraints (
minimum,maximum,multipleOf) and string length constraints are not supported; check ranges in the code that reads the result. Enums of strings and arrays of strings are fine. - The first request with a new schema is slower while the schema is compiled into a grammar, so allow for it in client timeouts. AWS notes the first compile can take up to about a minute and a half and recommends a generous client read timeout. Structured outputs also cannot be combined with Anthropic citations.
A schema guarantees format, not truth. When generated values must match data that changes (identifiers, balances, availability), generate them as schema-bound fields and then have a Lambda post-processing step check each one against the system of record before the response is sent. A schema can only constrain shape and fixed value lists; it knows nothing about the current state of a database.
Text-to-SQL for exact figures. A model totalling rows from retrieved text chunks will sometimes get the arithmetic wrong, however many chunks you retrieve. A structured knowledge base connects Bedrock Knowledge Bases to a structured store with Amazon Redshift as the query engine: it turns the question into SQL, the database computes the result, and the model writes the answer. The GenerateQuery API returns the SQL alone if you want to run it yourself. Consistency for business terms comes from the query generation configuration: table and column descriptions (for example, which column a business term such as "active customer" maps to), table inclusion or exclusion, and curated queries that pair example questions with the intended SQL.
Generated SQL is untrusted. Prompt instructions, input word filters and a prompt attack filter all try to persuade or detect; none constrains the SQL. Enforce safety outside the model: run the SQL as a database user with only SELECT on the approved views, and validate the statement before execution (a single SELECT that references only allowlisted objects).
Defense in depth: layering Comprehend, guardrails, Lambda and the API layer
Defense in depth means no single control is trusted to catch everything: each layer stops a different failure, so a bypass of one is caught by another. A typical GenAI request path has four layers:
- API layer: AWS WAF (size, rate, IP reputation) and API Gateway request validation reject abuse and malformed requests before any compute runs.
- Pre-processing: Lambda checks such as length, format, sanitisation and a classifier score (for example Amazon Comprehend) before the model is called.
- Model-based guardrails: Bedrock Guardrails on input and output (content filters, denied topics, prompt attack, grounding).
- Post-processing: Lambda validation of the response (schema, lookups against a system of record, business rules) before it is returned through the API.
Prefer the managed control at each layer when the scenario asks for least custom code: WAF instead of rate limiting in Lambda, a guardrail regex filter instead of a Lambda regex, request validators instead of validation code.
Choosing a safety classifier.
| Need | Service | Notes |
|---|---|---|
| Numeric per-category toxicity scores for any text, your own thresholds | Amazon Comprehend DetectToxicContent | English only; up to 10 text segments of up to 1 KB each per call; labels such as PROFANITY, HATE_SPEECH, INSULT, GRAPHIC, HARASSMENT_OR_ABUSE, SEXUAL, VIOLENCE_OR_THREAT plus an overall toxicity score; works for any model |
| Block harmful content with managed thresholds | Bedrock Guardrails content filters (ApplyGuardrail for non-Bedrock models) | Configuration only; Standard tier for non-English |
| Detect jailbreaks and prompt injection | Guardrails prompt attack filter (ApplyGuardrail source INPUT) | Benign-sounding jailbreaks are not "toxic", so toxicity scores miss them |
| Image moderation outside a guardrail | Amazon Rekognition DetectModerationLabels | Custom code around it; a guardrail's image modality avoids that |
| PII detection | Comprehend DetectPiiEntities or guardrail sensitive information filters | A privacy control (Task 3.2), not a toxicity classifier |
Amazon Comprehend prompt safety classification is closed to new customers (along with Comprehend topic modeling and event detection; accounts that used them recently keep access). AWS's migration path is a guardrail with the prompt attack filter called through ApplyGuardrail. Training a custom classifier for jailbreaks adds effort without matching the managed filter.
Testing safety: model evaluations for toxicity and automated adversarial testing
Safety controls need measurement before release and after every change. Amazon Bedrock evaluations score model outputs; ApplyGuardrail in a pipeline tests the guardrail itself.
Bedrock model evaluation job types:
| Job type | How it scores | Safety-relevant metrics |
|---|---|---|
| LLM-as-a-judge | A judge model scores each response; automated, no workforce | Responsible-AI metrics Harmfulness, Stereotyping and Refusal, alongside quality metrics such as Correctness and Faithfulness |
| Programmatic (automatic) | Algorithmic metrics, often on built-in datasets | Toxicity (for example on RealToxicityPrompts), Robustness, Accuracy: scores the bare model on a public dataset |
| Human-based | Your own or an AWS-managed work team rates responses | Any criteria you define; needs a workforce |
Use the metric where the failure is a risk: Harmfulness on responses to red-team prompts, and Refusal on responses to legitimate prompts to measure over-blocking. To compare candidate models, run a judge job with your own prompt dataset against each model.
Evaluating responses produced elsewhere. An evaluation dataset can include each prompt's logged response in modelResponses (a response plus a modelIdentifier, one identifier per job). Bedrock then skips invoking a model and judges exactly those responses, so you can score a model on a SageMaker AI endpoint, or the final responses after a guardrail and post-processing, without moving the model or paying for inference again.
Automated adversarial testing of guardrail changes. Because ApplyGuardrail does not invoke a model, a CI/CD stage (for example in AWS CodeBuild) can run thousands of known attack prompts and a set of benign prompts against the guardrail's working DRAFT, compute the attack block rate and the false-block rate, and fail the build on any regression. Only a passing build creates a new immutable guardrail version for production.
Shadow trials on real traffic. To learn how many real requests a new policy would affect without changing anyone's experience, add it with the Detect (no action) action, enable the guardrail trace, analyse the assessments, then switch to Block. Detect mode is the only action that records what a policy would do while letting every request through unchanged.
Tip. Task 3.1 questions are production scenarios in which a GenAI application lets something harmful, invented or malformed through, or blocks too much, and several constraints are stated at once: no custom code or least operational overhead, no change to where the model is hosted, no inference cost in testing, zero impact on users, languages to support, whether data may cross Regions, or exact repeatable figures. Most wrong options are real AWS controls that fail one requirement: a filter that cannot see tool results or untagged input, a check that runs after generation when cost must be saved, a feature that needs the Standard tier, a workflow type that cannot wait or cannot answer synchronously, a REST API feature on an HTTP API, or a model-based check where an exact database lookup is needed. Expect multiple-response items that pair two complementary controls, such as one at the API layer and one in the guardrail, or one for freshness and one for retrieval filtering.
- Harmful or out-of-scope input with no custom code: a Bedrock guardrail. Denied topics for subjects in any wording, word filters for exact strings, content filters for harm.
- Content filter strength is set separately for prompts and responses; LOW still blocks high-confidence harm.
- Non-English filtering and prompt leakage detection need the Standard tier, which requires cross-Region inference.
- API Gateway request validators and AWS WAF are REST API features; WAF sees only up to its body inspection limit, so pair the limit with oversize handling Match.
- Human review that takes hours: Step Functions Standard + .waitForTaskToken. Sub-second verdicts the API waits on: synchronous Express via StartSyncExecution. A2I is closed to new customers.
- ApplyGuardrail (source INPUT or OUTPUT) applies guardrail policies to any model or content without invoking a model.
- Streaming that must never leak or must mask: synchronous guardrail mode.
- InvokeModel needs input tags with a fresh random tagSuffix for prompt attack filtering; Converse guardrails do not read tool results, so screen them with ApplyGuardrail.
- Contextual grounding needs grounding_source and query marked; grounding catches invented facts, relevance catches off-question answers. Automated Reasoning is detect-only and non-streaming.
- JSON Schema structured outputs guarantee shape, not truth; exact values come from the database (text-to-SQL, lookups) behind least-privilege access.
Frequently asked questions
What is the difference between a denied topic and a word filter in Amazon Bedrock Guardrails?
A denied topic is defined in natural language with optional sample phrases and is detected semantically, so it catches a subject such as investment advice however the user words it. A word filter blocks exact words or phrases, which misses paraphrases and blocks harmless uses of the same words. Use denied topics for business scope and word filters for specific strings.
How do I apply Bedrock Guardrails to a model that is not hosted on Amazon Bedrock?
Call the ApplyGuardrail API, which evaluates text against a guardrail without invoking any model. Send the user's prompt with source INPUT before calling your model and the model's response with source OUTPUT before returning it. The API returns GUARDRAIL_INTERVENED or NONE along with masked text or the blocked message, so the same policies protect a SageMaker AI endpoint or a third-party model.
Why does the Bedrock prompt attack filter not catch prompt injection with InvokeModel?
With InvokeModel and InvokeModelWithResponseStream, the prompt attack filter only evaluates text wrapped in guardrail input tags; without tags, prompt attacks are not filtered even though other filters still run. Wrap the user's text in amazon-bedrock-guardrails-guardContent tags with a tagSuffix that is a new random alphanumeric value on every request, so users cannot close the tag and inject text outside it.
Do Bedrock Guardrails protect against indirect prompt injection from tool results?
Not automatically. With the Converse API, a guardrail does not evaluate tool results, tool definitions or tool-call arguments, so instructions hidden in a fetched web page or document pass through. Screen untrusted tool output yourself with ApplyGuardrail (source INPUT) before giving it to the model, and keep the tools an agent can call to least privilege.
What is the difference between grounding and relevance in a contextual grounding check?
Grounding measures whether a response is supported by the source passages supplied with the request, so it catches invented facts. Relevance measures whether the response answers the user's query, so it catches an accurate answer to the wrong question. Each has its own threshold between 0 and 0.99, and the check only runs when the source and query are marked with the grounding_source and query qualifiers or tags.
When should I use Automated Reasoning checks instead of contextual grounding checks?
Use Automated Reasoning checks when answers must be provably consistent with a set of interacting rules, such as eligibility or policy documents, and you need findings that name the specific rules an answer satisfies or contradicts. They work in detect mode only and do not support streaming, so validate each complete answer and act on the findings in code. Contextual grounding checks suit RAG answers that should stay within the retrieved passages.
Should I use synchronous or asynchronous guardrail streaming mode?
Use synchronous mode, the default, when no harmful or unmasked content may ever be shown: chunks are scanned before they are sent, at the cost of a little latency. Asynchronous mode sends chunks immediately and scans in the background, so violating content can reach the user before it is detected, and sensitive-information masking is not supported in that mode.
How can I test Bedrock guardrail changes automatically before deploying them?
Add a CI/CD stage that runs a set of known jailbreak and injection prompts and a set of normal prompts through the ApplyGuardrail API against the guardrail's draft, which costs no model inference. Fail the build if the attack block rate drops or the false-block rate rises, and create a new guardrail version only when it passes. To gauge real-traffic impact first, run new policies with the Detect (no action) setting and analyse the trace.
Source
This lesson covers the "AI Safety, Security, and Governance" domain of the official AIP-C01 exam guide. Vendors revise their guides — check the source for the current version.
- AWS Certified Generative AI Developer – Professional (AIP-C01) exam guide — Amazon Web Services
Sign up free to mark lessons complete, bookmark topics and track your exam readiness.