Lab 02 — Put a Guardrail in front of a Bedrock model

Scaffold: 5/5 (almost complete). Lab 01 is finished and the guardrail is built. You connect them.

The scenario

The invoke-a-model function from Lab 01 works, and now it needs a safety layer. Compliance will not sign off while the app can be talked into giving financial advice, and support tickets pasted into prompts sometimes contain customer emails and phone numbers that should never reach the model or the logs. An Amazon Bedrock Guardrail can screen both the input and the output in one place.

The requirement

Every request and response goes through the guardrail:

What’s provided

Your task

In src/handler.py:

  1. Add guardrailConfig (identifier, version, "trace": "enabled") to the converse() call so the guardrail screens input and output.
  2. Read response["stopReason"] and return guarded: true when it equals guardrail_intervened.

Two small edits. The docstring has the exact shapes.

Run it

# Prerequisite: Model access enabled for your model, as in Lab 01.
./scripts/deploy.sh
./scripts/test.sh          # benign answers; the stock question is blocked;
                           # the third prompt comes back with its PII masked
./scripts/teardown.sh

What success looks like

Before you wire it, all three prompts answer and guarded is always false. After, the benign prompt still answers, and the financial-advice prompt comes back with the blocked message and guarded: true.

The third prompt is the redaction test. It asks the model to repeat a line containing a fake email address and phone number back word for word, so the answer shows you what the model was given: {EMAIL} and {PHONE} instead of the real values, because the guardrail masked them on the way in. That echo is the observation to make. The masking itself happens between your function and the model, where you cannot see it directly unless you return the guardrail trace as well.

If it fails

Reveal the solution

SRC=solution ./scripts/deploy.sh && ./scripts/test.sh

What you just learned

Next

Lab 03 — Answer questions from a Knowledge Base. You move from a bare model call to retrieval: a Knowledge Base is provided, and you write the retrieve-and-generate call that grounds answers in your documents.