AI SDK Provider provides a comprehensive TypeScript specification and interface definitions for AI language model providers within the Vercel AI SDK ecosystem. It serves as the foundational contract that enables consistent integration with various AI services by defining standardized interfaces for language models, embedding models, image models, speech models, and transcription models.
npm install @ai-sdk/providerimport {
LanguageModelV2,
EmbeddingModelV2,
ImageModelV2,
SpeechModelV2,
TranscriptionModelV2,
ProviderV2,
JSONValue,
JSONSchema7
} from "@ai-sdk/provider";For CommonJS:
const {
LanguageModelV2,
EmbeddingModelV2,
ImageModelV2,
SpeechModelV2,
TranscriptionModelV2,
ProviderV2,
JSONValue,
JSONSchema7
} = require("@ai-sdk/provider");import { ProviderV2, LanguageModelV2, LanguageModelV2CallOptions } from "@ai-sdk/provider";
// Implement a custom provider
class CustomProvider implements ProviderV2 {
languageModel(modelId: string): LanguageModelV2 {
return new CustomLanguageModel(modelId);
}
textEmbeddingModel(modelId: string): EmbeddingModelV2<string> {
return new CustomEmbeddingModel(modelId);
}
imageModel(modelId: string): ImageModelV2 {
return new CustomImageModel(modelId);
}
}
// Implement a language model
class CustomLanguageModel implements LanguageModelV2 {
specificationVersion = 'v2' as const;
provider = 'custom';
modelId: string;
supportedUrls = {};
constructor(modelId: string) {
this.modelId = modelId;
}
async doGenerate(options: LanguageModelV2CallOptions) {
// Implementation here
return {
content: [{ type: 'text', text: 'Hello world' }],
finishReason: 'stop' as const,
usage: {
inputTokens: 10,
outputTokens: 5,
totalTokens: 15
}
};
}
async doStream(options: LanguageModelV2CallOptions) {
// Implementation here
return {
stream: new ReadableStream()
};
}
}AI SDK Provider is built around several key components:
Core interface for text generation models supporting streaming, tool calling, and structured output generation.
type LanguageModelV2 = {
specificationVersion: 'v2';
provider: string;
modelId: string;
supportedUrls: PromiseLike<Record<string, RegExp[]>> | Record<string, RegExp[]>;
doGenerate(options: LanguageModelV2CallOptions): PromiseLike<LanguageModelV2GenerateResult>;
doStream(options: LanguageModelV2CallOptions): PromiseLike<LanguageModelV2StreamResult>;
};
interface LanguageModelV2GenerateResult {
content: LanguageModelV2Content[];
finishReason: LanguageModelV2FinishReason;
usage: LanguageModelV2Usage;
providerMetadata?: SharedV2ProviderMetadata;
request?: { body?: unknown };
response?: LanguageModelV2ResponseMetadata & { headers?: SharedV2Headers; body?: unknown };
warnings: LanguageModelV2CallWarning[];
}
interface LanguageModelV2StreamResult {
stream: ReadableStream<LanguageModelV2StreamPart>;
request?: { body?: unknown };
response?: { headers?: SharedV2Headers };
}Interface for text embedding models that convert text into numerical vectors for similarity and search operations.
type EmbeddingModelV2<VALUE> = {
specificationVersion: 'v2';
provider: string;
modelId: string;
maxEmbeddingsPerCall: PromiseLike<number | undefined> | number | undefined;
supportsParallelCalls: PromiseLike<boolean> | boolean;
doEmbed(options: {
values: Array<VALUE>;
abortSignal?: AbortSignal;
providerOptions?: SharedV2ProviderOptions;
headers?: Record<string, string | undefined>;
}): PromiseLike<EmbeddingModelV2Result>;
};
interface EmbeddingModelV2Result {
embeddings: EmbeddingModelV2Embedding[];
usage?: { tokens: number };
providerMetadata?: SharedV2ProviderMetadata;
response?: { headers?: SharedV2Headers; body?: unknown };
}
type EmbeddingModelV2Embedding = Array<number>;Interface for image generation models that create images from text prompts.
type ImageModelV2 = {
specificationVersion: 'v2';
provider: string;
modelId: string;
maxImagesPerCall: number | undefined | GetMaxImagesPerCallFunction;
doGenerate(options: ImageModelV2CallOptions): PromiseLike<ImageModelV2Result>;
};
interface ImageModelV2Result {
images: Array<string> | Array<Uint8Array>;
warnings: ImageModelV2CallWarning[];
providerMetadata?: ImageModelV2ProviderMetadata;
response: { timestamp: Date; modelId: string; headers: Record<string, string> | undefined };
}Interface for text-to-speech models that convert text into spoken audio.
type SpeechModelV2 = {
specificationVersion: 'v2';
provider: string;
modelId: string;
doGenerate(options: SpeechModelV2CallOptions): PromiseLike<SpeechModelV2Result>;
};
interface SpeechModelV2Result {
audio: string | Uint8Array;
warnings: SpeechModelV2CallWarning[];
request?: { body?: unknown };
response: { timestamp: Date; modelId: string; headers?: SharedV2Headers; body?: unknown };
providerMetadata?: Record<string, Record<string, JSONValue>>;
}Interface for speech-to-text models that convert audio into text transcriptions.
type TranscriptionModelV2 = {
specificationVersion: 'v2';
provider: string;
modelId: string;
doGenerate(options: TranscriptionModelV2CallOptions): PromiseLike<TranscriptionModelV2Result>;
};
interface TranscriptionModelV2Result {
text: string;
segments: Array<{
text: string;
startSecond: number;
endSecond: number;
}>;
language: string | undefined;
durationInSeconds: number | undefined;
warnings: TranscriptionModelV2CallWarning[];
request?: { body?: string };
response: { timestamp: Date; modelId: string; headers?: SharedV2Headers; body?: unknown };
providerMetadata?: Record<string, Record<string, JSONValue>>;
}Main provider interface for accessing all model types in a unified way.
interface ProviderV2 {
languageModel(modelId: string): LanguageModelV2;
textEmbeddingModel(modelId: string): EmbeddingModelV2<string>;
imageModel(modelId: string): ImageModelV2;
transcriptionModel?(modelId: string): TranscriptionModelV2;
speechModel?(modelId: string): SpeechModelV2;
}Comprehensive error system with specific error types for different failure scenarios, all extending the base AISDKError class.
class AISDKError extends Error {
readonly cause?: unknown;
constructor({ name, message, cause }: { name: string; message: string; cause?: unknown });
static isInstance(error: unknown): error is AISDKError;
}
class APICallError extends AISDKError {
readonly url: string;
readonly requestBodyValues: unknown;
readonly statusCode?: number;
readonly responseHeaders?: Record<string, string>;
readonly responseBody?: string;
readonly isRetryable: boolean;
readonly data?: unknown;
}
class NoSuchModelError extends AISDKError {
readonly modelId: string;
readonly modelType: 'languageModel' | 'textEmbeddingModel' | 'imageModel' | 'transcriptionModel' | 'speechModel';
}Type-safe JSON handling utilities for working with structured data.
type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
type JSONObject = { [key: string]: JSONValue };
type JSONArray = JSONValue[];
function isJSONValue(value: unknown): value is JSONValue;
function isJSONArray(value: unknown): value is JSONArray;
function isJSONObject(value: unknown): value is JSONObject;Pluggable middleware system for language models to add custom behavior like logging, caching, or request modification.
interface LanguageModelV2Middleware {
middlewareVersion?: 'v2' | undefined;
overrideProvider?: (options: { model: LanguageModelV2 }) => string;
overrideModelId?: (options: { model: LanguageModelV2 }) => string;
transformParams?: (options: {
type: 'generate' | 'stream';
params: LanguageModelV2CallOptions;
model: LanguageModelV2;
}) => PromiseLike<LanguageModelV2CallOptions>;
wrapGenerate?: (options: {
doGenerate: () => ReturnType<LanguageModelV2['doGenerate']>;
doStream: () => ReturnType<LanguageModelV2['doStream']>;
params: LanguageModelV2CallOptions;
model: LanguageModelV2;
}) => Promise<Awaited<ReturnType<LanguageModelV2['doGenerate']>>>;
wrapStream?: (options: {
doGenerate: () => ReturnType<LanguageModelV2['doGenerate']>;
doStream: () => ReturnType<LanguageModelV2['doStream']>;
params: LanguageModelV2CallOptions;
model: LanguageModelV2;
}) => PromiseLike<Awaited<ReturnType<LanguageModelV2['doStream']>>>;
}type SharedV2Headers = Record<string, string>;
type SharedV2ProviderMetadata = Record<string, Record<string, JSONValue>>;
type SharedV2ProviderOptions = Record<string, Record<string, JSONValue>>;Re-exported from the json-schema library for convenience:
// From json-schema package
type JSONSchema7 = { ... }; // Complete JSON Schema Draft 7 specification
type JSONSchema7Definition = JSONSchema7 | boolean;