AI SDK 6

generateText

Generate a complete text response from an LLM using AI SDK 6.

generateText

Use generateText when you want the full response at once — no streaming.

Basic Usage

import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";

const model = openai("gpt-4o-mini");

const { text } = await generateText({
  model,
  prompt: "Write a haiku about sunrise.",
});

console.log(text);
// → "Golden rays appear,
//    Mountains wake in amber light,
//    A new day begins."

With a System Prompt

const { text } = await generateText({
  model,
  system: "You are a concise technical writer. Answer in bullet points.",
  prompt: "What are the benefits of TypeScript?",
});

With messages (chat history)

const { text } = await generateText({
  model,
  messages: [
    { role: "user",      content: "My name is Akash." },
    { role: "assistant", content: "Nice to meet you, Akash!" },
    { role: "user",      content: "What is my name?" },
  ],
});

console.log(text); // → "Your name is Akash."

Return Value

generateText returns a result object — destructure what you need:

FieldDescription
textThe generated string
usage{ inputTokens, outputTokens }
finishReasonWhy generation stopped ("stop", "length", etc.)

When to use

  • One-off queries
  • Non-interactive scripts
  • When you need the whole response before acting on it