@ai-sdk/openai-compatible 3.0.0-beta.3 → 3.0.0-beta.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,10 @@ import {
4
4
  SharedV4ProviderOptions,
5
5
  SharedV4Warning,
6
6
  } from '@ai-sdk/provider';
7
+ import {
8
+ toCamelCase,
9
+ warnIfDeprecatedProviderOptionsKey,
10
+ } from '../utils/to-camel-case';
7
11
  import {
8
12
  combineHeaders,
9
13
  convertBase64ToUint8Array,
@@ -14,6 +18,9 @@ import {
14
18
  FetchFunction,
15
19
  postFormDataToApi,
16
20
  postJsonToApi,
21
+ serializeModelOptions,
22
+ WORKFLOW_SERIALIZE,
23
+ WORKFLOW_DESERIALIZE,
17
24
  } from '@ai-sdk/provider-utils';
18
25
  import { z } from 'zod/v4';
19
26
  import {
@@ -24,7 +31,7 @@ import { OpenAICompatibleImageModelId } from './openai-compatible-image-settings
24
31
 
25
32
  export type OpenAICompatibleImageModelConfig = {
26
33
  provider: string;
27
- headers: () => Record<string, string | undefined>;
34
+ headers?: () => Record<string, string | undefined>;
28
35
  url: (options: { modelId: string; path: string }) => string;
29
36
  fetch?: FetchFunction;
30
37
  errorStructure?: ProviderErrorStructure<any>;
@@ -48,15 +55,34 @@ export class OpenAICompatibleImageModel implements ImageModelV4 {
48
55
  return this.config.provider.split('.')[0].trim();
49
56
  }
50
57
 
58
+ static [WORKFLOW_SERIALIZE](model: OpenAICompatibleImageModel) {
59
+ return serializeModelOptions({
60
+ modelId: model.modelId,
61
+ config: model.config,
62
+ });
63
+ }
64
+
65
+ static [WORKFLOW_DESERIALIZE](options: {
66
+ modelId: string;
67
+ config: OpenAICompatibleImageModelConfig;
68
+ }) {
69
+ return new OpenAICompatibleImageModel(options.modelId, options.config);
70
+ }
71
+
51
72
  constructor(
52
73
  readonly modelId: OpenAICompatibleImageModelId,
53
74
  private readonly config: OpenAICompatibleImageModelConfig,
54
75
  ) {}
55
76
 
56
- // TODO: deprecate non-camelCase keys and remove in future major version
57
77
  private getArgs(
58
78
  providerOptions: SharedV4ProviderOptions,
79
+ warnings: SharedV4Warning[],
59
80
  ): Record<string, unknown> {
81
+ warnIfDeprecatedProviderOptionsKey({
82
+ rawName: this.providerOptionsKey,
83
+ providerOptions,
84
+ warnings,
85
+ });
60
86
  return {
61
87
  ...providerOptions[this.providerOptionsKey],
62
88
  ...providerOptions[toCamelCase(this.providerOptionsKey)],
@@ -94,7 +120,7 @@ export class OpenAICompatibleImageModel implements ImageModelV4 {
94
120
 
95
121
  const currentDate = this.config._internal?.currentDate?.() ?? new Date();
96
122
 
97
- const args = this.getArgs(providerOptions);
123
+ const args = this.getArgs(providerOptions, warnings);
98
124
 
99
125
  // Image editing mode - use form data and /images/edits endpoint
100
126
  if (files != null && files.length > 0) {
@@ -103,7 +129,7 @@ export class OpenAICompatibleImageModel implements ImageModelV4 {
103
129
  path: '/images/edits',
104
130
  modelId: this.modelId,
105
131
  }),
106
- headers: combineHeaders(this.config.headers(), headers),
132
+ headers: combineHeaders(this.config.headers?.(), headers),
107
133
  formData: convertToFormData<OpenAICompatibleFormDataInput>({
108
134
  model: this.modelId,
109
135
  prompt,
@@ -140,7 +166,7 @@ export class OpenAICompatibleImageModel implements ImageModelV4 {
140
166
  path: '/images/generations',
141
167
  modelId: this.modelId,
142
168
  }),
143
- headers: combineHeaders(this.config.headers(), headers),
169
+ headers: combineHeaders(this.config.headers?.(), headers),
144
170
  body: {
145
171
  model: this.modelId,
146
172
  prompt,
@@ -199,7 +225,3 @@ async function fileToBlob(file: ImageModelV4File): Promise<Blob> {
199
225
 
200
226
  return new Blob([data as BlobPart], { type: file.mediaType });
201
227
  }
202
-
203
- function toCamelCase(str: string): string {
204
- return str.replace(/[_-]([a-z])/g, g => g[1].toUpperCase());
205
- }
@@ -0,0 +1,43 @@
1
+ import { SharedV4Warning } from '@ai-sdk/provider';
2
+
3
+ export function toCamelCase(str: string): string {
4
+ return str.replace(/[_-]([a-z])/g, g => g[1].toUpperCase());
5
+ }
6
+
7
+ /**
8
+ Resolves which key to use for providerMetadata based on what the caller
9
+ passed in providerOptions. Returns the camelCase variant when the caller
10
+ supplied it, otherwise falls back to the raw name.
11
+ */
12
+ export function resolveProviderOptionsKey(
13
+ rawName: string,
14
+ providerOptions: Record<string, unknown> | undefined,
15
+ ): string {
16
+ const camelName = toCamelCase(rawName);
17
+ if (camelName !== rawName && providerOptions?.[camelName] != null) {
18
+ return camelName;
19
+ }
20
+ return rawName;
21
+ }
22
+
23
+ /**
24
+ Pushes a deprecation warning when the user supplies providerOptions under a non-camelCase key
25
+ */
26
+ export function warnIfDeprecatedProviderOptionsKey({
27
+ rawName,
28
+ providerOptions,
29
+ warnings,
30
+ }: {
31
+ rawName: string;
32
+ providerOptions: Record<string, unknown> | undefined;
33
+ warnings: SharedV4Warning[];
34
+ }): void {
35
+ const camelName = toCamelCase(rawName);
36
+ if (camelName !== rawName && providerOptions?.[rawName] != null) {
37
+ warnings.push({
38
+ type: 'deprecated',
39
+ setting: `providerOptions key '${rawName}'`,
40
+ message: `Use '${camelName}' instead.`,
41
+ });
42
+ }
43
+ }
package/dist/index.d.mts DELETED
@@ -1,290 +0,0 @@
1
- import { SharedV4ProviderMetadata, LanguageModelV4, LanguageModelV4CallOptions, LanguageModelV4GenerateResult, LanguageModelV4StreamResult, EmbeddingModelV4, ImageModelV4, ProviderV4 } from '@ai-sdk/provider';
2
- import { FetchFunction } from '@ai-sdk/provider-utils';
3
- import { ZodType, z } from 'zod/v4';
4
-
5
- declare const openaiCompatibleErrorDataSchema: z.ZodObject<{
6
- error: z.ZodObject<{
7
- message: z.ZodString;
8
- type: z.ZodOptional<z.ZodNullable<z.ZodString>>;
9
- param: z.ZodOptional<z.ZodNullable<z.ZodAny>>;
10
- code: z.ZodOptional<z.ZodNullable<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>>;
11
- }, z.core.$strip>;
12
- }, z.core.$strip>;
13
- type OpenAICompatibleErrorData = z.infer<typeof openaiCompatibleErrorDataSchema>;
14
- type ProviderErrorStructure<T> = {
15
- errorSchema: ZodType<T>;
16
- errorToMessage: (error: T) => string;
17
- isRetryable?: (response: Response, error?: T) => boolean;
18
- };
19
-
20
- type OpenAICompatibleChatModelId = string;
21
- declare const openaiCompatibleLanguageModelChatOptions: z.ZodObject<{
22
- user: z.ZodOptional<z.ZodString>;
23
- reasoningEffort: z.ZodOptional<z.ZodString>;
24
- textVerbosity: z.ZodOptional<z.ZodString>;
25
- strictJsonSchema: z.ZodOptional<z.ZodBoolean>;
26
- }, z.core.$strip>;
27
- type OpenAICompatibleLanguageModelChatOptions = z.infer<typeof openaiCompatibleLanguageModelChatOptions>;
28
-
29
- /**
30
- * Extracts provider-specific metadata from API responses.
31
- * Used to standardize metadata handling across different LLM providers while allowing
32
- * provider-specific metadata to be captured.
33
- */
34
- type MetadataExtractor = {
35
- /**
36
- * Extracts provider metadata from a complete, non-streaming response.
37
- *
38
- * @param parsedBody - The parsed response JSON body from the provider's API.
39
- *
40
- * @returns Provider-specific metadata or undefined if no metadata is available.
41
- * The metadata should be under a key indicating the provider id.
42
- */
43
- extractMetadata: ({ parsedBody, }: {
44
- parsedBody: unknown;
45
- }) => Promise<SharedV4ProviderMetadata | undefined>;
46
- /**
47
- * Creates an extractor for handling streaming responses. The returned object provides
48
- * methods to process individual chunks and build the final metadata from the accumulated
49
- * stream data.
50
- *
51
- * @returns An object with methods to process chunks and build metadata from a stream
52
- */
53
- createStreamExtractor: () => {
54
- /**
55
- * Process an individual chunk from the stream. Called for each chunk in the response stream
56
- * to accumulate metadata throughout the streaming process.
57
- *
58
- * @param parsedChunk - The parsed JSON response chunk from the provider's API
59
- */
60
- processChunk(parsedChunk: unknown): void;
61
- /**
62
- * Builds the metadata object after all chunks have been processed.
63
- * Called at the end of the stream to generate the complete provider metadata.
64
- *
65
- * @returns Provider-specific metadata or undefined if no metadata is available.
66
- * The metadata should be under a key indicating the provider id.
67
- */
68
- buildMetadata(): SharedV4ProviderMetadata | undefined;
69
- };
70
- };
71
-
72
- type OpenAICompatibleChatConfig = {
73
- provider: string;
74
- headers: () => Record<string, string | undefined>;
75
- url: (options: {
76
- modelId: string;
77
- path: string;
78
- }) => string;
79
- fetch?: FetchFunction;
80
- includeUsage?: boolean;
81
- errorStructure?: ProviderErrorStructure<any>;
82
- metadataExtractor?: MetadataExtractor;
83
- /**
84
- * Whether the model supports structured outputs.
85
- */
86
- supportsStructuredOutputs?: boolean;
87
- /**
88
- * The supported URLs for the model.
89
- */
90
- supportedUrls?: () => LanguageModelV4['supportedUrls'];
91
- /**
92
- * Optional function to transform the request body before sending it to the API.
93
- * This is useful for proxy providers that may require a different request format
94
- * than the official OpenAI API.
95
- */
96
- transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
97
- };
98
- declare class OpenAICompatibleChatLanguageModel implements LanguageModelV4 {
99
- readonly specificationVersion = "v4";
100
- readonly supportsStructuredOutputs: boolean;
101
- readonly modelId: OpenAICompatibleChatModelId;
102
- private readonly config;
103
- private readonly failedResponseHandler;
104
- private readonly chunkSchema;
105
- constructor(modelId: OpenAICompatibleChatModelId, config: OpenAICompatibleChatConfig);
106
- get provider(): string;
107
- private get providerOptionsName();
108
- get supportedUrls(): Record<string, RegExp[]> | PromiseLike<Record<string, RegExp[]>>;
109
- private transformRequestBody;
110
- private getArgs;
111
- doGenerate(options: LanguageModelV4CallOptions): Promise<LanguageModelV4GenerateResult>;
112
- doStream(options: LanguageModelV4CallOptions): Promise<LanguageModelV4StreamResult>;
113
- }
114
-
115
- type OpenAICompatibleCompletionModelId = string;
116
- declare const openaiCompatibleLanguageModelCompletionOptions: z.ZodObject<{
117
- echo: z.ZodOptional<z.ZodBoolean>;
118
- logitBias: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
119
- suffix: z.ZodOptional<z.ZodString>;
120
- user: z.ZodOptional<z.ZodString>;
121
- }, z.core.$strip>;
122
- type OpenAICompatibleLanguageModelCompletionOptions = z.infer<typeof openaiCompatibleLanguageModelCompletionOptions>;
123
-
124
- type OpenAICompatibleCompletionConfig = {
125
- provider: string;
126
- includeUsage?: boolean;
127
- headers: () => Record<string, string | undefined>;
128
- url: (options: {
129
- modelId: string;
130
- path: string;
131
- }) => string;
132
- fetch?: FetchFunction;
133
- errorStructure?: ProviderErrorStructure<any>;
134
- /**
135
- * The supported URLs for the model.
136
- */
137
- supportedUrls?: () => LanguageModelV4['supportedUrls'];
138
- };
139
- declare class OpenAICompatibleCompletionLanguageModel implements LanguageModelV4 {
140
- readonly specificationVersion = "v4";
141
- readonly modelId: OpenAICompatibleCompletionModelId;
142
- private readonly config;
143
- private readonly failedResponseHandler;
144
- private readonly chunkSchema;
145
- constructor(modelId: OpenAICompatibleCompletionModelId, config: OpenAICompatibleCompletionConfig);
146
- get provider(): string;
147
- private get providerOptionsName();
148
- get supportedUrls(): Record<string, RegExp[]> | PromiseLike<Record<string, RegExp[]>>;
149
- private getArgs;
150
- doGenerate(options: LanguageModelV4CallOptions): Promise<LanguageModelV4GenerateResult>;
151
- doStream(options: LanguageModelV4CallOptions): Promise<LanguageModelV4StreamResult>;
152
- }
153
-
154
- type OpenAICompatibleEmbeddingModelId = string;
155
- declare const openaiCompatibleEmbeddingModelOptions: z.ZodObject<{
156
- dimensions: z.ZodOptional<z.ZodNumber>;
157
- user: z.ZodOptional<z.ZodString>;
158
- }, z.core.$strip>;
159
- type OpenAICompatibleEmbeddingModelOptions = z.infer<typeof openaiCompatibleEmbeddingModelOptions>;
160
-
161
- type OpenAICompatibleEmbeddingConfig = {
162
- /**
163
- * Override the maximum number of embeddings per call.
164
- */
165
- maxEmbeddingsPerCall?: number;
166
- /**
167
- * Override the parallelism of embedding calls.
168
- */
169
- supportsParallelCalls?: boolean;
170
- provider: string;
171
- url: (options: {
172
- modelId: string;
173
- path: string;
174
- }) => string;
175
- headers: () => Record<string, string | undefined>;
176
- fetch?: FetchFunction;
177
- errorStructure?: ProviderErrorStructure<any>;
178
- };
179
- declare class OpenAICompatibleEmbeddingModel implements EmbeddingModelV4 {
180
- readonly specificationVersion = "v4";
181
- readonly modelId: OpenAICompatibleEmbeddingModelId;
182
- private readonly config;
183
- get provider(): string;
184
- get maxEmbeddingsPerCall(): number;
185
- get supportsParallelCalls(): boolean;
186
- constructor(modelId: OpenAICompatibleEmbeddingModelId, config: OpenAICompatibleEmbeddingConfig);
187
- private get providerOptionsName();
188
- doEmbed({ values, headers, abortSignal, providerOptions, }: Parameters<EmbeddingModelV4['doEmbed']>[0]): Promise<Awaited<ReturnType<EmbeddingModelV4['doEmbed']>>>;
189
- }
190
-
191
- type OpenAICompatibleImageModelId = string;
192
-
193
- type OpenAICompatibleImageModelConfig = {
194
- provider: string;
195
- headers: () => Record<string, string | undefined>;
196
- url: (options: {
197
- modelId: string;
198
- path: string;
199
- }) => string;
200
- fetch?: FetchFunction;
201
- errorStructure?: ProviderErrorStructure<any>;
202
- _internal?: {
203
- currentDate?: () => Date;
204
- };
205
- };
206
- declare class OpenAICompatibleImageModel implements ImageModelV4 {
207
- readonly modelId: OpenAICompatibleImageModelId;
208
- private readonly config;
209
- readonly specificationVersion = "v4";
210
- readonly maxImagesPerCall = 10;
211
- get provider(): string;
212
- /**
213
- * The provider options key used to extract provider-specific options.
214
- */
215
- private get providerOptionsKey();
216
- constructor(modelId: OpenAICompatibleImageModelId, config: OpenAICompatibleImageModelConfig);
217
- private getArgs;
218
- doGenerate({ prompt, n, size, aspectRatio, seed, providerOptions, headers, abortSignal, files, mask, }: Parameters<ImageModelV4['doGenerate']>[0]): Promise<Awaited<ReturnType<ImageModelV4['doGenerate']>>>;
219
- }
220
-
221
- interface OpenAICompatibleProvider<CHAT_MODEL_IDS extends string = string, COMPLETION_MODEL_IDS extends string = string, EMBEDDING_MODEL_IDS extends string = string, IMAGE_MODEL_IDS extends string = string> extends Omit<ProviderV4, 'imageModel'> {
222
- (modelId: CHAT_MODEL_IDS): LanguageModelV4;
223
- languageModel(modelId: CHAT_MODEL_IDS, config?: Partial<OpenAICompatibleChatConfig>): LanguageModelV4;
224
- chatModel(modelId: CHAT_MODEL_IDS): LanguageModelV4;
225
- completionModel(modelId: COMPLETION_MODEL_IDS): LanguageModelV4;
226
- embeddingModel(modelId: EMBEDDING_MODEL_IDS): EmbeddingModelV4;
227
- /**
228
- * @deprecated Use `embeddingModel` instead.
229
- */
230
- textEmbeddingModel(modelId: EMBEDDING_MODEL_IDS): EmbeddingModelV4;
231
- imageModel(modelId: IMAGE_MODEL_IDS): ImageModelV4;
232
- }
233
- interface OpenAICompatibleProviderSettings {
234
- /**
235
- * Base URL for the API calls.
236
- */
237
- baseURL: string;
238
- /**
239
- * Provider name.
240
- */
241
- name: string;
242
- /**
243
- * API key for authenticating requests. If specified, adds an `Authorization`
244
- * header to request headers with the value `Bearer <apiKey>`. This will be added
245
- * before any headers potentially specified in the `headers` option.
246
- */
247
- apiKey?: string;
248
- /**
249
- * Optional custom headers to include in requests. These will be added to request headers
250
- * after any headers potentially added by use of the `apiKey` option.
251
- */
252
- headers?: Record<string, string>;
253
- /**
254
- * Optional custom url query parameters to include in request urls.
255
- */
256
- queryParams?: Record<string, string>;
257
- /**
258
- * Custom fetch implementation. You can use it as a middleware to intercept requests,
259
- * or to provide a custom fetch implementation for e.g. testing.
260
- */
261
- fetch?: FetchFunction;
262
- /**
263
- * Include usage information in streaming responses.
264
- */
265
- includeUsage?: boolean;
266
- /**
267
- * Whether the provider supports structured outputs in chat models.
268
- */
269
- supportsStructuredOutputs?: boolean;
270
- /**
271
- * Optional function to transform the request body before sending it to the API.
272
- * This is useful for proxy providers that may require a different request format
273
- * than the official OpenAI API.
274
- */
275
- transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
276
- /**
277
- * Optional metadata extractor to capture provider-specific metadata from API responses.
278
- * This is useful for extracting non-standard fields, experimental features,
279
- * or provider-specific metrics from both streaming and non-streaming responses.
280
- */
281
- metadataExtractor?: MetadataExtractor;
282
- }
283
- /**
284
- * Create an OpenAICompatible provider instance.
285
- */
286
- declare function createOpenAICompatible<CHAT_MODEL_IDS extends string, COMPLETION_MODEL_IDS extends string, EMBEDDING_MODEL_IDS extends string, IMAGE_MODEL_IDS extends string>(options: OpenAICompatibleProviderSettings): OpenAICompatibleProvider<CHAT_MODEL_IDS, COMPLETION_MODEL_IDS, EMBEDDING_MODEL_IDS, IMAGE_MODEL_IDS>;
287
-
288
- declare const VERSION: string;
289
-
290
- export { type MetadataExtractor, OpenAICompatibleChatLanguageModel, type OpenAICompatibleChatModelId, OpenAICompatibleCompletionLanguageModel, type OpenAICompatibleCompletionModelId, type OpenAICompatibleLanguageModelCompletionOptions as OpenAICompatibleCompletionProviderOptions, OpenAICompatibleEmbeddingModel, type OpenAICompatibleEmbeddingModelId, type OpenAICompatibleEmbeddingModelOptions, type OpenAICompatibleEmbeddingModelOptions as OpenAICompatibleEmbeddingProviderOptions, type OpenAICompatibleErrorData, OpenAICompatibleImageModel, type OpenAICompatibleLanguageModelChatOptions, type OpenAICompatibleLanguageModelCompletionOptions, type OpenAICompatibleProvider, type OpenAICompatibleLanguageModelChatOptions as OpenAICompatibleProviderOptions, type OpenAICompatibleProviderSettings, type ProviderErrorStructure, VERSION, createOpenAICompatible };