# Guardrail Support for the Generic Bedrock Agent Test UI

## Introduction

In the blog post [Developing a Generic Streamlit UI to Test Amazon Bedrock Agents](https://blog.avangards.io/developing-a-generic-streamlit-ui-to-test-amazon-bedrock-agents), I shared the design and [source code](https://github.com/acwwat/amazon-bedrock-agent-test-ui) of a basic yet functional UI for testing Bedrock agents. I’ve since added [support for Knowledge Bases for Amazon Bedrock](https://blog.avangards.io/knowledge-base-support-for-the-generic-bedrock-agent-test-ui) by displaying citations and their details to match the functionality in the Bedrock console.

Recently I’ve started experimenting with [Guardrails for Amazon Bedrock](https://aws.amazon.com/bedrock/guardrails/), a feature that enables the implementation of safeguards for your generative AI applications based on specific use cases and responsible AI policies. As part of the blog post [A Guide to Effective Use of the Terraform AWS Cloud Control Provider](https://blog.avangards.io/a-guide-to-effective-use-of-the-terraform-aws-cloud-control-provider), I created a simple history-themed Bedrock agent with a guardrail that filters violent content. As I test a guardrail-enabled agent in the Bedrock console, I see additional traces showing whether guardrails have intervened as they assess the input and output, and if so, what policies were triggered:

![Post-guardrail trace](https://cdn.hashnode.com/res/hashnode/image/upload/v1726987174511/5142caca-ff97-4ee3-8fc0-7e53afeac3b8.png align="center")

With a bit of work, I have added similar support to the generic test UI and I am happy to share the updates in the [GitHub repository](https://github.com/acwwat/amazon-bedrock-agent-test-ui).

## Design overview

With the latest update, guardrail traces are now added to the pre-processing and post-processing trace sections in a manner similar to how they are displayed in the Bedrock console:

![Guardrail trace details](https://cdn.hashnode.com/res/hashnode/image/upload/v1726987561302/7baf19b0-2f6d-41ee-884b-3fbb6d7f8345.png align="center")

The [Boto3 `invoke_agent` method](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-agent-runtime/client/invoke_agent.html) provides a new `guardrailTrace` trace type that includes trace details used in the guardrail. Distinguishing between pre- and post-guardrail traces took a bit of work, as they need to be tracked as events are streamed in sequence. If a `guardrailTrace` shows up for the first time (naturally before `preProcessingTrace`s, it would be a pre-guardrail trace. A subsequent `guardrailTrace` that shows up (naturally after any `postProcessingTrace`s would be a post-guardrail trace. Then they must be displayed under the **Pre-Processing** and **Post-Processing** sections as a single JSON object unlike other traces where they are broken down into smaller JSON objects for readability.

## Summary

With the improvements to the generic test UI outlined in this post, you should now be able to test any Bedrock agents with an associated guardrail. I will be sure to incorporate support for new Agents for Amazon Bedrock features. If you find this blog post helpful, there is plenty more similar content at the [Avangards Blog](https://blog.avangards.io). Be sure to check them out!
