Skip to main content

Overview

The generate() function generates a single response from a chat model. It’s the core function for synchronous text generation with support for tool calls, reasoning, and structured conversations.

Function signature

Parameters

ChatModel
required
The chat model instance to use for generation.
Message[]
required
Array of messages in the conversation. Must not be empty.
number
Sampling temperature (0-2). Higher values make output more random.
number
Maximum number of tokens to generate.
number
Nucleus sampling parameter (0-1).
ReasoningConfig
Configuration for extended thinking/reasoning capabilities.
ToolSet
Object mapping tool names to tool definitions. Enables the model to call functions.
ToolChoice
Controls how the model uses tools:
  • 'auto' - Model decides whether to use tools
  • 'none' - Model won’t use tools
  • 'required' - Model must use a tool
  • { type: 'tool', toolName: string } - Force specific tool
GenerateProviderOptions
Provider-specific options, namespaced by provider name (e.g. { openai: { user: '...' } }).
AbortSignal
AbortSignal for cancelling the request.

Return value

Returns a Promise<GenerateResult> with the following properties:
AssistantContentPart[]
Array of content parts in the response (text, reasoning, tool calls).
string | null
Concatenated text content from all text parts. null if no text was generated.
string | null
Concatenated reasoning content. null if no reasoning was generated.
ToolCall[]
Array of tool calls made by the model.
FinishReason
Why generation stopped: 'stop', 'length', 'tool-calls', 'content-filter', or 'unknown'.
ChatUsage
Token usage statistics including input/output tokens and cache details.

Examples

Basic text generation

With configuration

With tools

Multi-turn conversation

Error handling

Throws ValidationError if:
  • Messages array is empty
May also throw:
  • ProviderError if the provider returns an error during generation