Skip to content

mirascope.core.gemini.call_response

This module contains the GeminiCallResponse class.

Usage Documentation

Calls

GeminiCallResponse

Bases: BaseCallResponse[GenerateContentResponse | AsyncGenerateContentResponse, GeminiTool, Tool, GeminiDynamicConfig, ContentsType, GeminiCallParams, ContentDict]

A convenience wrapper around the Gemini API response.

When calling the Gemini API using a function decorated with gemini_call, the response will be a GeminiCallResponse instance with properties that allow for more convenient access to commonly used attributes.

Example:

from mirascope.core import prompt_template
from mirascope.core.gemini import gemini_call


@gemini_call("gemini-1.5-flash")
@prompt_template("Recommend a {genre} book")
def recommend_book(genre: str):
    ...


response = recommend_book("fantasy")  # response is an `GeminiCallResponse` 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.

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

id property

id: str | None

Returns the id of the response.

google.generativeai does not return an id

usage property

usage: None

Returns the usage of the chat completion.

google.generativeai does not have Usage, so we return None

input_tokens property

input_tokens: None

Returns the number of input tokens.

output_tokens property

output_tokens: None

Returns the number of output tokens.

cost property

cost: float | None

Returns the cost of the call.

message_param property

message_param: ContentDict

Returns the models's response as a message parameter.

tools property

tools: list[GeminiTool] | None

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

tool property

tool: GeminiTool | 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[GeminiTool, object]]
) -> list[FunctionResponse]

Returns the tool message parameters for tool call results.

Parameters:

Name Type Description Default
tools_and_outputs list[tuple[GeminiTool, object]]

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

required

Returns:

Type Description
list[FunctionResponse]

The list of constructed FunctionResponse parameters.

Source code in mirascope/core/gemini/call_response.py
@classmethod
def tool_message_params(
    cls, tools_and_outputs: list[tuple[GeminiTool, object]]
) -> list[FunctionResponse]:
    """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 `FunctionResponse` parameters.
    """
    return [
        FunctionResponse(name=tool._name(), response={"result": output})
        for tool, output in tools_and_outputs
    ]