This file provides context for AI coding assistants (Cursor, GitHub Copilot, Claude Code, etc.) working with the Vercel AI SDK repository.
The AI SDK by Vercel is a TypeScript/JavaScript SDK for building AI-powered applications with Large Language Models (LLMs). It provides a unified interface for multiple AI providers and framework integrations.
- Repository: https://github.com/vercel/ai
- Documentation: https://ai-sdk.dev/docs
- License: Apache-2.0
This is a monorepo using pnpm workspaces and Turborepo.
| Directory | Description |
|---|---|
packages/ai |
Main SDK package (ai on npm) |
packages/provider |
Provider interface specifications (@ai-sdk/provider) |
packages/provider-utils |
Shared utilities for providers and core (@ai-sdk/provider-utils) |
packages/<provider> |
AI provider implementations (openai, anthropic, google, azure, amazon-bedrock, etc.) |
packages/<framework> |
UI framework integrations (react, vue, svelte, angular, rsc) |
packages/codemod |
Automated migrations for major releases |
examples/ |
Example applications (ai-functions, next-openai, etc.) |
content/ |
Documentation source files (MDX) |
contributing/ |
Contributor guides and documentation |
tools/ |
Internal tooling (eslint-config, tsconfig) |
ai ─────────────────┬──▶ @ai-sdk/provider-utils ──▶ @ai-sdk/provider
│
@ai-sdk/<provider> ─┴──▶ @ai-sdk/provider-utils ──▶ @ai-sdk/provider
- Node.js: v18, v20, or v22 (v22 recommended for development)
- pnpm: v10+ (
npm install -g pnpm@10)
pnpm install # Install all dependencies
pnpm build # Build all packages| Command | Description |
|---|---|
pnpm install |
Install dependencies |
pnpm build |
Build all packages |
pnpm test |
Run all tests (excludes examples) |
pnpm lint |
Run linting |
pnpm prettier-fix |
Fix formatting issues |
pnpm prettier-check |
Check formatting |
pnpm type-check:full |
TypeScript type checking (includes examples) |
pnpm changeset |
Add a changeset for your PR |
pnpm update-references |
Update tsconfig.json references after adding package dependencies |
Run these from within a package directory (e.g., packages/ai):
| Command | Description |
|---|---|
pnpm build |
Build the package |
pnpm build:watch |
Build with watch mode |
pnpm test |
Run all tests (node + edge) |
pnpm test:node |
Run Node.js tests only |
pnpm test:edge |
Run Edge runtime tests only |
pnpm test:watch |
Run tests in watch mode |
cd examples/ai-functions
pnpm tsx src/stream-text/openai/basic.ts # Run a specific example- Place examples under
examples/ai-functions/src/<function>/<provider>/ - Use
basic.tsfor the provider entry example file - Place all other examples in the same provider folder using descriptive
kebab-casefile names - Do not create flat top-level provider files like
src/stream-text/openai.ts
| Function | Purpose | Package |
|---|---|---|
generateText |
Generate text completion | ai |
streamText |
Stream text completion | ai |
generateObject |
Generate structured output | ai |
streamObject |
Stream structured output | ai |
embed / embedMany |
Generate embeddings | ai |
generateImage |
Generate images | ai |
tool |
Define a tool | ai |
jsonSchema / zodSchema |
Define schemas | ai |
| What | Import From |
|---|---|
Core functions (generateText, streamText) |
ai |
Tool/schema utilities (tool, jsonSchema) |
ai |
| Provider implementations | @ai-sdk/<provider> (e.g., @ai-sdk/openai) |
| Error classes | ai (re-exports from @ai-sdk/provider) |
Provider type interfaces (LanguageModelV3) |
@ai-sdk/provider |
| Provider implementation utilities | @ai-sdk/provider-utils |
- Tool: Prettier
- Config: Defined in root
package.json - Settings: Single quotes, trailing commas, 2-space indentation, no tabs
- Pre-commit hook: Automatically formats staged files on commit via
lint-staged. Ifpackage.jsonchanges are staged,pnpm installruns automatically
- Framework: Vitest
- Test files:
*.test.tsalongside source files - Type tests:
*.test-d.tsfor type-level tests - Fixtures: Store in
__fixtures__subfolders - Snapshots: Store in
__snapshots__subfolders
The SDK supports both Zod 3 and Zod 4. Use correct imports:
// For Zod 3 (compatibility code only)
import * as z3 from 'zod/v3';
// For Zod 4
import * as z4 from 'zod/v4';
// Use z4.core.$ZodType for type referencesNever use JSON.parse directly in production code to prevent security risks.
Instead use parseJSON or safeParseJSON from @ai-sdk/provider-utils.
Always run type checking after making code changes:
pnpm type-check:full # Run from workspace rootThis ensures your changes don't introduce type errors across the codebase, including examples.
- Source files:
kebab-case.ts - Test files:
kebab-case.test.ts - Type test files:
kebab-case.test-d.ts - React/UI components:
kebab-case.tsx
Errors extend AISDKError from @ai-sdk/provider and use a marker pattern for instanceof checks:
import { AISDKError } from '@ai-sdk/provider';
const name = 'AI_MyError';
const marker = `vercel.ai.error.${name}`;
const symbol = Symbol.for(marker);
export class MyError extends AISDKError {
private readonly [symbol] = true; // used in isInstance
constructor({ message, cause }: { message: string; cause?: unknown }) {
super({ name, message, cause });
}
static isInstance(error: unknown): error is MyError {
return AISDKError.hasMarker(error, marker);
}
}For an overview of the project's key philosophies that guide decision making, see contributing/project-philosophies.md.
The SDK uses a layered provider architecture following the adapter pattern:
- Specifications (
@ai-sdk/provider): Defines interfaces likeLanguageModelV3 - Utilities (
@ai-sdk/provider-utils): Shared code for implementing providers - Providers (
@ai-sdk/<provider>): Concrete implementations for each AI service - Core (
ai): High-level functions likegenerateText,streamText,generateObject
For a focused conceptual walkthrough of AI functions, model specifications, and provider implementations, see architecture/provider-abstraction.md.
Provider Options Schemas (user-facing):
- Use
.optional()unlessnullis meaningful - Be as restrictive as possible for future flexibility
Response Schemas (API responses):
- Use
.nullish()instead of.optional() - Keep minimal - only include properties you need
- Allow flexibility for provider API changes
- Create folder under
packages/<name> - Add to root
tsconfig.jsonreferences - Run
pnpm update-referencesif adding dependencies between packages
| Task | Guide |
|---|---|
| Add new provider | contributing/add-new-provider.md |
| Add new model | contributing/add-new-model.md |
| Testing & fixtures | contributing/testing.md |
| Provider architecture | contributing/provider-architecture.md |
| Building new features | contributing/building-new-features.md |
| Codemods | contributing/codemods.md |
- Required: Every PR modifying production code needs a changeset
- Default: Use
patch(non-breaking changes) - Command:
pnpm changesetin workspace root - Note: Don't select example packages - they're not published
These guidelines outline typical artifacts for different task types. Use judgment to adapt based on scope and context.
A complete bug fix typically includes:
- Reproduction example: Create/update an example in
examples/that demonstrates the bug before fixing - Unit tests: Add tests that would fail without the fix (regression tests)
- Implementation: Fix the bug
- Manual verification: Run the reproduction example to confirm the fix
- Changeset: Describe what was broken and how it's fixed
A complete feature typically includes:
- Implementation: Build the feature
- Examples: Add usage examples in
examples/demonstrating the feature - Unit tests: Comprehensive test coverage for new functionality
- Documentation: Update relevant docs in
content/for public APIs - Changeset: Describe the feature for release notes
- Unit tests for any changed behavior
- No documentation needed for internal-only changes
- Changeset only if it affects published packages
These are guidelines, not rigid rules. Adjust based on:
- Scope: Trivial fixes (typos, comments) may not need examples
- Visibility: Internal changes may not need documentation
- Context: Some changes span multiple categories
When uncertain about expected artifacts, ask for clarification.
- Add minor/major changesets
- Change public APIs without updating documentation
- Use
require()for imports - Add new dependencies without running
pnpm update-references - Modify
content/docs/08-migration-guidesorpackages/codemodas part of broader codebase changes