generate()

The generate() method is used to interact with the language model 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

Message Object Structure

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

options (Optional)

messages:

string | string[] | Message[]
The messages to be processed by the LLM. 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.
string | JSONSchema7 | ZodSchema
(result: string) => Promise<void> | void
(step: string) => void
number
ToolsInput
string

Returns

text?:

string
The generated text response. Present when output is "text".

object?:

object
The generated structured response based on the provided schema. Present when a schema is provided.

error?:

string
Error message if the generation fails.

Examples

Basic Text Generation

const response = await llm.generate("What is AI?");
console.log(response.text);

Structured Output

import { z } from "zod";
 
const mySchema = z.object({
  definition: z.string(),
  examples: z.array(z.string()),
});
 
const response = await llm.generate(
  "Define machine learning and give examples.",
  {
    output: mySchema,
  },
);
 
console.log(response.object);

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


MIT 2025 © Nextra.