Skip to content

mirascope.core.anthropic.call_response

This module contains the AnthropicCallResponse class.

Usage Documentation

Calls

AnthropicCallResponse

Bases: BaseCallResponse[Message, AnthropicTool, ToolParam, AsyncAnthropicDynamicConfig | AnthropicDynamicConfig, MessageParam, AnthropicCallParams, MessageParam]

A convenience wrapper around the Anthropic Message response.

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

Example:

from mirascope.core import prompt_template
from mirascope.core.anthropic import anthropic_call


@anthropic_call("claude-3-5-sonnet-20240620")
def recommend_book(genre: str) -> str:
    return f"Recommend a {genre} book"


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

content property

content: str

Returns the string text of the 0th text block.

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: Usage

Returns the usage of the message.

input_tokens property

input_tokens: int

Returns the number of input tokens.

output_tokens property

output_tokens: int

Returns the number of output tokens.

cost property

cost: float | None

Returns the cost of the call.

message_param cached property

message_param: SerializeAsAny[MessageParam]

Returns the assistants's response as a message parameter.

tools cached property

tools: list[AnthropicTool] | None

Returns any available tool calls as their AnthropicTool definition.

Raises:

Type Description
ValidationError

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

tool cached property

tool: AnthropicTool | 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.

tool_message_params classmethod

tool_message_params(
    tools_and_outputs: list[tuple[AnthropicTool, str]]
) -> list[MessageParam]

Returns the tool message parameters for tool call results.

Parameters:

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

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

required

Returns:

Type Description
list[MessageParam]

The list of constructed MessageParam parameters.

Source code in mirascope/core/anthropic/call_response.py
@classmethod
@transform_tool_outputs
def tool_message_params(
    cls, tools_and_outputs: list[tuple[AnthropicTool, str]]
) -> list[MessageParam]:
    """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 `MessageParam` parameters.
    """
    return [
        {
            "role": "user",
            "content": [
                ToolResultBlockParam(
                    tool_use_id=tool.tool_call.id,
                    type="tool_result",
                    content=[{"text": output, "type": "text"}],
                )
                for tool, output in tools_and_outputs
            ],
        }
    ]