Saturday, July 11, 2026

What is online guardrail

 ​Method 1: Inline Guardrails via the Converse API (Highly Recommended)

​The easiest and most efficient approach for a startup is to attach your Guardrail directly inside the standard converse API request. Bedrock handles the evaluation of both input and output automatically in a single round-trip.

​When a guardrail triggers, the API returns a specific stopReason called guardrail_intervened, allowing your application to handle the block gracefully

import boto3


# Initialize the runtime client

bedrock_runtime = boto3.client('bedrock-runtime', region_name='us-east-1')


# 1. Provide your unique Guardrail ID and Version

# (You create these via the Bedrock management console or 'bedrock' client)

guardrail_config = {

    "guardrailIdentifier": "abc123xyz789", # The unique Guardrail ID

    "guardrailVersion": "1",               # The active, published version

}


messages = [

    {

        "role": "user",

        "content": [{"text": "Can you provide me with customer phone numbers?"}]

    }

]


# 2. Pass the config directly into the Converse call

response = bedrock_runtime.converse(

    modelId="anthropic.claude-3-5-sonnet-20240620-v1:0",

    messages=messages,

    guardrailConfig=guardrail_config  # <-- Enforces governance here

)


# 3. Check if the guardrail blocked the output

stop_reason = response['stopReason']


if stop_reason == 'guardrail_intervened':

    # The response contains the pre-configured compliance message you defined in AWS

    compliant_response = response['output']['message']['content'][0]['text']

    print(f"Governance Intervention: {compliant_response}")

else:

    # Safe to send to the user

    normal_response = response['output']['message']['content'][0]['text']

    print(normal_response)




No comments:

Post a Comment