generateTextUse generateText when you want the full response at once — no streaming.
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."
const { text } = await generateText({
model,
system: "You are a concise technical writer. Answer in bullet points.",
prompt: "What are the benefits of TypeScript?",
});
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."
generateText returns a result object — destructure what you need:
| Field | Description |
|---|---|
text | The generated string |
usage | { inputTokens, outputTokens } |
finishReason | Why generation stopped ("stop", "length", etc.) |