@ai-sdk/openai
Version:
The **[OpenAI provider](https://ai-sdk.dev/providers/ai-sdk-providers/openai)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for the OpenAI chat and completion APIs and embedding model support for the OpenAI embeddings API.
1,492 lines (1,472 loc) • 109 kB
JavaScript
// src/openai-chat-language-model.ts
import {
InvalidResponseDataError
} from "@ai-sdk/provider";
import {
combineHeaders,
createEventSourceResponseHandler,
createJsonResponseHandler,
generateId,
isParsableJson,
parseProviderOptions,
postJsonToApi
} from "@ai-sdk/provider-utils";
import { z as z5 } from "zod/v4";
// src/convert-to-openai-chat-messages.ts
import {
UnsupportedFunctionalityError
} from "@ai-sdk/provider";
import { convertToBase64 } from "@ai-sdk/provider-utils";
function convertToOpenAIChatMessages({
prompt,
systemMessageMode = "system"
}) {
const messages = [];
const warnings = [];
for (const { role, content } of prompt) {
switch (role) {
case "system": {
switch (systemMessageMode) {
case "system": {
messages.push({ role: "system", content });
break;
}
case "developer": {
messages.push({ role: "developer", content });
break;
}
case "remove": {
warnings.push({
type: "other",
message: "system messages are removed for this model"
});
break;
}
default: {
const _exhaustiveCheck = systemMessageMode;
throw new Error(
`Unsupported system message mode: ${_exhaustiveCheck}`
);
}
}
break;
}
case "user": {
if (content.length === 1 && content[0].type === "text") {
messages.push({ role: "user", content: content[0].text });
break;
}
messages.push({
role: "user",
content: content.map((part, index) => {
var _a, _b, _c;
switch (part.type) {
case "text": {
return { type: "text", text: part.text };
}
case "file": {
if (part.mediaType.startsWith("image/")) {
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
return {
type: "image_url",
image_url: {
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`,
// OpenAI specific extension: image detail
detail: (_b = (_a = part.providerOptions) == null ? void 0 : _a.openai) == null ? void 0 : _b.imageDetail
}
};
} else if (part.mediaType.startsWith("audio/")) {
if (part.data instanceof URL) {
throw new UnsupportedFunctionalityError({
functionality: "audio file parts with URLs"
});
}
switch (part.mediaType) {
case "audio/wav": {
return {
type: "input_audio",
input_audio: {
data: convertToBase64(part.data),
format: "wav"
}
};
}
case "audio/mp3":
case "audio/mpeg": {
return {
type: "input_audio",
input_audio: {
data: convertToBase64(part.data),
format: "mp3"
}
};
}
default: {
throw new UnsupportedFunctionalityError({
functionality: `audio content parts with media type ${part.mediaType}`
});
}
}
} else if (part.mediaType === "application/pdf") {
if (part.data instanceof URL) {
throw new UnsupportedFunctionalityError({
functionality: "PDF file parts with URLs"
});
}
return {
type: "file",
file: {
filename: (_c = part.filename) != null ? _c : `part-${index}.pdf`,
file_data: `data:application/pdf;base64,${convertToBase64(part.data)}`
}
};
} else {
throw new UnsupportedFunctionalityError({
functionality: `file part media type ${part.mediaType}`
});
}
}
}
})
});
break;
}
case "assistant": {
let text = "";
const toolCalls = [];
for (const part of content) {
switch (part.type) {
case "text": {
text += part.text;
break;
}
case "tool-call": {
toolCalls.push({
id: part.toolCallId,
type: "function",
function: {
name: part.toolName,
arguments: JSON.stringify(part.input)
}
});
break;
}
}
}
messages.push({
role: "assistant",
content: text,
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
});
break;
}
case "tool": {
for (const toolResponse of content) {
const output = toolResponse.output;
let contentValue;
switch (output.type) {
case "text":
case "error-text":
contentValue = output.value;
break;
case "content":
case "json":
case "error-json":
contentValue = JSON.stringify(output.value);
break;
}
messages.push({
role: "tool",
tool_call_id: toolResponse.toolCallId,
content: contentValue
});
}
break;
}
default: {
const _exhaustiveCheck = role;
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
}
}
}
return { messages, warnings };
}
// src/get-response-metadata.ts
function getResponseMetadata({
id,
model,
created
}) {
return {
id: id != null ? id : void 0,
modelId: model != null ? model : void 0,
timestamp: created != null ? new Date(created * 1e3) : void 0
};
}
// src/map-openai-finish-reason.ts
function mapOpenAIFinishReason(finishReason) {
switch (finishReason) {
case "stop":
return "stop";
case "length":
return "length";
case "content_filter":
return "content-filter";
case "function_call":
case "tool_calls":
return "tool-calls";
default:
return "unknown";
}
}
// src/openai-chat-options.ts
import { z } from "zod/v4";
var openaiProviderOptions = z.object({
/**
* Modify the likelihood of specified tokens appearing in the completion.
*
* Accepts a JSON object that maps tokens (specified by their token ID in
* the GPT tokenizer) to an associated bias value from -100 to 100.
*/
logitBias: z.record(z.coerce.number(), z.number()).optional(),
/**
* Return the log probabilities of the tokens.
*
* Setting to true will return the log probabilities of the tokens that
* were generated.
*
* Setting to a number will return the log probabilities of the top n
* tokens that were generated.
*/
logprobs: z.union([z.boolean(), z.number()]).optional(),
/**
* Whether to enable parallel function calling during tool use. Default to true.
*/
parallelToolCalls: z.boolean().optional(),
/**
* A unique identifier representing your end-user, which can help OpenAI to
* monitor and detect abuse.
*/
user: z.string().optional(),
/**
* Reasoning effort for reasoning models. Defaults to `medium`.
*/
reasoningEffort: z.enum(["low", "medium", "high"]).optional(),
/**
* Maximum number of completion tokens to generate. Useful for reasoning models.
*/
maxCompletionTokens: z.number().optional(),
/**
* Whether to enable persistence in responses API.
*/
store: z.boolean().optional(),
/**
* Metadata to associate with the request.
*/
metadata: z.record(z.string().max(64), z.string().max(512)).optional(),
/**
* Parameters for prediction mode.
*/
prediction: z.record(z.string(), z.any()).optional(),
/**
* Whether to use structured outputs.
*
* @default true
*/
structuredOutputs: z.boolean().optional(),
/**
* Service tier for the request.
* - 'auto': Default service tier
* - 'flex': 50% cheaper processing at the cost of increased latency. Only available for o3 and o4-mini models.
* - 'priority': Higher-speed processing with predictably low latency at premium cost. Available for Enterprise customers.
*
* @default 'auto'
*/
serviceTier: z.enum(["auto", "flex", "priority"]).optional(),
/**
* Whether to use strict JSON schema validation.
*
* @default false
*/
strictJsonSchema: z.boolean().optional()
});
// src/openai-error.ts
import { z as z2 } from "zod/v4";
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
var openaiErrorDataSchema = z2.object({
error: z2.object({
message: z2.string(),
// The additional information below is handled loosely to support
// OpenAI-compatible providers that have slightly different error
// responses:
type: z2.string().nullish(),
param: z2.any().nullish(),
code: z2.union([z2.string(), z2.number()]).nullish()
})
});
var openaiFailedResponseHandler = createJsonErrorResponseHandler({
errorSchema: openaiErrorDataSchema,
errorToMessage: (data) => data.error.message
});
// src/openai-prepare-tools.ts
import {
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
} from "@ai-sdk/provider";
// src/tool/file-search.ts
import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
import { z as z3 } from "zod/v4";
var comparisonFilterSchema = z3.object({
key: z3.string(),
type: z3.enum(["eq", "ne", "gt", "gte", "lt", "lte"]),
value: z3.union([z3.string(), z3.number(), z3.boolean()])
});
var compoundFilterSchema = z3.object({
type: z3.enum(["and", "or"]),
filters: z3.array(
z3.union([comparisonFilterSchema, z3.lazy(() => compoundFilterSchema)])
)
});
var filtersSchema = z3.union([comparisonFilterSchema, compoundFilterSchema]);
var fileSearchArgsSchema = z3.object({
/**
* List of vector store IDs to search through. If not provided, searches all available vector stores.
*/
vectorStoreIds: z3.array(z3.string()).optional(),
/**
* Maximum number of search results to return. Defaults to 10.
*/
maxNumResults: z3.number().optional(),
/**
* Ranking options for the search.
*/
ranking: z3.object({
ranker: z3.enum(["auto", "default-2024-08-21"]).optional()
}).optional(),
/**
* A filter to apply based on file attributes.
*/
filters: filtersSchema.optional()
});
var fileSearch = createProviderDefinedToolFactory({
id: "openai.file_search",
name: "file_search",
inputSchema: z3.object({
query: z3.string()
})
});
// src/tool/web-search-preview.ts
import { createProviderDefinedToolFactory as createProviderDefinedToolFactory2 } from "@ai-sdk/provider-utils";
import { z as z4 } from "zod/v4";
var webSearchPreviewArgsSchema = z4.object({
/**
* Search context size to use for the web search.
* - high: Most comprehensive context, highest cost, slower response
* - medium: Balanced context, cost, and latency (default)
* - low: Least context, lowest cost, fastest response
*/
searchContextSize: z4.enum(["low", "medium", "high"]).optional(),
/**
* User location information to provide geographically relevant search results.
*/
userLocation: z4.object({
/**
* Type of location (always 'approximate')
*/
type: z4.literal("approximate"),
/**
* Two-letter ISO country code (e.g., 'US', 'GB')
*/
country: z4.string().optional(),
/**
* City name (free text, e.g., 'Minneapolis')
*/
city: z4.string().optional(),
/**
* Region name (free text, e.g., 'Minnesota')
*/
region: z4.string().optional(),
/**
* IANA timezone (e.g., 'America/Chicago')
*/
timezone: z4.string().optional()
}).optional()
});
var webSearchPreview = createProviderDefinedToolFactory2({
id: "openai.web_search_preview",
name: "web_search_preview",
inputSchema: z4.object({})
});
// src/openai-prepare-tools.ts
function prepareTools({
tools,
toolChoice,
structuredOutputs,
strictJsonSchema
}) {
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
const toolWarnings = [];
if (tools == null) {
return { tools: void 0, toolChoice: void 0, toolWarnings };
}
const openaiTools = [];
for (const tool of tools) {
switch (tool.type) {
case "function":
openaiTools.push({
type: "function",
function: {
name: tool.name,
description: tool.description,
parameters: tool.inputSchema,
strict: structuredOutputs ? strictJsonSchema : void 0
}
});
break;
case "provider-defined":
switch (tool.id) {
case "openai.file_search": {
const args = fileSearchArgsSchema.parse(tool.args);
openaiTools.push({
type: "file_search",
vector_store_ids: args.vectorStoreIds,
max_num_results: args.maxNumResults,
ranking_options: args.ranking ? { ranker: args.ranking.ranker } : void 0,
filters: args.filters
});
break;
}
case "openai.web_search_preview": {
const args = webSearchPreviewArgsSchema.parse(tool.args);
openaiTools.push({
type: "web_search_preview",
search_context_size: args.searchContextSize,
user_location: args.userLocation
});
break;
}
default:
toolWarnings.push({ type: "unsupported-tool", tool });
break;
}
break;
default:
toolWarnings.push({ type: "unsupported-tool", tool });
break;
}
}
if (toolChoice == null) {
return { tools: openaiTools, toolChoice: void 0, toolWarnings };
}
const type = toolChoice.type;
switch (type) {
case "auto":
case "none":
case "required":
return { tools: openaiTools, toolChoice: type, toolWarnings };
case "tool":
return {
tools: openaiTools,
toolChoice: {
type: "function",
function: {
name: toolChoice.toolName
}
},
toolWarnings
};
default: {
const _exhaustiveCheck = type;
throw new UnsupportedFunctionalityError2({
functionality: `tool choice type: ${_exhaustiveCheck}`
});
}
}
}
// src/openai-chat-language-model.ts
var OpenAIChatLanguageModel = class {
constructor(modelId, config) {
this.specificationVersion = "v2";
this.supportedUrls = {
"image/*": [/^https?:\/\/.*$/]
};
this.modelId = modelId;
this.config = config;
}
get provider() {
return this.config.provider;
}
async getArgs({
prompt,
maxOutputTokens,
temperature,
topP,
topK,
frequencyPenalty,
presencePenalty,
stopSequences,
responseFormat,
seed,
tools,
toolChoice,
providerOptions
}) {
var _a, _b, _c, _d;
const warnings = [];
const openaiOptions = (_a = await parseProviderOptions({
provider: "openai",
providerOptions,
schema: openaiProviderOptions
})) != null ? _a : {};
const structuredOutputs = (_b = openaiOptions.structuredOutputs) != null ? _b : true;
if (topK != null) {
warnings.push({
type: "unsupported-setting",
setting: "topK"
});
}
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !structuredOutputs) {
warnings.push({
type: "unsupported-setting",
setting: "responseFormat",
details: "JSON response format schema is only supported with structuredOutputs"
});
}
const { messages, warnings: messageWarnings } = convertToOpenAIChatMessages(
{
prompt,
systemMessageMode: getSystemMessageMode(this.modelId)
}
);
warnings.push(...messageWarnings);
const strictJsonSchema = (_c = openaiOptions.strictJsonSchema) != null ? _c : false;
const baseArgs = {
// model id:
model: this.modelId,
// model specific settings:
logit_bias: openaiOptions.logitBias,
logprobs: openaiOptions.logprobs === true || typeof openaiOptions.logprobs === "number" ? true : void 0,
top_logprobs: typeof openaiOptions.logprobs === "number" ? openaiOptions.logprobs : typeof openaiOptions.logprobs === "boolean" ? openaiOptions.logprobs ? 0 : void 0 : void 0,
user: openaiOptions.user,
parallel_tool_calls: openaiOptions.parallelToolCalls,
// standardized settings:
max_tokens: maxOutputTokens,
temperature,
top_p: topP,
frequency_penalty: frequencyPenalty,
presence_penalty: presencePenalty,
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? structuredOutputs && responseFormat.schema != null ? {
type: "json_schema",
json_schema: {
schema: responseFormat.schema,
strict: strictJsonSchema,
name: (_d = responseFormat.name) != null ? _d : "response",
description: responseFormat.description
}
} : { type: "json_object" } : void 0,
stop: stopSequences,
seed,
// openai specific settings:
// TODO remove in next major version; we auto-map maxOutputTokens now
max_completion_tokens: openaiOptions.maxCompletionTokens,
store: openaiOptions.store,
metadata: openaiOptions.metadata,
prediction: openaiOptions.prediction,
reasoning_effort: openaiOptions.reasoningEffort,
service_tier: openaiOptions.serviceTier,
// messages:
messages
};
if (isReasoningModel(this.modelId)) {
if (baseArgs.temperature != null) {
baseArgs.temperature = void 0;
warnings.push({
type: "unsupported-setting",
setting: "temperature",
details: "temperature is not supported for reasoning models"
});
}
if (baseArgs.top_p != null) {
baseArgs.top_p = void 0;
warnings.push({
type: "unsupported-setting",
setting: "topP",
details: "topP is not supported for reasoning models"
});
}
if (baseArgs.frequency_penalty != null) {
baseArgs.frequency_penalty = void 0;
warnings.push({
type: "unsupported-setting",
setting: "frequencyPenalty",
details: "frequencyPenalty is not supported for reasoning models"
});
}
if (baseArgs.presence_penalty != null) {
baseArgs.presence_penalty = void 0;
warnings.push({
type: "unsupported-setting",
setting: "presencePenalty",
details: "presencePenalty is not supported for reasoning models"
});
}
if (baseArgs.logit_bias != null) {
baseArgs.logit_bias = void 0;
warnings.push({
type: "other",
message: "logitBias is not supported for reasoning models"
});
}
if (baseArgs.logprobs != null) {
baseArgs.logprobs = void 0;
warnings.push({
type: "other",
message: "logprobs is not supported for reasoning models"
});
}
if (baseArgs.top_logprobs != null) {
baseArgs.top_logprobs = void 0;
warnings.push({
type: "other",
message: "topLogprobs is not supported for reasoning models"
});
}
if (baseArgs.max_tokens != null) {
if (baseArgs.max_completion_tokens == null) {
baseArgs.max_completion_tokens = baseArgs.max_tokens;
}
baseArgs.max_tokens = void 0;
}
} else if (this.modelId.startsWith("gpt-4o-search-preview") || this.modelId.startsWith("gpt-4o-mini-search-preview")) {
if (baseArgs.temperature != null) {
baseArgs.temperature = void 0;
warnings.push({
type: "unsupported-setting",
setting: "temperature",
details: "temperature is not supported for the search preview models and has been removed."
});
}
}
if (openaiOptions.serviceTier === "flex" && !supportsFlexProcessing(this.modelId)) {
warnings.push({
type: "unsupported-setting",
setting: "serviceTier",
details: "flex processing is only available for o3 and o4-mini models"
});
baseArgs.service_tier = void 0;
}
if (openaiOptions.serviceTier === "priority" && !supportsPriorityProcessing(this.modelId)) {
warnings.push({
type: "unsupported-setting",
setting: "serviceTier",
details: "priority processing is only available for supported models (GPT-4, o3, o4-mini) and requires Enterprise access"
});
baseArgs.service_tier = void 0;
}
const {
tools: openaiTools,
toolChoice: openaiToolChoice,
toolWarnings
} = prepareTools({
tools,
toolChoice,
structuredOutputs,
strictJsonSchema
});
return {
args: {
...baseArgs,
tools: openaiTools,
tool_choice: openaiToolChoice
},
warnings: [...warnings, ...toolWarnings]
};
}
async doGenerate(options) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
const { args: body, warnings } = await this.getArgs(options);
const {
responseHeaders,
value: response,
rawValue: rawResponse
} = await postJsonToApi({
url: this.config.url({
path: "/chat/completions",
modelId: this.modelId
}),
headers: combineHeaders(this.config.headers(), options.headers),
body,
failedResponseHandler: openaiFailedResponseHandler,
successfulResponseHandler: createJsonResponseHandler(
openaiChatResponseSchema
),
abortSignal: options.abortSignal,
fetch: this.config.fetch
});
const choice = response.choices[0];
const content = [];
const text = choice.message.content;
if (text != null && text.length > 0) {
content.push({ type: "text", text });
}
for (const toolCall of (_a = choice.message.tool_calls) != null ? _a : []) {
content.push({
type: "tool-call",
toolCallId: (_b = toolCall.id) != null ? _b : generateId(),
toolName: toolCall.function.name,
input: toolCall.function.arguments
});
}
for (const annotation of (_c = choice.message.annotations) != null ? _c : []) {
content.push({
type: "source",
sourceType: "url",
id: generateId(),
url: annotation.url,
title: annotation.title
});
}
const completionTokenDetails = (_d = response.usage) == null ? void 0 : _d.completion_tokens_details;
const promptTokenDetails = (_e = response.usage) == null ? void 0 : _e.prompt_tokens_details;
const providerMetadata = { openai: {} };
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
providerMetadata.openai.acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
}
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
providerMetadata.openai.rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
}
if (((_f = choice.logprobs) == null ? void 0 : _f.content) != null) {
providerMetadata.openai.logprobs = choice.logprobs.content;
}
return {
content,
finishReason: mapOpenAIFinishReason(choice.finish_reason),
usage: {
inputTokens: (_h = (_g = response.usage) == null ? void 0 : _g.prompt_tokens) != null ? _h : void 0,
outputTokens: (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens) != null ? _j : void 0,
totalTokens: (_l = (_k = response.usage) == null ? void 0 : _k.total_tokens) != null ? _l : void 0,
reasoningTokens: (_m = completionTokenDetails == null ? void 0 : completionTokenDetails.reasoning_tokens) != null ? _m : void 0,
cachedInputTokens: (_n = promptTokenDetails == null ? void 0 : promptTokenDetails.cached_tokens) != null ? _n : void 0
},
request: { body },
response: {
...getResponseMetadata(response),
headers: responseHeaders,
body: rawResponse
},
warnings,
providerMetadata
};
}
async doStream(options) {
const { args, warnings } = await this.getArgs(options);
const body = {
...args,
stream: true,
stream_options: {
include_usage: true
}
};
const { responseHeaders, value: response } = await postJsonToApi({
url: this.config.url({
path: "/chat/completions",
modelId: this.modelId
}),
headers: combineHeaders(this.config.headers(), options.headers),
body,
failedResponseHandler: openaiFailedResponseHandler,
successfulResponseHandler: createEventSourceResponseHandler(
openaiChatChunkSchema
),
abortSignal: options.abortSignal,
fetch: this.config.fetch
});
const toolCalls = [];
let finishReason = "unknown";
const usage = {
inputTokens: void 0,
outputTokens: void 0,
totalTokens: void 0
};
let isFirstChunk = true;
let isActiveText = false;
const providerMetadata = { openai: {} };
return {
stream: response.pipeThrough(
new TransformStream({
start(controller) {
controller.enqueue({ type: "stream-start", warnings });
},
transform(chunk, controller) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
if (options.includeRawChunks) {
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
}
if (!chunk.success) {
finishReason = "error";
controller.enqueue({ type: "error", error: chunk.error });
return;
}
const value = chunk.value;
if ("error" in value) {
finishReason = "error";
controller.enqueue({ type: "error", error: value.error });
return;
}
if (isFirstChunk) {
isFirstChunk = false;
controller.enqueue({
type: "response-metadata",
...getResponseMetadata(value)
});
}
if (value.usage != null) {
usage.inputTokens = (_a = value.usage.prompt_tokens) != null ? _a : void 0;
usage.outputTokens = (_b = value.usage.completion_tokens) != null ? _b : void 0;
usage.totalTokens = (_c = value.usage.total_tokens) != null ? _c : void 0;
usage.reasoningTokens = (_e = (_d = value.usage.completion_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : void 0;
usage.cachedInputTokens = (_g = (_f = value.usage.prompt_tokens_details) == null ? void 0 : _f.cached_tokens) != null ? _g : void 0;
if (((_h = value.usage.completion_tokens_details) == null ? void 0 : _h.accepted_prediction_tokens) != null) {
providerMetadata.openai.acceptedPredictionTokens = (_i = value.usage.completion_tokens_details) == null ? void 0 : _i.accepted_prediction_tokens;
}
if (((_j = value.usage.completion_tokens_details) == null ? void 0 : _j.rejected_prediction_tokens) != null) {
providerMetadata.openai.rejectedPredictionTokens = (_k = value.usage.completion_tokens_details) == null ? void 0 : _k.rejected_prediction_tokens;
}
}
const choice = value.choices[0];
if ((choice == null ? void 0 : choice.finish_reason) != null) {
finishReason = mapOpenAIFinishReason(choice.finish_reason);
}
if (((_l = choice == null ? void 0 : choice.logprobs) == null ? void 0 : _l.content) != null) {
providerMetadata.openai.logprobs = choice.logprobs.content;
}
if ((choice == null ? void 0 : choice.delta) == null) {
return;
}
const delta = choice.delta;
if (delta.content != null) {
if (!isActiveText) {
controller.enqueue({ type: "text-start", id: "0" });
isActiveText = true;
}
controller.enqueue({
type: "text-delta",
id: "0",
delta: delta.content
});
}
if (delta.tool_calls != null) {
for (const toolCallDelta of delta.tool_calls) {
const index = toolCallDelta.index;
if (toolCalls[index] == null) {
if (toolCallDelta.type !== "function") {
throw new InvalidResponseDataError({
data: toolCallDelta,
message: `Expected 'function' type.`
});
}
if (toolCallDelta.id == null) {
throw new InvalidResponseDataError({
data: toolCallDelta,
message: `Expected 'id' to be a string.`
});
}
if (((_m = toolCallDelta.function) == null ? void 0 : _m.name) == null) {
throw new InvalidResponseDataError({
data: toolCallDelta,
message: `Expected 'function.name' to be a string.`
});
}
controller.enqueue({
type: "tool-input-start",
id: toolCallDelta.id,
toolName: toolCallDelta.function.name
});
toolCalls[index] = {
id: toolCallDelta.id,
type: "function",
function: {
name: toolCallDelta.function.name,
arguments: (_n = toolCallDelta.function.arguments) != null ? _n : ""
},
hasFinished: false
};
const toolCall2 = toolCalls[index];
if (((_o = toolCall2.function) == null ? void 0 : _o.name) != null && ((_p = toolCall2.function) == null ? void 0 : _p.arguments) != null) {
if (toolCall2.function.arguments.length > 0) {
controller.enqueue({
type: "tool-input-delta",
id: toolCall2.id,
delta: toolCall2.function.arguments
});
}
if (isParsableJson(toolCall2.function.arguments)) {
controller.enqueue({
type: "tool-input-end",
id: toolCall2.id
});
controller.enqueue({
type: "tool-call",
toolCallId: (_q = toolCall2.id) != null ? _q : generateId(),
toolName: toolCall2.function.name,
input: toolCall2.function.arguments
});
toolCall2.hasFinished = true;
}
}
continue;
}
const toolCall = toolCalls[index];
if (toolCall.hasFinished) {
continue;
}
if (((_r = toolCallDelta.function) == null ? void 0 : _r.arguments) != null) {
toolCall.function.arguments += (_t = (_s = toolCallDelta.function) == null ? void 0 : _s.arguments) != null ? _t : "";
}
controller.enqueue({
type: "tool-input-delta",
id: toolCall.id,
delta: (_u = toolCallDelta.function.arguments) != null ? _u : ""
});
if (((_v = toolCall.function) == null ? void 0 : _v.name) != null && ((_w = toolCall.function) == null ? void 0 : _w.arguments) != null && isParsableJson(toolCall.function.arguments)) {
controller.enqueue({
type: "tool-input-end",
id: toolCall.id
});
controller.enqueue({
type: "tool-call",
toolCallId: (_x = toolCall.id) != null ? _x : generateId(),
toolName: toolCall.function.name,
input: toolCall.function.arguments
});
toolCall.hasFinished = true;
}
}
}
if (delta.annotations != null) {
for (const annotation of delta.annotations) {
controller.enqueue({
type: "source",
sourceType: "url",
id: generateId(),
url: annotation.url,
title: annotation.title
});
}
}
},
flush(controller) {
if (isActiveText) {
controller.enqueue({ type: "text-end", id: "0" });
}
controller.enqueue({
type: "finish",
finishReason,
usage,
...providerMetadata != null ? { providerMetadata } : {}
});
}
})
),
request: { body },
response: { headers: responseHeaders }
};
}
};
var openaiTokenUsageSchema = z5.object({
prompt_tokens: z5.number().nullish(),
completion_tokens: z5.number().nullish(),
total_tokens: z5.number().nullish(),
prompt_tokens_details: z5.object({
cached_tokens: z5.number().nullish()
}).nullish(),
completion_tokens_details: z5.object({
reasoning_tokens: z5.number().nullish(),
accepted_prediction_tokens: z5.number().nullish(),
rejected_prediction_tokens: z5.number().nullish()
}).nullish()
}).nullish();
var openaiChatResponseSchema = z5.object({
id: z5.string().nullish(),
created: z5.number().nullish(),
model: z5.string().nullish(),
choices: z5.array(
z5.object({
message: z5.object({
role: z5.literal("assistant").nullish(),
content: z5.string().nullish(),
tool_calls: z5.array(
z5.object({
id: z5.string().nullish(),
type: z5.literal("function"),
function: z5.object({
name: z5.string(),
arguments: z5.string()
})
})
).nullish(),
annotations: z5.array(
z5.object({
type: z5.literal("url_citation"),
start_index: z5.number(),
end_index: z5.number(),
url: z5.string(),
title: z5.string()
})
).nullish()
}),
index: z5.number(),
logprobs: z5.object({
content: z5.array(
z5.object({
token: z5.string(),
logprob: z5.number(),
top_logprobs: z5.array(
z5.object({
token: z5.string(),
logprob: z5.number()
})
)
})
).nullish()
}).nullish(),
finish_reason: z5.string().nullish()
})
),
usage: openaiTokenUsageSchema
});
var openaiChatChunkSchema = z5.union([
z5.object({
id: z5.string().nullish(),
created: z5.number().nullish(),
model: z5.string().nullish(),
choices: z5.array(
z5.object({
delta: z5.object({
role: z5.enum(["assistant"]).nullish(),
content: z5.string().nullish(),
tool_calls: z5.array(
z5.object({
index: z5.number(),
id: z5.string().nullish(),
type: z5.literal("function").nullish(),
function: z5.object({
name: z5.string().nullish(),
arguments: z5.string().nullish()
})
})
).nullish(),
annotations: z5.array(
z5.object({
type: z5.literal("url_citation"),
start_index: z5.number(),
end_index: z5.number(),
url: z5.string(),
title: z5.string()
})
).nullish()
}).nullish(),
logprobs: z5.object({
content: z5.array(
z5.object({
token: z5.string(),
logprob: z5.number(),
top_logprobs: z5.array(
z5.object({
token: z5.string(),
logprob: z5.number()
})
)
})
).nullish()
}).nullish(),
finish_reason: z5.string().nullish(),
index: z5.number()
})
),
usage: openaiTokenUsageSchema
}),
openaiErrorDataSchema
]);
function isReasoningModel(modelId) {
return modelId.startsWith("o");
}
function supportsFlexProcessing(modelId) {
return modelId.startsWith("o3") || modelId.startsWith("o4-mini");
}
function supportsPriorityProcessing(modelId) {
return modelId.startsWith("gpt-4") || modelId.startsWith("o3") || modelId.startsWith("o4-mini");
}
function getSystemMessageMode(modelId) {
var _a, _b;
if (!isReasoningModel(modelId)) {
return "system";
}
return (_b = (_a = reasoningModels[modelId]) == null ? void 0 : _a.systemMessageMode) != null ? _b : "developer";
}
var reasoningModels = {
"o1-mini": {
systemMessageMode: "remove"
},
"o1-mini-2024-09-12": {
systemMessageMode: "remove"
},
"o1-preview": {
systemMessageMode: "remove"
},
"o1-preview-2024-09-12": {
systemMessageMode: "remove"
},
o3: {
systemMessageMode: "developer"
},
"o3-2025-04-16": {
systemMessageMode: "developer"
},
"o3-mini": {
systemMessageMode: "developer"
},
"o3-mini-2025-01-31": {
systemMessageMode: "developer"
},
"o4-mini": {
systemMessageMode: "developer"
},
"o4-mini-2025-04-16": {
systemMessageMode: "developer"
}
};
// src/openai-completion-language-model.ts
import {
combineHeaders as combineHeaders2,
createEventSourceResponseHandler as createEventSourceResponseHandler2,
createJsonResponseHandler as createJsonResponseHandler2,
parseProviderOptions as parseProviderOptions2,
postJsonToApi as postJsonToApi2
} from "@ai-sdk/provider-utils";
import { z as z7 } from "zod/v4";
// src/convert-to-openai-completion-prompt.ts
import {
InvalidPromptError,
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
} from "@ai-sdk/provider";
function convertToOpenAICompletionPrompt({
prompt,
user = "user",
assistant = "assistant"
}) {
let text = "";
if (prompt[0].role === "system") {
text += `${prompt[0].content}
`;
prompt = prompt.slice(1);
}
for (const { role, content } of prompt) {
switch (role) {
case "system": {
throw new InvalidPromptError({
message: "Unexpected system message in prompt: ${content}",
prompt
});
}
case "user": {
const userMessage = content.map((part) => {
switch (part.type) {
case "text": {
return part.text;
}
}
}).filter(Boolean).join("");
text += `${user}:
${userMessage}
`;
break;
}
case "assistant": {
const assistantMessage = content.map((part) => {
switch (part.type) {
case "text": {
return part.text;
}
case "tool-call": {
throw new UnsupportedFunctionalityError3({
functionality: "tool-call messages"
});
}
}
}).join("");
text += `${assistant}:
${assistantMessage}
`;
break;
}
case "tool": {
throw new UnsupportedFunctionalityError3({
functionality: "tool messages"
});
}
default: {
const _exhaustiveCheck = role;
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
}
}
}
text += `${assistant}:
`;
return {
prompt: text,
stopSequences: [`
${user}:`]
};
}
// src/openai-completion-options.ts
import { z as z6 } from "zod/v4";
var openaiCompletionProviderOptions = z6.object({
/**
Echo back the prompt in addition to the completion.
*/
echo: z6.boolean().optional(),
/**
Modify the likelihood of specified tokens appearing in the completion.
Accepts a JSON object that maps tokens (specified by their token ID in
the GPT tokenizer) to an associated bias value from -100 to 100. You
can use this tokenizer tool to convert text to token IDs. Mathematically,
the bias is added to the logits generated by the model prior to sampling.
The exact effect will vary per model, but values between -1 and 1 should
decrease or increase likelihood of selection; values like -100 or 100
should result in a ban or exclusive selection of the relevant token.
As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
token from being generated.
*/
logitBias: z6.record(z6.string(), z6.number()).optional(),
/**
The suffix that comes after a completion of inserted text.
*/
suffix: z6.string().optional(),
/**
A unique identifier representing your end-user, which can help OpenAI to
monitor and detect abuse. Learn more.
*/
user: z6.string().optional(),
/**
Return the log probabilities of the tokens. Including logprobs will increase
the response size and can slow down response times. However, it can
be useful to better understand how the model is behaving.
Setting to true will return the log probabilities of the tokens that
were generated.
Setting to a number will return the log probabilities of the top n
tokens that were generated.
*/
logprobs: z6.union([z6.boolean(), z6.number()]).optional()
});
// src/openai-completion-language-model.ts
var OpenAICompletionLanguageModel = class {
constructor(modelId, config) {
this.specificationVersion = "v2";
this.supportedUrls = {
// No URLs are supported for completion models.
};
this.modelId = modelId;
this.config = config;
}
get providerOptionsName() {
return this.config.provider.split(".")[0].trim();
}
get provider() {
return this.config.provider;
}
async getArgs({
prompt,
maxOutputTokens,
temperature,
topP,
topK,
frequencyPenalty,
presencePenalty,
stopSequences: userStopSequences,
responseFormat,
tools,
toolChoice,
seed,
providerOptions
}) {
const warnings = [];
const openaiOptions = {
...await parseProviderOptions2({
provider: "openai",
providerOptions,
schema: openaiCompletionProviderOptions
}),
...await parseProviderOptions2({
provider: this.providerOptionsName,
providerOptions,
schema: openaiCompletionProviderOptions
})
};
if (topK != null) {
warnings.push({ type: "unsupported-setting", setting: "topK" });
}
if (tools == null ? void 0 : tools.length) {
warnings.push({ type: "unsupported-setting", setting: "tools" });
}
if (toolChoice != null) {
warnings.push({ type: "unsupported-setting", setting: "toolChoice" });
}
if (responseFormat != null && responseFormat.type !== "text") {
warnings.push({
type: "unsupported-setting",
setting: "responseFormat",
details: "JSON response format is not supported."
});
}
const { prompt: completionPrompt, stopSequences } = convertToOpenAICompletionPrompt({ prompt });
const stop = [...stopSequences != null ? stopSequences : [], ...userStopSequences != null ? userStopSequences : []];
return {
args: {
// model id:
model: this.modelId,
// model specific settings:
echo: openaiOptions.echo,
logit_bias: openaiOptions.logitBias,
logprobs: (openaiOptions == null ? void 0 : openaiOptions.logprobs) === true ? 0 : (openaiOptions == null ? void 0 : openaiOptions.logprobs) === false ? void 0 : openaiOptions == null ? void 0 : openaiOptions.logprobs,
suffix: openaiOptions.suffix,
user: openaiOptions.user,
// standardized settings:
max_tokens: maxOutputTokens,
temperature,
top_p: topP,
frequency_penalty: frequencyPenalty,
presence_penalty: presencePenalty,
seed,
// prompt:
prompt: completionPrompt,
// stop sequences:
stop: stop.length > 0 ? stop : void 0
},
warnings
};
}
async doGenerate(options) {
var _a, _b, _c;
const { args, warnings } = await this.getArgs(options);
const {
responseHeaders,
value: response,
rawValue: rawResponse
} = await postJsonToApi2({
url: this.config.url({
path: "/completions",
modelId: this.modelId
}),
headers: combineHeaders2(this.config.headers(), options.headers),
body: args,
failedResponseHandler: openaiFailedResponseHandler,
successfulResponseHandler: createJsonResponseHandler2(
openaiCompletionResponseSchema
),
abortSignal: options.abortSignal,
fetch: this.config.fetch
});
const choice = response.choices[0];
const providerMetadata = { openai: {} };
if (choice.logprobs != null) {
providerMetadata.openai.logprobs = choice.logprobs;
}
return {
content: [{ type: "text", text: choice.text }],
usage: {
inputTokens: (_a = response.usage) == null ? void 0 : _a.prompt_tokens,
outputTokens: (_b = response.usage) == null ? void 0 : _b.completion_tokens,
totalTokens: (_c = response.usage) == null ? void 0 : _c.total_tokens
},
finishReason: mapOpenAIFinishReason(choice.finish_reason),
request: { body: args },
response: {
...getResponseMetadata(response),
headers: responseHeaders,
body: rawResponse
},
providerMetadata,
warnings
};
}
async doStream(options) {
const { args, warnings } = await this.getArgs(options);
const body = {
...args,
stream: true,
stream_options: {
include_usage: true
}
};
const { responseHeaders, value: response } = await postJsonToApi2({
url: this.config.url({
path: "/completions",
modelId: this.modelId
}),
headers: combineHeaders2(this.config.headers(), options.headers),
body,
failedResponseHandler: openaiFailedResponseHandler,
successfulResponseHandler: createEventSourceResponseHandler2(
openaiCompletionChunkSchema
),
abortSignal: options.abortSignal,
fetch: this.config.fetch
});
let finishReason = "unknown";
const providerMetadata = { openai: {} };
const usage = {
inputTokens: void 0,
outputTokens: void 0,
totalTokens: void 0
};
let isFirstChunk = true;
return {
stream: response.pipeThrough(
new TransformStream({
start(controller) {
controller.enqueue({ type: "stream-start", warnings });
},
transform(chunk, controller) {
if (options.includeRawChunks) {
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
}
if (!chunk.success) {
finishReason = "error";
controller.enqueue({ type: "error", error: chunk.error });
return;
}
const value = chunk.value;
if ("error" in value) {
finishReason = "error";
controller.enqueue({ type: "error", error: value.error });
return;
}
if (isFirstChunk) {
isFirstChunk = false;
controller.enqueue({
type: "response-metadata",
...getResponseMetadata(value)
});
controller.enqueue({ type: "text-start", id: "0" });
}
if (value.usage != null) {
usage.inputTokens = value.usage.prompt_tokens;
usage.outputTokens = value.usage.completion_tokens;
usage.totalTokens = value.usage.total_tokens;
}
const choice = value.choices[0];
if ((choice == null ? void 0 : choice.finish_reason) != null) {
finishReason = mapOpenAIFinishReason(choice.finish_reason);
}
if ((choice == null ? void 0 : choice.logprobs) != null) {
providerMetadata.openai.logprobs = choice.logprobs;
}
if ((choice == null ? void 0 : choice.text) != null && choice.text.length > 0) {
controller.enqueue({
type: "text-delta",
id: "0",
delta: choice.text
});
}
},
flush(controller) {