Skip to content

ConverseTokensRequest dataclass

The inputs from a Converse API request for token counting.

This structure mirrors the input format for the Converse operation, allowing you to count tokens for conversation-based inference requests.

Source code in src/aws_sdk_bedrock_runtime/models.py
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
@dataclass(kw_only=True)
class ConverseTokensRequest:
    """The inputs from a `Converse` API request for token counting.

    This structure mirrors the input format for the `Converse` operation,
    allowing you to count tokens for conversation-based inference requests.
    """

    messages: list[Message] | None = None
    """An array of messages to count tokens for."""

    system: list[SystemContentBlock] | None = None
    """The system content blocks to count tokens for. System content provides
    instructions or context to the model about how it should behave or
    respond. The token count will include any system content provided.
    """

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

    def serialize_members(self, serializer: ShapeSerializer):
        if self.messages is not None:
            _serialize_messages(
                serializer,
                _SCHEMA_CONVERSE_TOKENS_REQUEST.members["messages"],
                self.messages,
            )

        if self.system is not None:
            _serialize_system_content_blocks(
                serializer,
                _SCHEMA_CONVERSE_TOKENS_REQUEST.members["system"],
                self.system,
            )

    @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["messages"] = _deserialize_messages(
                        de, _SCHEMA_CONVERSE_TOKENS_REQUEST.members["messages"]
                    )

                case 1:
                    kwargs["system"] = _deserialize_system_content_blocks(
                        de, _SCHEMA_CONVERSE_TOKENS_REQUEST.members["system"]
                    )

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

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

Attributes

messages class-attribute instance-attribute

messages: list[Message] | None = None

An array of messages to count tokens for.

system class-attribute instance-attribute

system: list[SystemContentBlock] | None = None

The system content blocks to count tokens for. System content provides instructions or context to the model about how it should behave or respond. The token count will include any system content provided.