generate()

The generate() method is used to interact with an agent to produce text or structured responses. This method accepts messages and an optional options object as parameters.

Parameters

messages

The messages parameter can be:

  • A single string
  • An array of strings
  • An array of message objects with role and content properties

The message object structure:

interface Message {
  role: 'system' | 'user' | 'assistant';
  content: string;
}

options (Optional)

An optional object that can include:

  • structuredOutput (or schema): An object defining the expected structure of the output. Can be a JSON Schema or a Zod schema.
  • Additional options like onStepFinish, maxSteps, threadId, resourceId, etc.

messages:

string | Array<string> | Array<Message>
The messages to be processed by the agent. Can be a single string, an array of strings, or an array of message objects with `role` and `content`.

options?:

object
Additional options for the `generate` method.
object | Zod schema
(step: string) => void
number
string
string
Array<Message>

Returns

The return value of the generate() method depends on the options provided, specifically the structuredOutput option.

PropertiesTable for Return Values

text?:

string
The generated text response. Present if `structuredOutput` is not provided.

object?:

object
The generated structured response based on the provided schema. Present if `structuredOutput` is provided.

toolCalls?:

Array<ToolCall>
The tool calls made during the generation process.

error?:

string
Error message if the generation fails.

ToolCall Structure

toolName:

string
The name of the tool invoked.

args:

any
The arguments passed to the tool.

For real-time streaming responses, see the stream() method documentation.


MIT 2025 © Nextra.