Skip to content

mirascope.core.bedrock.call_response

This module contains the BedrockCallResponse class.

Usage Documentation

Calls

BedrockCallResponse

Bases: BaseCallResponse[ConverseResponseTypeDef | ConverseResponseTypeDef, BedrockTool, ToolTypeDef, AsyncBedrockDynamicConfig | BedrockDynamicConfig, InternalBedrockMessageParam, BedrockCallParams, UserMessageTypeDef]

A convenience wrapper around the Bedrock ChatCompletion response.

When calling the Bedrock API using a function decorated with bedrock_call, the response will be an BedrockCallResponse instance with properties that allow for more convenience access to commonly used attributes.

Example:

from mirascope.core import prompt_template
from mirascope.core.bedrock import bedrock_call


@bedrock_call("anthropic.claude-3-haiku-20240307-v1:0")
@prompt_template("Recommend a {genre} book")
def recommend_book(genre: str):
    ...


response = recommend_book("fantasy")  # response is an `BedrockCallResponse` instance
print(response.content)

message property

message: SyncMessageTypeDef | AsyncMessageTypeDef | None

Returns the message of the response.

content property

content: str

Returns the content of the chat completion for the 0th choice.

finish_reasons property

finish_reasons: list[str]

Returns the finish reasons of the response.

model property

model: str

Returns the name of the response model.

id property

id: str

Returns the id of the response.

usage property

usage: TokenUsageTypeDef

Returns the usage of the chat completion.

input_tokens property

input_tokens: int | None

Returns the number of input tokens.

output_tokens property

output_tokens: int | None

Returns the number of output tokens.

cost property

cost: float | None

Returns the cost of the call.

message_param cached property

message_param: SerializeAsAny[AssistantMessageTypeDef]

Returns the assistants's response as a message parameter.

tools cached property

tools: list[BedrockTool] | None

Returns any available tool calls as their BedrockTool definition.

Raises:

Type Description
ValidationError

if a tool call doesn't match the tool's schema.

ValueError

if the model refused to response, in which case the error message will be the refusal.

tool cached property

tool: BedrockTool | None

Returns the 0th tool for the 0th choice message.

Raises:

Type Description
ValidationError

if the tool call doesn't match the tool's schema.

ValueError

if the model refused to response, in which case the error message will be the refusal.

tool_message_params classmethod

tool_message_params(
    tools_and_outputs: list[tuple[BedrockTool, str]]
) -> list[ToolResultBlockMessageTypeDef]

Returns the tool message parameters for tool call results.

Parameters:

Name Type Description Default
tools_and_outputs list[tuple[BedrockTool, str]]

The list of tools and their outputs from which the tool message parameters should be constructed.

required

Returns:

Type Description
list[ToolResultBlockMessageTypeDef]

The list of constructed ChatCompletionToolMessageParam parameters.

Source code in mirascope/core/bedrock/call_response.py
@classmethod
@transform_tool_outputs
def tool_message_params(
    cls, tools_and_outputs: list[tuple[BedrockTool, str]]
) -> list[ToolResultBlockMessageTypeDef]:
    """Returns the tool message parameters for tool call results.

    Args:
        tools_and_outputs: The list of tools and their outputs from which the tool
            message parameters should be constructed.

    Returns:
        The list of constructed `ChatCompletionToolMessageParam` parameters.
    """
    return [
        ToolResultBlockMessageTypeDef(
            role="user",
            content=[
                cast(
                    ToolResultBlockContentTypeDef,
                    {
                        "toolResult": {
                            "content": [{"text": output}],
                            "toolUseId": tool.tool_call["toolUse"]["toolUseId"],
                            "name": tool._name(),
                        }
                    },
                )
            ],
        )
        for tool, output in tools_and_outputs
    ]