Skip to content

apply_guardrail

Operation

apply_guardrail async

apply_guardrail(input: ApplyGuardrailInput, plugins: list[Plugin] | None = None) -> ApplyGuardrailOutput

The action to apply a guardrail.

For troubleshooting some of the common errors you might encounter when using the ApplyGuardrail API, see Troubleshooting Amazon Bedrock API Error Codes in the Amazon Bedrock User Guide

Parameters:

Name Type Description Default
input ApplyGuardrailInput

An instance of ApplyGuardrailInput.

required
plugins list[Plugin] | None

A list of callables that modify the configuration dynamically. Changes made by these plugins only apply for the duration of the operation execution and will not affect any other operation invocations.

None

Returns:

Type Description
ApplyGuardrailOutput

An instance of ApplyGuardrailOutput.

Source code in src/aws_sdk_bedrock_runtime/client.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
async def apply_guardrail(
    self, input: ApplyGuardrailInput, plugins: list[Plugin] | None = None
) -> ApplyGuardrailOutput:
    """The action to apply a guardrail.

    For troubleshooting some of the common errors you might encounter when
    using the `ApplyGuardrail` API, see [Troubleshooting Amazon Bedrock API
    Error
    Codes](https://docs.aws.amazon.com/bedrock/latest/userguide/troubleshooting-api-error-codes.html)
    in the Amazon Bedrock User Guide

    Args:
        input:
            An instance of `ApplyGuardrailInput`.
        plugins:
            A list of callables that modify the configuration dynamically.
            Changes made by these plugins only apply for the duration of the
            operation execution and will not affect any other operation
            invocations.

    Returns:
        An instance of `ApplyGuardrailOutput`.
    """
    operation_plugins: list[Plugin] = []
    if plugins:
        operation_plugins.extend(plugins)
    config = deepcopy(self._config)
    for plugin in operation_plugins:
        plugin(config)
    if config.protocol is None or config.transport is None:
        raise ExpectationNotMetError(
            "protocol and transport MUST be set on the config to make calls."
        )
    pipeline = RequestPipeline(protocol=config.protocol, transport=config.transport)
    call = ClientCall(
        input=input,
        operation=APPLY_GUARDRAIL,
        context=TypedProperties({"config": config}),
        interceptor=InterceptorChain(config.interceptors),
        auth_scheme_resolver=config.auth_scheme_resolver,
        supported_auth_schemes=config.auth_schemes,
        endpoint_resolver=config.endpoint_resolver,
        retry_strategy=config.retry_strategy,
    )

    return await pipeline(call)

Input

ApplyGuardrailInput dataclass

Dataclass for ApplyGuardrailInput structure.

Source code in src/aws_sdk_bedrock_runtime/models.py
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
@dataclass(kw_only=True)
class ApplyGuardrailInput:
    """Dataclass for ApplyGuardrailInput structure."""

    guardrail_identifier: str | None = None
    """The guardrail identifier used in the request to apply the guardrail."""

    guardrail_version: str | None = None
    """The guardrail version used in the request to apply the guardrail."""

    source: str | None = None
    """The source of data used in the request to apply the guardrail."""

    content: list[GuardrailContentBlock] | None = None
    """The content details used in the request to apply the guardrail."""

    output_scope: str | None = None
    """Specifies the scope of the output that you get in the response. Set to
    `FULL` to return the entire output, including any detected and
    non-detected entries in the response for enhanced debugging.

    Note that the full output scope doesn't apply to word filters or regex
    in sensitive information filters. It does apply to all other filtering
    policies, including sensitive information with filters that can detect
    personally identifiable information (PII).
    """

    def serialize(self, serializer: ShapeSerializer):
        serializer.write_struct(_SCHEMA_APPLY_GUARDRAIL_INPUT, self)

    def serialize_members(self, serializer: ShapeSerializer):
        if self.guardrail_identifier is not None:
            serializer.write_string(
                _SCHEMA_APPLY_GUARDRAIL_INPUT.members["guardrailIdentifier"],
                self.guardrail_identifier,
            )

        if self.guardrail_version is not None:
            serializer.write_string(
                _SCHEMA_APPLY_GUARDRAIL_INPUT.members["guardrailVersion"],
                self.guardrail_version,
            )

        if self.source is not None:
            serializer.write_string(
                _SCHEMA_APPLY_GUARDRAIL_INPUT.members["source"], self.source
            )

        if self.content is not None:
            _serialize_guardrail_content_block_list(
                serializer,
                _SCHEMA_APPLY_GUARDRAIL_INPUT.members["content"],
                self.content,
            )

        if self.output_scope is not None:
            serializer.write_string(
                _SCHEMA_APPLY_GUARDRAIL_INPUT.members["outputScope"], self.output_scope
            )

    @classmethod
    def deserialize(cls, deserializer: ShapeDeserializer) -> Self:
        return cls(**cls.deserialize_kwargs(deserializer))

    @classmethod
    def deserialize_kwargs(cls, deserializer: ShapeDeserializer) -> dict[str, Any]:
        kwargs: dict[str, Any] = {}

        def _consumer(schema: Schema, de: ShapeDeserializer) -> None:
            match schema.expect_member_index():
                case 0:
                    kwargs["guardrail_identifier"] = de.read_string(
                        _SCHEMA_APPLY_GUARDRAIL_INPUT.members["guardrailIdentifier"]
                    )

                case 1:
                    kwargs["guardrail_version"] = de.read_string(
                        _SCHEMA_APPLY_GUARDRAIL_INPUT.members["guardrailVersion"]
                    )

                case 2:
                    kwargs["source"] = de.read_string(
                        _SCHEMA_APPLY_GUARDRAIL_INPUT.members["source"]
                    )

                case 3:
                    kwargs["content"] = _deserialize_guardrail_content_block_list(
                        de, _SCHEMA_APPLY_GUARDRAIL_INPUT.members["content"]
                    )

                case 4:
                    kwargs["output_scope"] = de.read_string(
                        _SCHEMA_APPLY_GUARDRAIL_INPUT.members["outputScope"]
                    )

                case _:
                    logger.debug("Unexpected member schema: %s", schema)

        deserializer.read_struct(_SCHEMA_APPLY_GUARDRAIL_INPUT, consumer=_consumer)
        return kwargs

Attributes

content class-attribute instance-attribute
content: list[GuardrailContentBlock] | None = None

The content details used in the request to apply the guardrail.

guardrail_identifier class-attribute instance-attribute
guardrail_identifier: str | None = None

The guardrail identifier used in the request to apply the guardrail.

guardrail_version class-attribute instance-attribute
guardrail_version: str | None = None

The guardrail version used in the request to apply the guardrail.

output_scope class-attribute instance-attribute
output_scope: str | None = None

Specifies the scope of the output that you get in the response. Set to FULL to return the entire output, including any detected and non-detected entries in the response for enhanced debugging.

Note that the full output scope doesn't apply to word filters or regex in sensitive information filters. It does apply to all other filtering policies, including sensitive information with filters that can detect personally identifiable information (PII).

source class-attribute instance-attribute
source: str | None = None

The source of data used in the request to apply the guardrail.

Output

ApplyGuardrailOutput dataclass

Dataclass for ApplyGuardrailOutput structure.

Source code in src/aws_sdk_bedrock_runtime/models.py
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
@dataclass(kw_only=True)
class ApplyGuardrailOutput:
    """Dataclass for ApplyGuardrailOutput structure."""

    usage: GuardrailUsage
    """The usage details in the response from the guardrail."""

    action: str
    """The action taken in the response from the guardrail."""

    outputs: list[GuardrailOutputContent]
    """The output details in the response from the guardrail."""

    assessments: list[GuardrailAssessment]
    """The assessment details in the response from the guardrail."""

    action_reason: str | None = None
    """The reason for the action taken when harmful content is detected."""

    guardrail_coverage: GuardrailCoverage | None = None
    """The guardrail coverage details in the apply guardrail response."""

    def serialize(self, serializer: ShapeSerializer):
        serializer.write_struct(_SCHEMA_APPLY_GUARDRAIL_OUTPUT, self)

    def serialize_members(self, serializer: ShapeSerializer):
        serializer.write_struct(
            _SCHEMA_APPLY_GUARDRAIL_OUTPUT.members["usage"], self.usage
        )
        serializer.write_string(
            _SCHEMA_APPLY_GUARDRAIL_OUTPUT.members["action"], self.action
        )
        if self.action_reason is not None:
            serializer.write_string(
                _SCHEMA_APPLY_GUARDRAIL_OUTPUT.members["actionReason"],
                self.action_reason,
            )

        _serialize_guardrail_output_content_list(
            serializer, _SCHEMA_APPLY_GUARDRAIL_OUTPUT.members["outputs"], self.outputs
        )
        _serialize_guardrail_assessment_list(
            serializer,
            _SCHEMA_APPLY_GUARDRAIL_OUTPUT.members["assessments"],
            self.assessments,
        )
        if self.guardrail_coverage is not None:
            serializer.write_struct(
                _SCHEMA_APPLY_GUARDRAIL_OUTPUT.members["guardrailCoverage"],
                self.guardrail_coverage,
            )

    @classmethod
    def deserialize(cls, deserializer: ShapeDeserializer) -> Self:
        return cls(**cls.deserialize_kwargs(deserializer))

    @classmethod
    def deserialize_kwargs(cls, deserializer: ShapeDeserializer) -> dict[str, Any]:
        kwargs: dict[str, Any] = {}

        def _consumer(schema: Schema, de: ShapeDeserializer) -> None:
            match schema.expect_member_index():
                case 0:
                    kwargs["usage"] = GuardrailUsage.deserialize(de)

                case 1:
                    kwargs["action"] = de.read_string(
                        _SCHEMA_APPLY_GUARDRAIL_OUTPUT.members["action"]
                    )

                case 2:
                    kwargs["action_reason"] = de.read_string(
                        _SCHEMA_APPLY_GUARDRAIL_OUTPUT.members["actionReason"]
                    )

                case 3:
                    kwargs["outputs"] = _deserialize_guardrail_output_content_list(
                        de, _SCHEMA_APPLY_GUARDRAIL_OUTPUT.members["outputs"]
                    )

                case 4:
                    kwargs["assessments"] = _deserialize_guardrail_assessment_list(
                        de, _SCHEMA_APPLY_GUARDRAIL_OUTPUT.members["assessments"]
                    )

                case 5:
                    kwargs["guardrail_coverage"] = GuardrailCoverage.deserialize(de)

                case _:
                    logger.debug("Unexpected member schema: %s", schema)

        deserializer.read_struct(_SCHEMA_APPLY_GUARDRAIL_OUTPUT, consumer=_consumer)
        return kwargs

Attributes

action instance-attribute
action: str

The action taken in the response from the guardrail.

action_reason class-attribute instance-attribute
action_reason: str | None = None

The reason for the action taken when harmful content is detected.

assessments instance-attribute
assessments: list[GuardrailAssessment]

The assessment details in the response from the guardrail.

guardrail_coverage class-attribute instance-attribute
guardrail_coverage: GuardrailCoverage | None = None

The guardrail coverage details in the apply guardrail response.

outputs instance-attribute
outputs: list[GuardrailOutputContent]

The output details in the response from the guardrail.

usage instance-attribute

The usage details in the response from the guardrail.

Errors