Back to roadmaps vercel-ai-sdk Course

Vercel AI SDK API Cheat Sheet

This quick reference guide summarizes the core API methods and hook options when building with the Vercel AI SDK.


1. Core Server APIs Reference

Method Target Behavior Typical Output Response
generateText Non-streaming text completion Promise<GenerateTextResult>
streamText Stream text tokens incrementally Promise<StreamTextResult>
generateObject Generate schema-compliant JSON Promise<GenerateObjectResult>
streamObject Stream schema-compliant JSON chunks Promise<StreamObjectResult>

2. Server-Side Execution Example

import { generateText, tool } from "ai";
import { z } from "zod";
import { openai } from "@ai-sdk/openai";

const result = await generateText({
  model: openai("gpt-4o"),
  prompt: "Lookup stock for Item A",
  tools: {
    checkStock: tool({
      description: "Get product stock count",
      parameters: z.object({ itemName: z.string() }),
      execute: async ({ itemName }) => ({ count: 42 })
    })
  }
});

3. Client useChat Hook return Values

Below are the most frequently used variables and handlers returned by the useChat() client hook:

  • messages: Array of message objects (includes id, role, content, toolInvocations).
  • input: State variable for the current text input field.
  • handleInputChange: Input field change handler function.
  • handleSubmit: Form submission handler function.
  • isLoading: Boolean flag representing active streaming connections.
  • reload: Function to re-trigger the last assistant response generation.
  • stop: Function to immediately interrupt the active response stream.
Published on Last updated: