Use Guardrails for safeguarding generative AI applications built using custom or third-party models
Learn how the ApplyGuardrail API can provide a flexible way to integrate Guardrails with your generative AI applications

- Using different models outside of Bedrock (e.g. Amazon SageMaker)
- Enforcing Guardrails at different stages of a generative AI application.
- Testing Guardrails without invoking the model.
ApplyGuardrail
API makes it possible to evaluate user inputs and model responses independently at different stages of your generative AI applications. For example, in a RAG application, you can use Guardrails to filter potentially harmful user inputs before performing a search on your knowledge base. Then, you can also evaluate the final model response (after completing the search and the generation step).ApplyGuardrail
API, let's consider a generative AI application that acts as a virtual assistant to manage doctor appointments. Users invoke it using natural language, for example "I want an appointment for Dr. Smith". Note that this is an over-simplified version for demonstration purposes.
\b(?:Health\s*Insurance\s*ID|HIID|Insurance\s*ID)\s*[:=]?\s*([a-zA-Z0-9]+)\b
Health Insurance ID
is just an example, and this could be any sensitive data that needs to be blocked/masked/filtered.

ApplyGuardrail
API. I have used the AWS SDK for Python (boto3
), but it will work with any of the SDKs.source
to INPUT
, which means that the content to be evaluated is from a user (typically the LLM prompt). To evaluate the model output, the source should be set to OUTPUT
. You will see it in action in the next section.

- Guardrails did not block the input.
- Sagemaker endpoint was invoked and returned a response.
- Guardrails did not block the output either, and it was returned to the caller.
- Guardrails did not block the input - it was valid.
- Sagemaker endpoint was invoked and returned the response.
- Guardrails masked (the response wasn't completely blocked) the part of the output that contained the health insurance ID. You can see the details in logs in the part that says
'action': 'ANONYMIZED'
Patient {Health Insurance ID}
in the final response. Having the option to partially mask the output is quite flexible in these situations where the rest of the response is valid and you don't want to block it entirely.ApplyGuardrail
is a really flexible API that lets you evaluate input prompts and model responses for foundation models on Amazon Bedrock, as well as custom and third-party models, irrespective of there they are hosted. This allows you to use Guardrails for centralized governance across all your generative AI applications.Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.