Skip to content

create_guardrail

create_guardrail(
    name: GuardrailName,
    blocked_input_messaging: GuardrailBlockedMessaging,
    blocked_outputs_messaging: GuardrailBlockedMessaging,
    *,
    config_overrides: Optional[BedrockClientConfig] = None,
    description: Optional[GuardrailDescription] = None,
    topic_policy_config: Optional[GuardrailTopicPolicyConfig] = None,
    content_policy_config: Optional[GuardrailContentPolicyConfig] = None,
    word_policy_config: Optional[GuardrailWordPolicyConfig] = None,
    sensitive_information_policy_config: Optional[
        GuardrailSensitiveInformationPolicyConfig
    ] = None,
    contextual_grounding_policy_config: Optional[
        GuardrailContextualGroundingPolicyConfig
    ] = None,
    automated_reasoning_policy_config: Optional[
        GuardrailAutomatedReasoningPolicyConfig
    ] = None,
    cross_region_config: Optional[GuardrailCrossRegionConfig] = None,
    kms_key_id: Optional[KmsKeyId] = None,
    tags: Optional[TagList] = None,
    client_request_token: Optional[IdempotencyToken] = None,
) -> CreateGuardrailResponse

Creates a guardrail to block topics and to implement safeguards for your generative AI applications.

You can configure the following policies in a guardrail to avoid undesirable and harmful content, filter out denied topics and words, and remove sensitive information for privacy protection.

  • Content filters - Adjust filter strengths to block input prompts or model responses containing harmful content.

  • Denied topics - Define a set of topics that are undesirable in the context of your application. These topics will be blocked if detected in user queries or model responses.

  • Word filters - Configure filters to block undesirable words, phrases, and profanity. Such words can include offensive terms, competitor names etc.

  • Sensitive information filters - Block or mask sensitive information such as personally identifiable information (PII) or custom regex in user inputs and model responses.

In addition to the above policies, you can also configure the messages to be returned to the user if a user input or model response is in violation of the policies defined in the guardrail.

For more information, see Amazon Bedrock Guardrails in the Amazon Bedrock User Guide.

Parameters:

Raises:

  • AccessDeniedException –

    The request is denied because of missing access permissions.

  • ConflictException –

    Error occurred because of a conflict while performing an operation.

  • InternalServerException –

    An internal server error occurred. Retry your request.

  • ResourceNotFoundException –

    The specified resource Amazon Resource Name (ARN) was not found. Check the Amazon Resource Name (ARN) and try your request again.

  • ServiceQuotaExceededException –

    The number of requests exceeds the service quota. Resubmit your request later.

  • ThrottlingException –

    The number of requests exceeds the limit. Resubmit your request later.

  • TooManyTagsException –

    The request contains more tags than can be associated with a resource (50 tags per resource). The maximum number of tags includes both existing tags and those included in your current request.

  • ValidationException –

    Input validation failed. Check your request parameters and retry the request.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Source code in docs_src/capo_bedrock/_services/bedrock.py
def create_guardrail(
    self,
    name: "capo_bedrock.types.guardrail_name.GuardrailName",
    blocked_input_messaging: "capo_bedrock.types.guardrail_blocked_messaging.GuardrailBlockedMessaging",
    blocked_outputs_messaging: "capo_bedrock.types.guardrail_blocked_messaging.GuardrailBlockedMessaging",
    *,
    config_overrides: Optional[BedrockClientConfig] = None,
    description: Optional[
        "capo_bedrock.types.guardrail_description.GuardrailDescription"
    ] = None,
    topic_policy_config: Optional[
        "capo_bedrock.types.guardrail_topic_policy_config.GuardrailTopicPolicyConfig"
    ] = None,
    content_policy_config: Optional[
        "capo_bedrock.types.guardrail_content_policy_config.GuardrailContentPolicyConfig"
    ] = None,
    word_policy_config: Optional[
        "capo_bedrock.types.guardrail_word_policy_config.GuardrailWordPolicyConfig"
    ] = None,
    sensitive_information_policy_config: Optional[
        "capo_bedrock.types.guardrail_sensitive_information_policy_config.GuardrailSensitiveInformationPolicyConfig"
    ] = None,
    contextual_grounding_policy_config: Optional[
        "capo_bedrock.types.guardrail_contextual_grounding_policy_config.GuardrailContextualGroundingPolicyConfig"
    ] = None,
    automated_reasoning_policy_config: Optional[
        "capo_bedrock.types.guardrail_automated_reasoning_policy_config.GuardrailAutomatedReasoningPolicyConfig"
    ] = None,
    cross_region_config: Optional[
        "capo_bedrock.types.guardrail_cross_region_config.GuardrailCrossRegionConfig"
    ] = None,
    kms_key_id: Optional["capo_bedrock.types.kms_key_id.KmsKeyId"] = None,
    tags: Optional["capo_bedrock.types.tag_list.TagList"] = None,
    client_request_token: Optional[
        "capo_bedrock.types.idempotency_token.IdempotencyToken"
    ] = None,
) -> "capo_bedrock.types.create_guardrail_response.CreateGuardrailResponse":
    """<p>Creates a guardrail to block topics and to implement safeguards for your generative AI applications.</p> <p>You can configure the following policies in a guardrail to avoid undesirable and harmful content, filter out denied topics and words, and remove sensitive information for privacy protection.</p> <ul> <li> <p> <b>Content filters</b> - Adjust filter strengths to block input prompts or model responses containing harmful content.</p> </li> <li> <p> <b>Denied topics</b> - Define a set of topics that are undesirable in the context of your application. These topics will be blocked if detected in user queries or model responses.</p> </li> <li> <p> <b>Word filters</b> - Configure filters to block undesirable words, phrases, and profanity. Such words can include offensive terms, competitor names etc.</p> </li> <li> <p> <b>Sensitive information filters</b> - Block or mask sensitive information such as personally identifiable information (PII) or custom regex in user inputs and model responses.</p> </li> </ul> <p>In addition to the above policies, you can also configure the messages to be returned to the user if a user input or model response is in violation of the policies defined in the guardrail.</p> <p>For more information, see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails.html">Amazon Bedrock Guardrails</a> in the <i>Amazon Bedrock User Guide</i>.</p>

    Args:
        name: <p>The name to give the guardrail.</p>
        description: <p>A description of the guardrail.</p>
        topic_policy_config: <p>The topic policies to configure for the guardrail.</p>
        content_policy_config: <p>The content filter policies to configure for the guardrail.</p>
        word_policy_config: <p>The word policy you configure for the guardrail.</p>
        sensitive_information_policy_config: <p>The sensitive information policy to configure for the guardrail.</p>
        contextual_grounding_policy_config: <p>The contextual grounding policy configuration used to create a guardrail.</p>
        automated_reasoning_policy_config: <p>Optional configuration for integrating Automated Reasoning policies with the new guardrail.</p>
        cross_region_config: <p>The system-defined guardrail profile that you're using with your guardrail. Guardrail profiles define the destination Amazon Web Services Regions where guardrail inference requests can be automatically routed.</p> <p>For more information, see the <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-cross-region.html">Amazon Bedrock User Guide</a>.</p>
        blocked_input_messaging: <p>The message to return when the guardrail blocks a prompt.</p>
        blocked_outputs_messaging: <p>The message to return when the guardrail blocks a model response.</p>
        kms_key_id: <p>The ARN of the KMS key that you use to encrypt the guardrail.</p>
        tags: <p>The tags that you want to attach to the guardrail. </p>
        client_request_token: <p>A unique, case-sensitive identifier to ensure that the API request completes no more than once. If this token matches a previous request, Amazon Bedrock ignores the request, but does not return an error. For more information, see <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/Run_Instance_Idempotency.html">Ensuring idempotency</a> in the <i>Amazon S3 User Guide</i>.</p>

    Raises:
        capo_bedrock.errors.access_denied_exception.AccessDeniedException: <p>The request is denied because of missing access permissions.</p>
        capo_bedrock.errors.conflict_exception.ConflictException: <p>Error occurred because of a conflict while performing an operation.</p>
        capo_bedrock.errors.internal_server_exception.InternalServerException: <p>An internal server error occurred. Retry your request.</p>
        capo_bedrock.errors.resource_not_found_exception.ResourceNotFoundException: <p>The specified resource Amazon Resource Name (ARN) was not found. Check the Amazon Resource Name (ARN) and try your request again.</p>
        capo_bedrock.errors.service_quota_exceeded_exception.ServiceQuotaExceededException: <p>The number of requests exceeds the service quota. Resubmit your request later.</p>
        capo_bedrock.errors.throttling_exception.ThrottlingException: <p>The number of requests exceeds the limit. Resubmit your request later.</p>
        capo_bedrock.errors.too_many_tags_exception.TooManyTagsException: <p>The request contains more tags than can be associated with a resource (50 tags per resource). The maximum number of tags includes both existing tags and those included in your current request. </p>
        capo_bedrock.errors.validation_exception.ValidationException: <p>Input validation failed. Check your request parameters and retry the request.</p>
        capo_bedrock.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    def _handler(
        req: "OperationRequest[capo_bedrock.types.create_guardrail_request.CreateGuardrailRequest]",
    ) -> OperationResponse[
        "capo_bedrock.types.create_guardrail_response.CreateGuardrailResponse"
    ]:
        import capo_bedrock._operations.amazon_bedrock_control_plane_service.create_guardrail

        output, http_response = (
            capo_bedrock._operations.amazon_bedrock_control_plane_service.create_guardrail.create_guardrail(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_bedrock.types.create_guardrail_request.CreateGuardrailRequest",
    ) -> dict[str, Any]:
        import capo_bedrock.types.create_guardrail_request

        return capo_bedrock.types.create_guardrail_request.redact_sensitive(input_)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging("CreateGuardrail", redact_input=_redact_input),
    )
    input_: capo_bedrock.types.create_guardrail_request.CreateGuardrailRequest = {
        "name": name,
        "blocked_input_messaging": blocked_input_messaging,
        "blocked_outputs_messaging": blocked_outputs_messaging,
    }
    if description is not None:
        input_["description"] = description
    if topic_policy_config is not None:
        input_["topic_policy_config"] = topic_policy_config
    if content_policy_config is not None:
        input_["content_policy_config"] = content_policy_config
    if word_policy_config is not None:
        input_["word_policy_config"] = word_policy_config
    if sensitive_information_policy_config is not None:
        input_["sensitive_information_policy_config"] = (
            sensitive_information_policy_config
        )
    if contextual_grounding_policy_config is not None:
        input_["contextual_grounding_policy_config"] = (
            contextual_grounding_policy_config
        )
    if automated_reasoning_policy_config is not None:
        input_["automated_reasoning_policy_config"] = (
            automated_reasoning_policy_config
        )
    if cross_region_config is not None:
        input_["cross_region_config"] = cross_region_config
    if kms_key_id is not None:
        input_["kms_key_id"] = kms_key_id
    if tags is not None:
        input_["tags"] = tags
    if client_request_token is None:
        client_request_token = str(uuid.uuid4())
    input_["client_request_token"] = client_request_token

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output