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
Total output allowance for the response, covering reasoning tokens and the visible answer. When omitted, the model’s output ceiling applies. If the value cannot hold the reasoning budget of the requested effort, the call throws a ValidationError. See maxTokens.
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
With tool discovery in use, toolName may only name a tool in tools. 'none' also keeps the model from discovering tools, and a discovery call satisfies 'required'.
ToolDiscoveryOptions
Enables tool discovery: core-ai declares a discovery tool, and the model’s calls to it arrive as tool-discovery-call parts in parts. Answer each one with a tool-discovery message before the next request. Keep it set consistently within a conversation; switching it costs a prompt cache rewrite. See Tool Discovery.
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, discovery 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, including calls to discovered tools. Discovery calls appear in discoveryCalls instead.
ToolDiscoveryCall[]
Calls from the model asking to discover tools, when the request sets toolDiscovery. Answer each one before the next request. See Tool Discovery.
FinishReason
Why generation stopped: 'stop', 'length', 'tool-calls', 'content-filter', or 'unknown'. 'tool-calls' means tool calls or discovery calls are pending: answer both toolCalls and discoveryCalls before the next request.
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
  • The request breaks a tool discovery rule, such as an unanswered discovery call. See Validation.
May also throw:
  • ProviderError if the provider returns an error during generation