GenerateResponse is the result from a generate() call and contains one or more generated candidate messages.

Type Parameters

  • O = unknown

Implements

Constructors

  • Type Parameters

    • O = unknown

    Parameters

    • response: {
          candidates?: {
              custom?: unknown;
              finishMessage?: string;
              finishReason:
                  | "length"
                  | "stop"
                  | "interrupted"
                  | "blocked"
                  | "unknown"
                  | "other";
              index: number;
              message: {
                  content: (
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text: string;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media: { contentType?: string; url: string };
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest: { input?: unknown; name: string; ref?: string };
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse: { name: string; output?: unknown; ref?: string };
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom: Record<string, any>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                  )[];
                  metadata?: Record<string, unknown>;
                  role: "user" | "model" | "tool" | "system";
              };
              usage?: {
                  custom?: Record<string, number>;
                  inputAudioFiles?: number;
                  inputCharacters?: number;
                  inputImages?: number;
                  inputTokens?: number;
                  inputVideos?: number;
                  outputAudioFiles?: number;
                  outputCharacters?: number;
                  outputImages?: number;
                  outputTokens?: number;
                  outputVideos?: number;
                  totalTokens?: number;
              };
          }[];
          custom?: unknown;
          finishMessage?: string;
          finishReason?: | "length"
          | "stop"
          | "interrupted"
          | "blocked"
          | "unknown"
          | "other";
          latencyMs?: number;
          message?: {
              content: (
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text: string;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media: { contentType?: string; url: string };
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest: { input?: unknown; name: string; ref?: string };
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse: { name: string; output?: unknown; ref?: string };
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom: Record<string, any>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
              )[];
              metadata?: Record<string, unknown>;
              role: "user" | "model" | "tool" | "system";
          };
          raw?: unknown;
          request?: {
              candidates?: number;
              config?: any;
              docs?: {
                  content: (
                      | { media?: undefined; text: string }
                      | {
                          media: { contentType?: (...) | (...); url: string };
                          text?: undefined;
                      }
                  )[];
                  metadata?: Record<string, any>;
              }[];
              messages: {
                  content: (
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text: string;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media: { contentType?: string; url: string };
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest: { input?: unknown; name: string; ref?: string };
                          toolResponse?: undefined;
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse: { name: string; output?: unknown; ref?: string };
                      }
                      | {
                          custom?: Record<string, unknown>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                      | {
                          custom: Record<string, any>;
                          data?: unknown;
                          media?: undefined;
                          metadata?: Record<string, unknown>;
                          text?: undefined;
                          toolRequest?: undefined;
                          toolResponse?: undefined;
                      }
                  )[];
                  metadata?: Record<string, unknown>;
                  role: "user" | "model" | "tool" | "system";
              }[];
              output?: {
                  constrained?: boolean;
                  contentType?: string;
                  format?: string;
                  instructions?: string;
                  schema?: Record<string, any>;
              };
              toolChoice?: "required"
              | "auto"
              | "none";
              tools?: {
                  description: string;
                  inputSchema?: null | Record<string, any>;
                  metadata?: Record<string, any>;
                  name: string;
                  outputSchema?: null | Record<string, any>;
              }[];
          };
          usage?: {
              custom?: Record<string, number>;
              inputAudioFiles?: number;
              inputCharacters?: number;
              inputImages?: number;
              inputTokens?: number;
              inputVideos?: number;
              outputAudioFiles?: number;
              outputCharacters?: number;
              outputImages?: number;
              outputTokens?: number;
              outputVideos?: number;
              totalTokens?: number;
          };
      }
    • Optionaloptions: { parser?: MessageParser<O>; request?: GenerateRequest<ZodTypeAny> }

    Returns GenerateResponse<O>

Properties

custom: unknown

Provider-specific response data.

finishMessage?: string

Additional information about why the model stopped generating, if any.

finishReason:
    | "length"
    | "stop"
    | "interrupted"
    | "blocked"
    | "unknown"
    | "other"

The reason generation stopped for this request.

message?: Message<O>

The generated message.

parser?: MessageParser<O>

The parser for output parsing of this response.

The request that generated this response.

usage: {
    custom?: Record<string, number>;
    inputAudioFiles?: number;
    inputCharacters?: number;
    inputImages?: number;
    inputTokens?: number;
    inputVideos?: number;
    outputAudioFiles?: number;
    outputCharacters?: number;
    outputImages?: number;
    outputTokens?: number;
    outputVideos?: number;
    totalTokens?: number;
}

Usage information.

Accessors

  • get data(): null | O
  • Returns the first detected data part of the generated message.

    Returns null | O

    The first data part detected in the candidate (if any).

  • get interrupts(): {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest: { input?: unknown; name: string; ref?: string };
        toolResponse?: undefined;
    }[]
  • Returns all tool requests annotated as interrupts found in the generated message.

    Returns {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest: { input?: unknown; name: string; ref?: string };
        toolResponse?: undefined;
    }[]

    A list of ToolRequestParts.

  • get media(): null | { contentType?: string; url: string }
  • Returns the first detected media part in the generated message. Useful for extracting (for example) an image from a generation expected to create one.

    Returns null | { contentType?: string; url: string }

    The first detected media part in the candidate.

  • get messages(): {
        content: (
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text: string;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media: { contentType?: string; url: string };
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest: { input?: unknown; name: string; ref?: string };
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse: { name: string; output?: unknown; ref?: string };
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom: Record<string, any>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
        )[];
        metadata?: Record<string, unknown>;
        role: "user" | "model" | "tool" | "system";
    }[]
  • Appends the message generated by the selected candidate to the messages already present in the generation request. The result of this method can be safely serialized to JSON for persistence in a database.

    Returns {
        content: (
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text: string;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media: { contentType?: string; url: string };
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest: { input?: unknown; name: string; ref?: string };
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse: { name: string; output?: unknown; ref?: string };
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom: Record<string, any>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
        )[];
        metadata?: Record<string, unknown>;
        role: "user" | "model" | "tool" | "system";
    }[]

    A serializable list of messages compatible with generate({history}).

  • get output(): null | O
  • If the generated message contains a data part, it is returned. Otherwise, the output() method extracts the first valid JSON object or array from the text contained in the selected candidate's message and returns it.

    Returns null | O

    The structured output contained in the selected candidate.

  • get raw(): unknown
  • Returns unknown

  • get text(): string
  • Concatenates all text parts present in the generated message with no delimiter.

    Returns string

    A string of all concatenated text parts.

  • get toolRequests(): {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest: { input?: unknown; name: string; ref?: string };
        toolResponse?: undefined;
    }[]
  • Returns all tool request found in the generated message.

    Returns {
        custom?: Record<string, unknown>;
        data?: unknown;
        media?: undefined;
        metadata?: Record<string, unknown>;
        text?: undefined;
        toolRequest: { input?: unknown; name: string; ref?: string };
        toolResponse?: undefined;
    }[]

    Array of all tool request found in the candidate.

Methods

  • Throws an error if the response does not contain valid output.

    Returns void

  • Returns {
        custom?: unknown;
        finishMessage?: string;
        finishReason:
            | "length"
            | "stop"
            | "interrupted"
            | "blocked"
            | "unknown"
            | "other";
        latencyMs?: number;
        message?: {
            content: (
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    text: string;
                    toolRequest?: undefined;
                    toolResponse?: undefined;
                }
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media: { contentType?: string; url: string };
                    metadata?: Record<string, unknown>;
                    text?: undefined;
                    toolRequest?: undefined;
                    toolResponse?: undefined;
                }
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    text?: undefined;
                    toolRequest: { input?: unknown; name: string; ref?: string };
                    toolResponse?: undefined;
                }
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    text?: undefined;
                    toolRequest?: undefined;
                    toolResponse: { name: string; output?: unknown; ref?: string };
                }
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    text?: undefined;
                    toolRequest?: undefined;
                    toolResponse?: undefined;
                }
                | {
                    custom: Record<string, any>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    text?: undefined;
                    toolRequest?: undefined;
                    toolResponse?: undefined;
                }
            )[];
            metadata?: Record<string, unknown>;
            role: "user" | "model" | "tool" | "system";
        };
        raw?: unknown;
        request?: {
            candidates?: number;
            config?: any;
            docs?: {
                content: (
                    | { media?: undefined; text: string }
                    | {
                        media: { contentType?: (...) | (...); url: string };
                        text?: undefined;
                    }
                )[];
                metadata?: Record<string, any>;
            }[];
            messages: {
                content: (
                    | {
                        custom?: Record<string, unknown>;
                        data?: unknown;
                        media?: undefined;
                        metadata?: Record<string, unknown>;
                        text: string;
                        toolRequest?: undefined;
                        toolResponse?: undefined;
                    }
                    | {
                        custom?: Record<string, unknown>;
                        data?: unknown;
                        media: { contentType?: string; url: string };
                        metadata?: Record<string, unknown>;
                        text?: undefined;
                        toolRequest?: undefined;
                        toolResponse?: undefined;
                    }
                    | {
                        custom?: Record<string, unknown>;
                        data?: unknown;
                        media?: undefined;
                        metadata?: Record<string, unknown>;
                        text?: undefined;
                        toolRequest: { input?: unknown; name: string; ref?: string };
                        toolResponse?: undefined;
                    }
                    | {
                        custom?: Record<string, unknown>;
                        data?: unknown;
                        media?: undefined;
                        metadata?: Record<string, unknown>;
                        text?: undefined;
                        toolRequest?: undefined;
                        toolResponse: { name: string; output?: unknown; ref?: string };
                    }
                    | {
                        custom?: Record<string, unknown>;
                        data?: unknown;
                        media?: undefined;
                        metadata?: Record<string, unknown>;
                        text?: undefined;
                        toolRequest?: undefined;
                        toolResponse?: undefined;
                    }
                    | {
                        custom: Record<string, any>;
                        data?: unknown;
                        media?: undefined;
                        metadata?: Record<string, unknown>;
                        text?: undefined;
                        toolRequest?: undefined;
                        toolResponse?: undefined;
                    }
                )[];
                metadata?: Record<string, unknown>;
                role: "user" | "model" | "tool" | "system";
            }[];
            output?: {
                constrained?: boolean;
                contentType?: string;
                format?: string;
                instructions?: string;
                schema?: Record<string, any>;
            };
            toolChoice?: "required"
            | "auto"
            | "none";
            tools?: {
                description: string;
                inputSchema?: null | Record<string, any>;
                metadata?: Record<string, any>;
                name: string;
                outputSchema?: null | Record<string, any>;
            }[];
        };
        usage?: {
            custom?: Record<string, number>;
            inputAudioFiles?: number;
            inputCharacters?: number;
            inputImages?: number;
            inputTokens?: number;
            inputVideos?: number;
            outputAudioFiles?: number;
            outputCharacters?: number;
            outputImages?: number;
            outputTokens?: number;
            outputVideos?: number;
            totalTokens?: number;
        };
    }