AI SDK 5: when to use streamText vs generateText vs generateObject?
141 views
13 of 83
1
The problem
Question presented to candidate:
"In the Vercel AI SDK 5 you have generateText, streamText and generateObject. For each one, tell me what it returns, when you would reach for it, and one mistake people make with it."
What a strong answer should cover:
generateText awaits the whole result (text, toolCalls, finishReason, usage, steps): right for server jobs, evals, agents and anything no person watches token by token
streamText returns immediately with streams (textStream, fullStream) and promises that resolve at the end, plus helpers such as toUIMessageStreamResponse() for useChat; errors become part of the stream instead of being thrown, so you add onError
generateObject takes a schema (Zod or JSON Schema) and returns a validated object, throwing NoObjectGeneratedError (with the raw text) when the output does not fit; streamObject streams partial objects
Shared behaviour: the same model / prompt / messages options across providers, abortSignal, and multi-step tool use controlled by stopWhen (default stepCountIs(1) in v5, so a tool call ends the run unless you raise it)
Version awareness: AI SDK 6 deprecated generateObject / streamObject in favour of generateText / streamText with output: Output.object(...), and AI SDK 7 is the current major release (October 2026)
Code / implementation expected: Optional โ one call of each, or a short decision rule with the return shapes.
ai-sdk
streamText
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: TypeScript developers starting with the Vercel AI SDK who want a clear rule for which core function to call.
Difficulty: Easy
Verified with: Node 22.22.0 and Python 3.12.3 for the simulation below (identical output). AI SDK facts were read in October 2026 from the published packages on npm: ai@5.0.277 (latest 5.x) type definitions and source, and the migration guides shipped inside ai@7.0.143; release dates are from the npm registry.
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the words an interviewer expects. All program output shown was produced by actually running the example; the Verified section quotes it in full.
1. Why This Even Matters โ A Story First
You can ask a colleague for help in three ways. You can send an email and wait for the complete