Models API
The Models API provides functions for interacting with generative AI models.
generate()
Generate calls a generative model based on the provided prompt and configuration.
Signature:
Parameters
Generation options The model to use (name string or ModelAction). If not provided, uses the default model.
User prompt - can be text or multipart content
System instructions for the model
Conversation history for multi-turn interactions
Tools the model can call. Model will automatically invoke tools unless returnToolRequests is true.
If true, return tool requests without executing them
Model configuration including:
temperature: Number (0-2) - Sampling temperature
maxOutputTokens: Maximum tokens to generate
topK: Top-K sampling parameter
topP: Top-P (nucleus) sampling parameter
stopSequences: Array of sequences that stop generation
Output schema specification
schema: Zod schema for structured output
format: Output format (e.g., ‘json’, ‘text’)
Documents for retrieval-augmented generation (RAG)
Returns
Generation response object The generated text content
Parses and returns structured output according to the schema
Complete conversation including user prompts, model responses, and tool calls
Token usage statistics:
inputTokens: Number of input tokens
outputTokens: Number of generated tokens
totalTokens: Total tokens used
Why generation stopped (e.g., ‘stop’, ‘length’, ‘safety’)
Additional details about finish reason
Example
generateStream()
Streaming version of generate() that yields response chunks as they are generated.
Signature:
Parameters
Accepts the same GenerateOptions as generate(), plus:
Callback invoked for each chunk:
Returns
Streaming response object response Promise<GenerateResponse>
Promise that resolves to the final complete response
stream Channel<GenerateResponseChunk>
Async iterable channel of response chunks. Each chunk contains:
text: Incremental text
content: Array of content parts
usage: Cumulative token usage
Example
modelRef()
Creates a reference to a model by name.
Signature:
Parameters
Model name (e.g., ‘googleai/gemini-2.0-flash-exp’)
Default configuration for this model reference
Model metadata (capabilities, version, etc.)
Returns
A reference object that can be passed to generate() or other model functions
Example
Types
GenerateRequest
The raw request object sent to a model.
GenerateResponse
The response from a model.
Part
Content part within a message.
MessageData
A message in the conversation.
See Also