@ai-sdk/provider
Version:
1,619 lines (1,571 loc) • 130 kB
TypeScript
import { JSONSchema7 } from 'json-schema';
export { JSONSchema7, JSONSchema7Definition } from 'json-schema';
type SharedV3Headers = Record<string, string>;
/**
* A JSON value can be a string, number, boolean, object, array, or null.
* JSON values can be serialized and deserialized by the JSON.stringify and JSON.parse methods.
*/
type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
type JSONObject = {
[key: string]: JSONValue | undefined;
};
type JSONArray = JSONValue[];
/**
* Additional provider-specific metadata.
* Metadata are additional outputs from the provider.
* They are passed through to the provider from the AI SDK
* and enable provider-specific functionality
* that can be fully encapsulated in the provider.
*
* This enables us to quickly ship provider-specific functionality
* without affecting the core AI SDK.
*
* The outer record is keyed by the provider name, and the inner
* record is keyed by the provider-specific metadata key.
*
* ```ts
* {
* "anthropic": {
* "cacheControl": { "type": "ephemeral" }
* }
* }
* ```
*/
type SharedV3ProviderMetadata = Record<string, JSONObject>;
/**
* Additional provider-specific options.
* Options are additional input to the provider.
* They are passed through to the provider from the AI SDK
* and enable provider-specific functionality
* that can be fully encapsulated in the provider.
*
* This enables us to quickly ship provider-specific functionality
* without affecting the core AI SDK.
*
* The outer record is keyed by the provider name, and the inner
* record is keyed by the provider-specific metadata key.
*
* ```ts
* {
* "anthropic": {
* "cacheControl": { "type": "ephemeral" }
* }
* }
* ```
*/
type SharedV3ProviderOptions = Record<string, JSONObject>;
/**
* Warning from the model.
*
* For example, that certain features are unsupported or compatibility
* functionality is used (which might lead to suboptimal results).
*/
type SharedV3Warning = {
/**
* A feature is not supported by the model.
*/
type: 'unsupported';
/**
* The feature that is not supported.
*/
feature: string;
/**
* Additional details about the warning.
*/
details?: string;
} | {
/**
* A compatibility feature is used that might lead to suboptimal results.
*/
type: 'compatibility';
/**
* The feature that is used in a compatibility mode.
*/
feature: string;
/**
* Additional details about the warning.
*/
details?: string;
} | {
/**
* Other warning.
*/
type: 'other';
/**
* The message of the warning.
*/
message: string;
};
type SharedV2Headers = Record<string, string>;
/**
* Additional provider-specific metadata.
* Metadata are additional outputs from the provider.
* They are passed through to the provider from the AI SDK
* and enable provider-specific functionality
* that can be fully encapsulated in the provider.
*
* This enables us to quickly ship provider-specific functionality
* without affecting the core AI SDK.
*
* The outer record is keyed by the provider name, and the inner
* record is keyed by the provider-specific metadata key.
*
* ```ts
* {
* "anthropic": {
* "cacheControl": { "type": "ephemeral" }
* }
* }
* ```
*/
type SharedV2ProviderMetadata = Record<string, Record<string, JSONValue>>;
/**
* Additional provider-specific options.
* Options are additional input to the provider.
* They are passed through to the provider from the AI SDK
* and enable provider-specific functionality
* that can be fully encapsulated in the provider.
*
* This enables us to quickly ship provider-specific functionality
* without affecting the core AI SDK.
*
* The outer record is keyed by the provider name, and the inner
* record is keyed by the provider-specific metadata key.
*
* ```ts
* {
* "anthropic": {
* "cacheControl": { "type": "ephemeral" }
* }
* }
* ```
*/
type SharedV2ProviderOptions = Record<string, Record<string, JSONValue>>;
type EmbeddingModelV3CallOptions = {
/**
* List of text values to generate embeddings for.
*/
values: Array<string>;
/**
* Abort signal for cancelling the operation.
*/
abortSignal?: AbortSignal;
/**
* Additional provider-specific options. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerOptions?: SharedV3ProviderOptions;
/**
* Additional HTTP headers to be sent with the request.
* Only applicable for HTTP-based providers.
*/
headers?: SharedV3Headers;
};
/**
* An embedding is a vector, i.e. an array of numbers.
* It is e.g. used to represent a text as a vector of word embeddings.
*/
type EmbeddingModelV3Embedding = Array<number>;
/**
* The result of a embedding model doEmbed call.
*/
type EmbeddingModelV3Result = {
/**
* Generated embeddings. They are in the same order as the input values.
*/
embeddings: Array<EmbeddingModelV3Embedding>;
/**
* Token usage. We only have input tokens for embeddings.
*/
usage?: {
tokens: number;
};
/**
* Additional provider-specific metadata. They are passed through
* from the provider to the AI SDK and enable provider-specific
* results that can be fully encapsulated in the provider.
*/
providerMetadata?: SharedV3ProviderMetadata;
/**
* Optional response information for debugging purposes.
*/
response?: {
/**
* Response headers.
*/
headers?: SharedV3Headers;
/**
* The response body.
*/
body?: unknown;
};
/**
* Warnings for the call, e.g. unsupported settings.
*/
warnings: Array<SharedV3Warning>;
};
/**
* Specification for an embedding model that implements the embedding model
* interface version 3.
*
* It is specific to text embeddings.
*/
type EmbeddingModelV3 = {
/**
* The embedding model must specify which embedding model interface
* version it implements. This will allow us to evolve the embedding
* model interface and retain backwards compatibility. The different
* implementation versions can be handled as a discriminated union
* on our side.
*/
readonly specificationVersion: 'v3';
/**
* Name of the provider for logging purposes.
*/
readonly provider: string;
/**
* Provider-specific model ID for logging purposes.
*/
readonly modelId: string;
/**
* Limit of how many embeddings can be generated in a single API call.
*
* Use Infinity for models that do not have a limit.
*/
readonly maxEmbeddingsPerCall: PromiseLike<number | undefined> | number | undefined;
/**
* True if the model can handle multiple embedding calls in parallel.
*/
readonly supportsParallelCalls: PromiseLike<boolean> | boolean;
/**
* Generates a list of embeddings for the given input text.
*
* Naming: "do" prefix to prevent accidental direct usage of the method
* by the user.
*/
doEmbed(options: EmbeddingModelV3CallOptions): PromiseLike<EmbeddingModelV3Result>;
};
/**
* An embedding is a vector, i.e. an array of numbers.
* It is e.g. used to represent a text as a vector of word embeddings.
*/
type EmbeddingModelV2Embedding = Array<number>;
/**
* Specification for an embedding model that implements the embedding model
* interface version 2.
*
* VALUE is the type of the values that the model can embed.
* This will allow us to go beyond text embeddings in the future,
* e.g. to support image embeddings
*/
type EmbeddingModelV2<VALUE> = {
/**
* The embedding model must specify which embedding model interface
* version it implements. This will allow us to evolve the embedding
* model interface and retain backwards compatibility. The different
* implementation versions can be handled as a discriminated union
* on our side.
*/
readonly specificationVersion: 'v2';
/**
* Name of the provider for logging purposes.
*/
readonly provider: string;
/**
* Provider-specific model ID for logging purposes.
*/
readonly modelId: string;
/**
* Limit of how many embeddings can be generated in a single API call.
*
* Use Infinity for models that do not have a limit.
*/
readonly maxEmbeddingsPerCall: PromiseLike<number | undefined> | number | undefined;
/**
* True if the model can handle multiple embedding calls in parallel.
*/
readonly supportsParallelCalls: PromiseLike<boolean> | boolean;
/**
* Generates a list of embeddings for the given input text.
*
* Naming: "do" prefix to prevent accidental direct usage of the method
* by the user.
*/
doEmbed(options: {
/**
* List of values to embed.
*/
values: Array<VALUE>;
/**
* Abort signal for cancelling the operation.
*/
abortSignal?: AbortSignal;
/**
* Additional provider-specific options. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerOptions?: SharedV2ProviderOptions;
/**
* Additional HTTP headers to be sent with the request.
* Only applicable for HTTP-based providers.
*/
headers?: Record<string, string | undefined>;
}): PromiseLike<{
/**
* Generated embeddings. They are in the same order as the input values.
*/
embeddings: Array<EmbeddingModelV2Embedding>;
/**
* Token usage. We only have input tokens for embeddings.
*/
usage?: {
tokens: number;
};
/**
* Additional provider-specific metadata. They are passed through
* from the provider to the AI SDK and enable provider-specific
* results that can be fully encapsulated in the provider.
*/
providerMetadata?: SharedV2ProviderMetadata;
/**
* Optional response information for debugging purposes.
*/
response?: {
/**
* Response headers.
*/
headers?: SharedV2Headers;
/**
* The response body.
*/
body?: unknown;
};
}>;
};
declare const symbol$d: unique symbol;
/**
* Custom error class for AI SDK related errors.
* @extends Error
*/
declare class AISDKError extends Error {
private readonly [symbol$d];
/**
* The underlying cause of the error, if any.
*/
readonly cause?: unknown;
/**
* Creates an AI SDK Error.
*
* @param {Object} params - The parameters for creating the error.
* @param {string} params.name - The name of the error.
* @param {string} params.message - The error message.
* @param {unknown} [params.cause] - The underlying cause of the error.
*/
constructor({ name, message, cause, }: {
name: string;
message: string;
cause?: unknown;
});
/**
* Checks if the given error is an AI SDK Error.
* @param {unknown} error - The error to check.
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
*/
static isInstance(error: unknown): error is AISDKError;
protected static hasMarker(error: unknown, marker: string): boolean;
}
declare const symbol$c: unique symbol;
declare class APICallError extends AISDKError {
private readonly [symbol$c];
readonly url: string;
readonly requestBodyValues: unknown;
readonly statusCode?: number;
readonly responseHeaders?: Record<string, string>;
readonly responseBody?: string;
readonly isRetryable: boolean;
readonly data?: unknown;
constructor({ message, url, requestBodyValues, statusCode, responseHeaders, responseBody, cause, isRetryable, // server error
data, }: {
message: string;
url: string;
requestBodyValues: unknown;
statusCode?: number;
responseHeaders?: Record<string, string>;
responseBody?: string;
cause?: unknown;
isRetryable?: boolean;
data?: unknown;
});
static isInstance(error: unknown): error is APICallError;
}
declare const symbol$b: unique symbol;
declare class EmptyResponseBodyError extends AISDKError {
private readonly [symbol$b];
constructor({ message }?: {
message?: string;
});
static isInstance(error: unknown): error is EmptyResponseBodyError;
}
declare function getErrorMessage(error: unknown | undefined): string;
declare const symbol$a: unique symbol;
/**
* A function argument is invalid.
*/
declare class InvalidArgumentError extends AISDKError {
private readonly [symbol$a];
readonly argument: string;
constructor({ message, cause, argument, }: {
argument: string;
message: string;
cause?: unknown;
});
static isInstance(error: unknown): error is InvalidArgumentError;
}
declare const symbol$9: unique symbol;
/**
* A prompt is invalid. This error should be thrown by providers when they cannot
* process a prompt.
*/
declare class InvalidPromptError extends AISDKError {
private readonly [symbol$9];
readonly prompt: unknown;
constructor({ prompt, message, cause, }: {
prompt: unknown;
message: string;
cause?: unknown;
});
static isInstance(error: unknown): error is InvalidPromptError;
}
declare const symbol$8: unique symbol;
/**
* Server returned a response with invalid data content.
* This should be thrown by providers when they cannot parse the response from the API.
*/
declare class InvalidResponseDataError extends AISDKError {
private readonly [symbol$8];
readonly data: unknown;
constructor({ data, message, }: {
data: unknown;
message?: string;
});
static isInstance(error: unknown): error is InvalidResponseDataError;
}
declare const symbol$7: unique symbol;
declare class JSONParseError extends AISDKError {
private readonly [symbol$7];
readonly text: string;
constructor({ text, cause }: {
text: string;
cause: unknown;
});
static isInstance(error: unknown): error is JSONParseError;
}
declare const symbol$6: unique symbol;
declare class LoadAPIKeyError extends AISDKError {
private readonly [symbol$6];
constructor({ message }: {
message: string;
});
static isInstance(error: unknown): error is LoadAPIKeyError;
}
declare const symbol$5: unique symbol;
declare class LoadSettingError extends AISDKError {
private readonly [symbol$5];
constructor({ message }: {
message: string;
});
static isInstance(error: unknown): error is LoadSettingError;
}
declare const symbol$4: unique symbol;
/**
* Thrown when the AI provider fails to generate any content.
*/
declare class NoContentGeneratedError extends AISDKError {
private readonly [symbol$4];
constructor({ message, }?: {
message?: string;
});
static isInstance(error: unknown): error is NoContentGeneratedError;
}
declare const symbol$3: unique symbol;
declare class NoSuchModelError extends AISDKError {
private readonly [symbol$3];
readonly modelId: string;
readonly modelType: 'languageModel' | 'embeddingModel' | 'imageModel' | 'transcriptionModel' | 'speechModel' | 'rerankingModel' | 'videoModel';
constructor({ errorName, modelId, modelType, message, }: {
errorName?: string;
modelId: string;
modelType: 'languageModel' | 'embeddingModel' | 'imageModel' | 'transcriptionModel' | 'speechModel' | 'rerankingModel' | 'videoModel';
message?: string;
});
static isInstance(error: unknown): error is NoSuchModelError;
}
declare const symbol$2: unique symbol;
declare class TooManyEmbeddingValuesForCallError extends AISDKError {
private readonly [symbol$2];
readonly provider: string;
readonly modelId: string;
readonly maxEmbeddingsPerCall: number;
readonly values: Array<unknown>;
constructor(options: {
provider: string;
modelId: string;
maxEmbeddingsPerCall: number;
values: Array<unknown>;
});
static isInstance(error: unknown): error is TooManyEmbeddingValuesForCallError;
}
declare const symbol$1: unique symbol;
interface TypeValidationContext {
/**
* Field path in dot notation (e.g., "message.metadata", "message.parts[3].data")
*/
field?: string;
/**
* Entity name (e.g., tool name, data type name)
*/
entityName?: string;
/**
* Entity identifier (e.g., message ID, tool call ID)
*/
entityId?: string;
}
declare class TypeValidationError extends AISDKError {
private readonly [symbol$1];
readonly value: unknown;
readonly context?: TypeValidationContext;
constructor({ value, cause, context, }: {
value: unknown;
cause: unknown;
context?: TypeValidationContext;
});
static isInstance(error: unknown): error is TypeValidationError;
/**
* Wraps an error into a TypeValidationError.
* If the cause is already a TypeValidationError with the same value and context, it returns the cause.
* Otherwise, it creates a new TypeValidationError.
*
* @param {Object} params - The parameters for wrapping the error.
* @param {unknown} params.value - The value that failed validation.
* @param {unknown} params.cause - The original error or cause of the validation failure.
* @param {TypeValidationContext} params.context - Optional context about what is being validated.
* @returns {TypeValidationError} A TypeValidationError instance.
*/
static wrap({ value, cause, context, }: {
value: unknown;
cause: unknown;
context?: TypeValidationContext;
}): TypeValidationError;
}
declare const symbol: unique symbol;
declare class UnsupportedFunctionalityError extends AISDKError {
private readonly [symbol];
readonly functionality: string;
constructor({ functionality, message, }: {
functionality: string;
message?: string;
});
static isInstance(error: unknown): error is UnsupportedFunctionalityError;
}
declare function isJSONValue(value: unknown): value is JSONValue;
declare function isJSONArray(value: unknown): value is JSONArray;
declare function isJSONObject(value: unknown): value is JSONObject;
/**
* Usage information for an image model call.
*/
type ImageModelV3Usage = {
/**
* The number of input (prompt) tokens used.
*/
inputTokens: number | undefined;
/**
* The number of output tokens used, if reported by the provider.
*/
outputTokens: number | undefined;
/**
* The total number of tokens as reported by the provider.
*/
totalTokens: number | undefined;
};
/**
* An image file that can be used for image editing or variation generation.
*/
type ImageModelV3File = {
type: 'file';
/**
* The IANA media type of the file, e.g. `image/png`. Any string is supported.
*
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
*/
mediaType: string;
/**
* Generated file data as base64 encoded strings or binary data.
*
* The file data should be returned without any unnecessary conversion.
* If the API returns base64 encoded strings, the file data should be returned
* as base64 encoded strings. If the API returns binary data, the file data should
* be returned as binary data.
*/
data: string | Uint8Array;
/**
* Optional provider-specific metadata for the file part.
*/
providerOptions?: SharedV3ProviderMetadata;
} | {
type: 'url';
/**
* The URL of the image file.
*/
url: string;
/**
* Optional provider-specific metadata for the file part.
*/
providerOptions?: SharedV3ProviderMetadata;
};
type ImageModelV3CallOptions = {
/**
* Prompt for the image generation. Some operations, like upscaling, may not require a prompt.
*/
prompt: string | undefined;
/**
* Number of images to generate.
*/
n: number;
/**
* Size of the images to generate.
* Must have the format `{width}x{height}`.
* `undefined` will use the provider's default size.
*/
size: `${number}x${number}` | undefined;
/**
* Aspect ratio of the images to generate.
* Must have the format `{width}:{height}`.
* `undefined` will use the provider's default aspect ratio.
*/
aspectRatio: `${number}:${number}` | undefined;
/**
* Seed for the image generation.
* `undefined` will use the provider's default seed.
*/
seed: number | undefined;
/**
* Array of images for image editing or variation generation.
* The images should be provided as base64 encoded strings or binary data.
*/
files: ImageModelV3File[] | undefined;
/**
* Mask image for inpainting operations.
* The mask should be provided as base64 encoded strings or binary data.
*/
mask: ImageModelV3File | undefined;
/**
* Additional provider-specific options that are passed through to the provider
* as body parameters.
*
* The outer record is keyed by the provider name, and the inner
* record is keyed by the provider-specific metadata key.
*
* ```ts
* {
* "openai": {
* "style": "vivid"
* }
* }
* ```
*/
providerOptions: SharedV3ProviderOptions;
/**
* Abort signal for cancelling the operation.
*/
abortSignal?: AbortSignal;
/**
* Additional HTTP headers to be sent with the request.
* Only applicable for HTTP-based providers.
*/
headers?: Record<string, string | undefined>;
};
type ImageModelV3ProviderMetadata = Record<string, {
images: JSONArray;
} & JSONValue>;
type GetMaxImagesPerCallFunction$1 = (options: {
modelId: string;
}) => PromiseLike<number | undefined> | number | undefined;
/**
* Image generation model specification version 3.
*/
type ImageModelV3 = {
/**
* The image model must specify which image model interface
* version it implements. This will allow us to evolve the image
* model interface and retain backwards compatibility. The different
* implementation versions can be handled as a discriminated union
* on our side.
*/
readonly specificationVersion: 'v3';
/**
* Name of the provider for logging purposes.
*/
readonly provider: string;
/**
* Provider-specific model ID for logging purposes.
*/
readonly modelId: string;
/**
* Limit of how many images can be generated in a single API call.
* Can be set to a number for a fixed limit, to undefined to use
* the global limit, or a function that returns a number or undefined,
* optionally as a promise.
*/
readonly maxImagesPerCall: number | undefined | GetMaxImagesPerCallFunction$1;
/**
* Generates an array of images.
*/
doGenerate(options: ImageModelV3CallOptions): PromiseLike<{
/**
* Generated images as base64 encoded strings or binary data.
* The images should be returned without any unnecessary conversion.
* If the API returns base64 encoded strings, the images should be returned
* as base64 encoded strings. If the API returns binary data, the images should
* be returned as binary data.
*/
images: Array<string> | Array<Uint8Array>;
/**
* Warnings for the call, e.g. unsupported features.
*/
warnings: Array<SharedV3Warning>;
/**
* Additional provider-specific metadata. They are passed through
* from the provider to the AI SDK and enable provider-specific
* results that can be fully encapsulated in the provider.
*
* The outer record is keyed by the provider name, and the inner
* record is provider-specific metadata. It always includes an
* `images` key with image-specific metadata
*
* ```ts
* {
* "openai": {
* "images": ["revisedPrompt": "Revised prompt here."]
* }
* }
* ```
*/
providerMetadata?: ImageModelV3ProviderMetadata;
/**
* Response information for telemetry and debugging purposes.
*/
response: {
/**
* Timestamp for the start of the generated response.
*/
timestamp: Date;
/**
* The ID of the response model that was used to generate the response.
*/
modelId: string;
/**
* Response headers.
*/
headers: Record<string, string> | undefined;
};
/**
* Optional token usage for the image generation call (if the provider reports it).
*/
usage?: ImageModelV3Usage;
}>;
};
type ImageModelV2CallOptions = {
/**
* Prompt for the image generation.
*/
prompt: string;
/**
* Number of images to generate.
*/
n: number;
/**
* Size of the images to generate.
* Must have the format `{width}x{height}`.
* `undefined` will use the provider's default size.
*/
size: `${number}x${number}` | undefined;
/**
* Aspect ratio of the images to generate.
* Must have the format `{width}:{height}`.
* `undefined` will use the provider's default aspect ratio.
*/
aspectRatio: `${number}:${number}` | undefined;
/**
* Seed for the image generation.
* `undefined` will use the provider's default seed.
*/
seed: number | undefined;
/**
* Additional provider-specific options that are passed through to the provider
* as body parameters.
*
* The outer record is keyed by the provider name, and the inner
* record is keyed by the provider-specific metadata key.
* ```ts
* {
* "openai": {
* "style": "vivid"
* }
* }
* ```
*/
providerOptions: SharedV2ProviderOptions;
/**
* Abort signal for cancelling the operation.
*/
abortSignal?: AbortSignal;
/**
* Additional HTTP headers to be sent with the request.
* Only applicable for HTTP-based providers.
*/
headers?: Record<string, string | undefined>;
};
/**
* Warning from the model provider for this call. The call will proceed, but e.g.
* some settings might not be supported, which can lead to suboptimal results.
*/
type ImageModelV2CallWarning = {
type: 'unsupported-setting';
setting: keyof ImageModelV2CallOptions;
details?: string;
} | {
type: 'other';
message: string;
};
type ImageModelV2ProviderMetadata = Record<string, {
images: JSONArray;
} & JSONValue>;
type GetMaxImagesPerCallFunction = (options: {
modelId: string;
}) => PromiseLike<number | undefined> | number | undefined;
/**
* Image generation model specification version 2.
*/
type ImageModelV2 = {
/**
* The image model must specify which image model interface
* version it implements. This will allow us to evolve the image
* model interface and retain backwards compatibility. The different
* implementation versions can be handled as a discriminated union
* on our side.
*/
readonly specificationVersion: 'v2';
/**
* Name of the provider for logging purposes.
*/
readonly provider: string;
/**
* Provider-specific model ID for logging purposes.
*/
readonly modelId: string;
/**
* Limit of how many images can be generated in a single API call.
* Can be set to a number for a fixed limit, to undefined to use
* the global limit, or a function that returns a number or undefined,
* optionally as a promise.
*/
readonly maxImagesPerCall: number | undefined | GetMaxImagesPerCallFunction;
/**
* Generates an array of images.
*/
doGenerate(options: ImageModelV2CallOptions): PromiseLike<{
/**
* Generated images as base64 encoded strings or binary data.
* The images should be returned without any unnecessary conversion.
* If the API returns base64 encoded strings, the images should be returned
* as base64 encoded strings. If the API returns binary data, the images should
* be returned as binary data.
*/
images: Array<string> | Array<Uint8Array>;
/**
* Warnings for the call, e.g. unsupported settings.
*/
warnings: Array<ImageModelV2CallWarning>;
/**
* Additional provider-specific metadata. They are passed through
* from the provider to the AI SDK and enable provider-specific
* results that can be fully encapsulated in the provider.
*
* The outer record is keyed by the provider name, and the inner
* record is provider-specific metadata. It always includes an
* `images` key with image-specific metadata
*
* ```ts
* {
* "openai": {
* "images": ["revisedPrompt": "Revised prompt here."]
* }
* }
* ```
*/
providerMetadata?: ImageModelV2ProviderMetadata;
/**
* Response information for telemetry and debugging purposes.
*/
response: {
/**
* Timestamp for the start of the generated response.
*/
timestamp: Date;
/**
* The ID of the response model that was used to generate the response.
*/
modelId: string;
/**
* Response headers.
*/
headers: Record<string, string> | undefined;
};
}>;
};
/**
* Middleware for ImageModelV3.
* This type defines the structure for middleware that can be used to modify
* the behavior of ImageModelV3 operations.
*/
type ImageModelV3Middleware = {
/**
* Middleware specification version. Use `v3` for the current version.
*/
readonly specificationVersion: 'v3';
/**
* Override the provider name if desired.
* @param options.model - The image model instance.
*/
overrideProvider?: (options: {
model: ImageModelV3;
}) => string;
/**
* Override the model ID if desired.
* @param options.model - The image model instance.
*/
overrideModelId?: (options: {
model: ImageModelV3;
}) => string;
/**
* Override the limit of how many images can be generated in a single API call if desired.
* @param options.model - The image model instance.
*/
overrideMaxImagesPerCall?: (options: {
model: ImageModelV3;
}) => ImageModelV3['maxImagesPerCall'];
/**
* Transforms the parameters before they are passed to the image model.
* @param options - Object containing the parameters.
* @param options.params - The original parameters for the image model call.
* @returns A promise that resolves to the transformed parameters.
*/
transformParams?: (options: {
params: ImageModelV3CallOptions;
model: ImageModelV3;
}) => PromiseLike<ImageModelV3CallOptions>;
/**
* Wraps the generate operation of the image model.
*
* @param options - Object containing the generate function, parameters, and model.
* @param options.doGenerate - The original generate function.
* @param options.params - The parameters for the generate call. If the
* `transformParams` middleware is used, this will be the transformed parameters.
* @param options.model - The image model instance.
* @returns A promise that resolves to the result of the generate operation.
*/
wrapGenerate?: (options: {
doGenerate: () => ReturnType<ImageModelV3['doGenerate']>;
params: ImageModelV3CallOptions;
model: ImageModelV3;
}) => Promise<Awaited<ReturnType<ImageModelV3['doGenerate']>>>;
};
/**
* A tool has a name, a description, and a set of parameters.
*
* Note: this is **not** the user-facing tool definition. The AI SDK methods will
* map the user-facing tool definitions to this format.
*/
type LanguageModelV3FunctionTool = {
/**
* The type of the tool (always 'function').
*/
type: 'function';
/**
* The name of the tool. Unique within this model call.
*/
name: string;
/**
* A description of the tool. The language model uses this to understand the
* tool's purpose and to provide better completion suggestions.
*/
description?: string;
/**
* The parameters that the tool expects. The language model uses this to
* understand the tool's input requirements and to provide matching suggestions.
*/
inputSchema: JSONSchema7;
/**
* An optional list of input examples that show the language
* model what the input should look like.
*/
inputExamples?: Array<{
input: JSONObject;
}>;
/**
* Strict mode setting for the tool.
*
* Providers that support strict mode will use this setting to determine
* how the input should be generated. Strict mode will always produce
* valid inputs, but it might limit what input schemas are supported.
*/
strict?: boolean;
/**
* The provider-specific options for the tool.
*/
providerOptions?: SharedV3ProviderOptions;
};
/**
* Data content. Can be a Uint8Array, base64 encoded data as a string or a URL.
*/
type LanguageModelV3DataContent = Uint8Array | string | URL;
/**
* A prompt is a list of messages.
*
* Note: Not all models and prompt formats support multi-modal inputs and
* tool calls. The validation happens at runtime.
*
* Note: This is not a user-facing prompt. The AI SDK methods will map the
* user-facing prompt types such as chat or instruction prompts to this format.
*/
type LanguageModelV3Prompt = Array<LanguageModelV3Message>;
type LanguageModelV3Message = ({
role: 'system';
content: string;
} | {
role: 'user';
content: Array<LanguageModelV3TextPart | LanguageModelV3FilePart>;
} | {
role: 'assistant';
content: Array<LanguageModelV3TextPart | LanguageModelV3FilePart | LanguageModelV3ReasoningPart | LanguageModelV3ToolCallPart | LanguageModelV3ToolResultPart>;
} | {
role: 'tool';
content: Array<LanguageModelV3ToolResultPart | LanguageModelV3ToolApprovalResponsePart>;
}) & {
/**
* Additional provider-specific options. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerOptions?: SharedV3ProviderOptions;
};
/**
* Text content part of a prompt. It contains a string of text.
*/
interface LanguageModelV3TextPart {
type: 'text';
/**
* The text content.
*/
text: string;
/**
* Additional provider-specific options. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerOptions?: SharedV3ProviderOptions;
}
/**
* Reasoning content part of a prompt. It contains a string of reasoning text.
*/
interface LanguageModelV3ReasoningPart {
type: 'reasoning';
/**
* The reasoning text.
*/
text: string;
/**
* Additional provider-specific options. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerOptions?: SharedV3ProviderOptions;
}
/**
* File content part of a prompt. It contains a file.
*/
interface LanguageModelV3FilePart {
type: 'file';
/**
* Optional filename of the file.
*/
filename?: string;
/**
* File data. Can be a Uint8Array, base64 encoded data as a string or a URL.
*/
data: LanguageModelV3DataContent;
/**
* IANA media type of the file.
*
* Can support wildcards, e.g. `image/*` (in which case the provider needs to take appropriate action).
*
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
*/
mediaType: string;
/**
* Additional provider-specific options. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerOptions?: SharedV3ProviderOptions;
}
/**
* Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
*/
interface LanguageModelV3ToolCallPart {
type: 'tool-call';
/**
* ID of the tool call. This ID is used to match the tool call with the tool result.
*/
toolCallId: string;
/**
* Name of the tool that is being called.
*/
toolName: string;
/**
* Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
*/
input: unknown;
/**
* Whether the tool call will be executed by the provider.
* If this flag is not set or is false, the tool call will be executed by the client.
*/
providerExecuted?: boolean;
/**
* Additional provider-specific options. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerOptions?: SharedV3ProviderOptions;
}
/**
* Tool result content part of a prompt. It contains the result of the tool call with the matching ID.
*/
interface LanguageModelV3ToolResultPart {
type: 'tool-result';
/**
* ID of the tool call that this result is associated with.
*/
toolCallId: string;
/**
* Name of the tool that generated this result.
*/
toolName: string;
/**
* Result of the tool call.
*/
output: LanguageModelV3ToolResultOutput;
/**
* Additional provider-specific options. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerOptions?: SharedV3ProviderOptions;
}
/**
* Tool approval response content part of a prompt. It contains the user's
* decision to approve or deny a provider-executed tool call.
*/
interface LanguageModelV3ToolApprovalResponsePart {
type: 'tool-approval-response';
/**
* ID of the approval request that this response refers to.
*/
approvalId: string;
/**
* Whether the approval was granted (true) or denied (false).
*/
approved: boolean;
/**
* Optional reason for approval or denial.
*/
reason?: string;
/**
* Additional provider-specific options. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerOptions?: SharedV3ProviderOptions;
}
/**
* Result of a tool call.
*/
type LanguageModelV3ToolResultOutput = {
/**
* Text tool output that should be directly sent to the API.
*/
type: 'text';
value: string;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
type: 'json';
value: JSONValue;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
/**
* Type when the user has denied the execution of the tool call.
*/
type: 'execution-denied';
/**
* Optional reason for the execution denial.
*/
reason?: string;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
type: 'error-text';
value: string;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
type: 'error-json';
value: JSONValue;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
type: 'content';
value: Array<{
type: 'text';
/**
* Text content.
*/
text: string;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
type: 'file-data';
/**
* Base-64 encoded media data.
*/
data: string;
/**
* IANA media type.
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
*/
mediaType: string;
/**
* Optional filename of the file.
*/
filename?: string;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
type: 'file-url';
/**
* URL of the file.
*/
url: string;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
type: 'file-id';
/**
* ID of the file.
*
* If you use multiple providers, you need to
* specify the provider specific ids using
* the Record option. The key is the provider
* name, e.g. 'openai' or 'anthropic'.
*/
fileId: string | Record<string, string>;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
/**
* Images that are referenced using base64 encoded data.
*/
type: 'image-data';
/**
* Base-64 encoded image data.
*/
data: string;
/**
* IANA media type.
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
*/
mediaType: string;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
/**
* Images that are referenced using a URL.
*/
type: 'image-url';
/**
* URL of the image.
*/
url: string;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
/**
* Images that are referenced using a provider file id.
*/
type: 'image-file-id';
/**
* Image that is referenced using a provider file id.
*
* If you use multiple providers, you need to
* specify the provider specific ids using
* the Record option. The key is the provider
* name, e.g. 'openai' or 'anthropic'.
*/
fileId: string | Record<string, string>;
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
} | {
/**
* Custom content part. This can be used to implement
* provider-specific content parts.
*/
type: 'custom';
/**
* Provider-specific options.
*/
providerOptions?: SharedV3ProviderOptions;
}>;
};
/**
* The configuration of a provider tool.
*
* Provider tools are tools that are specific to a certain provider.
* The input and output schemas are defined be the provider, and
* some of the tools are also executed on the provider systems.
*/
type LanguageModelV3ProviderTool = {
/**
* The type of the tool (always 'provider').
*/
type: 'provider';
/**
* The ID of the tool. Should follow the format `<provider-id>.<unique-tool-name>`.
*/
id: `${string}.${string}`;
/**
* The name of the tool. Unique within this model call.
*/
name: string;
/**
* The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
*/
args: Record<string, unknown>;
};
type LanguageModelV3ToolChoice = {
type: 'auto';
} | {
type: 'none';
} | {
type: 'required';
} | {
type: 'tool';
toolName: string;
};
type LanguageModelV3CallOptions = {
/**
* A language mode prompt is a standardized prompt type.
*
* Note: This is **not** the user-facing prompt. The AI SDK methods will map the
* user-facing prompt types such as chat or instruction prompts to this format.
* That approach allows us to evolve the user facing prompts without breaking
* the language model interface.
*/
prompt: LanguageModelV3Prompt;
/**
* Maximum number of tokens to generate.
*/
maxOutputTokens?: number;
/**
* Temperature setting. The range depends on the provider and model.
*/
temperature?: number;
/**
* Stop sequences.
* If set, the model will stop generating text when one of the stop sequences is generated.
* Providers may have limits on the number of stop sequences.
*/
stopSequences?: string[];
/**
* Nucleus sampling.
*/
topP?: number;
/**
* Only sample from the top K options for each subsequent token.
*
* Used to remove "long tail" low probability responses.
* Recommended for advanced use cases only. You usually only need to use temperature.
*/
topK?: number;
/**
* Presence penalty setting. It affects the likelihood of the model to
* repeat information that is already in the prompt.
*/
presencePenalty?: number;
/**
* Frequency penalty setting. It affects the likelihood of the model
* to repeatedly use the same words or phrases.
*/
frequencyPenalty?: number;
/**
* Response format. The output can either be text or JSON. Default is text.
*
* If JSON is selected, a schema can optionally be provided to guide the LLM.
*/
responseFormat?: {
type: 'text';
} | {
type: 'json';
/**
* JSON schema that the generated output should conform to.
*/
schema?: JSONSchema7;
/**
* Name of output that should be generated. Used by some providers for additional LLM guidance.
*/
name?: string;
/**
* Description of the output that should be generated. Used by some providers for additional LLM guidance.
*/
description?: string;
};
/**
* The seed (integer) to use for random sampling. If set and supported
* by the model, calls will generate deterministic results.
*/
seed?: number;
/**
* The tools that are available for the model.
*/
tools?: Array<LanguageModelV3FunctionTool | LanguageModelV3ProviderTool>;
/**
* Specifies how the tool should be selected. Defaults to 'auto'.
*/
toolChoice?: LanguageModelV3ToolChoice;
/**
* Include raw chunks in the stream. Only applicable for streaming calls.
*/
includeRawChunks?: boolean;
/**
* Abort signal for cancelling the operation.
*/
abortSignal?: AbortSignal;
/**
* Additional HTTP headers to be sent with the request.
* Only applicable for HTTP-based providers.
*/
headers?: Record<string, string | undefined>;
/**
* Additional provider-specific options. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerOptions?: SharedV3ProviderOptions;
};
/**
* A file that has been generated by the model.
* Generated files as base64 encoded strings or binary data.
* The files should be returned without any unnecessary conversion.
*/
type LanguageModelV3File = {
type: 'file';
/**
* The IANA media type of the file, e.g. `image/png` or `audio/mp3`.
*
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
*/
mediaType: string;
/**
* Generated file data as base64 encoded strings or binary data.
*
* The file data should be returned without any unnecessary conversion.
* If the API returns base64 encoded strings, the file data should be returned
* as base64 encoded strings. If the API returns binary data, the file data should
* be returned as binary data.
*/
data: string | Uint8Array;
/**
* Optional provider-specific metadata for the file part.
*/
providerMetadata?: SharedV3ProviderMetadata;
};
/**
* Reasoning that the model has generated.
*/
type LanguageModelV3Reasoning = {
type: 'reasoning';
text: string;
/**
* Optional provider-specific metadata for the reasoning part.
*/
providerMetadata?: SharedV3ProviderMetadata;
};
/**
* A source that has been used as input to generate the response.
*/
type LanguageModelV3Source = {
type: 'source';
/**
* The type of source - URL sources reference web content.
*/
sourceType: 'url';
/**
* The ID of the source.
*/
id: string;
/**
* The URL of the source.
*/
url: string;
/**
* The title of the source.
*/
title?: string;
/**
* Additional provider metadata for the source.
*/
providerMetadata?: SharedV3ProviderMetadata;
} | {
type: 'source';
/**
* The type of source - document sources reference files/documents.
*/
sourceType: 'document';
/**
* The ID of the source.