Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Tools · Function

generateText

generateText produces text from a prompt with a language model and can call tools during execution. It is suited to non-interactive tasks such as automation, drafting, summarization, and tool-using agents.

How it works

Call generateText with a language model and either a string prompt or a message array. Await its result to access generated text, content, metadata, and other execution details; tools can also be supplied for agent-style workflows.

Syntax

generateText({ model, prompt, tools? }) => Promise<GenerateTextResult>

Parameters

NameTypeDescription
modelLanguageModelThe language model used to generate the response.
promptstring | Array<SystemModelMessage | UserModelMessage | AssistantModelMessage | ToolModelMessage>Input text or an array of system, user, assistant, and tool messages.
toolsToolSetTools available for model-directed tool calls.

Returns

A promise resolving to a result object containing generated text, content, and metadata.

Key points

  • generateText produces model output and can execute tools during the same request.
  • Use generateText for complete output, while streamText is the alternative for streaming responses.
  • Provide both a model and a prompt in the function options.
  • Typical uses include automation, drafting, summarization, and agent workflows.
  • The awaited result exposes generated output together with metadata.

Common mistakes

  • Use generateText for automation and other non-interactive work rather than interfaces that need incremental output.
  • Read the returned result object for generated content and metadata instead of treating the call as if it returned only a string.

Example: Generate text from a prompt

import { generateText } from 'ai';

const { text } = await generateText({
  model: 'anthropic/claude-sonnet-5.5',
  prompt: 'Summarize the benefits of automated testing in three sentences.',
});

console.log(text);

Generates text from a prompt with the selected language model and prints the returned text.

When to use it

Content generation

Create emails, reports, or other prose from supplied instructions.

Automation and summarization

Summarize documents or web-page content in a backend job.

Tool-using agents

Let a model select and invoke application tools.

  • streamText
  • Output.object
  • tool calling

Source: generateText. Checked 2026-10-15.