Skip to content

mirascope.core.vertex.call_response

This module contains the VertexCallResponse class.

Usage Documentation

Calls

VertexCallResponse

Bases: BaseCallResponse[GenerationResponse, VertexTool, Tool, VertexDynamicConfig, Content, VertexCallParams, Content]

A convenience wrapper around the Vertex AI GenerateContentResponse.

When calling the Vertex AI API using a function decorated with vertex_call, the response will be a VertexCallResponse instance with properties that allow for more convenient access to commonly used attributes.

Example:

from mirascope.core import prompt_template
from mirascope.core.vertex import vertex_call


@vertex_call("gemini-1.5-flash")
def recommend_book(genre: str) -> str:
    return f"Recommend a {genre} book"


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

content property

content: str

Returns the contained string content for the 0th choice.

finish_reasons property

finish_reasons: list[str]

Returns the finish reasons of the response.

model property

model: str

Returns the model name.

vertex does not return model, so we return the model provided by the user.

id property

id: str | None

Returns the id of the response.

vertex does not return an id

usage property

usage: UsageMetadata

Returns the usage of the chat completion.

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

Returns the models's response as a message parameter.

tools cached property

tools: list[VertexTool] | None

Returns the list of tools for the 0th candidate's 0th content part.

tool cached property

tool: VertexTool | None

Returns the 0th tool for the 0th candidate's 0th content part.

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[VertexTool, str]]
) -> list[Content]

Returns the tool message parameters for tool call results.

Parameters:

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

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

required

Returns:

Type Description
list[Content]

The list of constructed Content from the tool responses.

Source code in mirascope/core/vertex/call_response.py
@classmethod
@transform_tool_outputs
def tool_message_params(
    cls, tools_and_outputs: list[tuple[VertexTool, str]]
) -> list[Content]:
    """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 `Content` from the tool responses.
    """
    return [
        Content(
            role="user",
            parts=[
                Part.from_function_response(
                    name=tool._name(), response={"result": output}
                )
                for tool, output in tools_and_outputs
            ],
        )
    ]