@ai-sdk/openai-compatible 2.0.89 → 2.0.91

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.
@@ -82,8 +82,12 @@ export type OpenAICompatibleChatConfig = {
82
82
  * Optional function to transform the request body before sending it to the API.
83
83
  * This is useful for proxy providers that may require a different request format
84
84
  * than the official OpenAI API.
85
+ * The optional warnings array can be used to report request transformations.
85
86
  */
86
- transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
87
+ transformRequestBody?: (
88
+ args: Record<string, any>,
89
+ warnings?: SharedV3Warning[],
90
+ ) => Record<string, any>;
87
91
 
88
92
  /**
89
93
  * Optional usage converter for OpenAI-compatible providers with different
@@ -134,8 +138,11 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
134
138
  return this.config.supportedUrls?.() ?? {};
135
139
  }
136
140
 
137
- private transformRequestBody(args: Record<string, any>): Record<string, any> {
138
- return this.config.transformRequestBody?.(args) ?? args;
141
+ private transformRequestBody(
142
+ args: Record<string, any>,
143
+ warnings: SharedV3Warning[],
144
+ ): Record<string, any> {
145
+ return this.config.transformRequestBody?.(args, warnings) ?? args;
139
146
  }
140
147
 
141
148
  private convertUsage(
@@ -298,7 +305,7 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
298
305
  ): Promise<LanguageModelV3GenerateResult> {
299
306
  const { args, warnings, metadataKey } = await this.getArgs({ ...options });
300
307
 
301
- const transformedBody = this.transformRequestBody(args);
308
+ const transformedBody = this.transformRequestBody(args, warnings);
302
309
  const body = JSON.stringify(transformedBody);
303
310
 
304
311
  const {
@@ -342,6 +349,16 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
342
349
 
343
350
  content.push(...convertOpenAICompatibleContent(choice.message.content));
344
351
 
352
+ // When the model refuses to answer, the API returns the refusal in
353
+ // `refusal` and sets `content` to null, so surface it as text instead of
354
+ // dropping it. Content and refusal are both returned when the model
355
+ // refuses after answering, so keep both:
356
+ const refusal = choice.message.refusal;
357
+ const hasRefusal = refusal != null && refusal.length > 0;
358
+ if (hasRefusal) {
359
+ content.push({ type: 'text', text: refusal });
360
+ }
361
+
345
362
  // tool calls:
346
363
  if (choice.message.tool_calls != null) {
347
364
  for (const toolCall of choice.message.tool_calls) {
@@ -384,7 +401,9 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
384
401
  return {
385
402
  content,
386
403
  finishReason: {
387
- unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
404
+ unified: mapOpenAICompatibleFinishReason(choice.finish_reason, {
405
+ hasRefusal,
406
+ }),
388
407
  raw: choice.finish_reason ?? undefined,
389
408
  },
390
409
  usage: this.convertUsage(responseBody.usage),
@@ -404,15 +423,18 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
404
423
  ): Promise<LanguageModelV3StreamResult> {
405
424
  const { args, warnings, metadataKey } = await this.getArgs({ ...options });
406
425
 
407
- const body = this.transformRequestBody({
408
- ...args,
409
- stream: true,
426
+ const body = this.transformRequestBody(
427
+ {
428
+ ...args,
429
+ stream: true,
410
430
 
411
- // only include stream_options when in strict compatibility mode:
412
- stream_options: this.config.includeUsage
413
- ? { include_usage: true }
414
- : undefined,
415
- });
431
+ // only include stream_options when in strict compatibility mode:
432
+ stream_options: this.config.includeUsage
433
+ ? { include_usage: true }
434
+ : undefined,
435
+ },
436
+ warnings,
437
+ );
416
438
 
417
439
  const metadataExtractor =
418
440
  this.config.metadataExtractor?.createStreamExtractor();
@@ -493,12 +515,14 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
493
515
  };
494
516
 
495
517
  let finishReason: LanguageModelV3FinishReason | undefined;
518
+ let rawFinishReason: string | undefined = undefined;
496
519
  let usage:
497
520
  | z.infer<ReturnType<typeof openaiCompatibleTokenUsageSchema>>
498
521
  | undefined = undefined;
499
522
  let isFirstChunk = true;
500
523
  let isActiveReasoning = false;
501
524
  let isActiveText = false;
525
+ let hasRefusal = false;
502
526
  const convertUsage = (
503
527
  usage: z.infer<ReturnType<typeof openaiCompatibleTokenUsageSchema>>,
504
528
  ) => this.convertUsage(usage);
@@ -575,11 +599,13 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
575
599
 
576
600
  const choice = value.choices[0];
577
601
 
602
+ // store the raw finish reason and map it in `flush`, after all
603
+ // chunks have been read: many OpenAI-compatible servers send the
604
+ // last delta (which can contain a refusal) in the same chunk as
605
+ // the finish reason, and a refusal turns `stop` into
606
+ // `content-filter`:
578
607
  if (choice?.finish_reason != null) {
579
- finishReason = {
580
- unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
581
- raw: choice.finish_reason ?? undefined,
582
- };
608
+ rawFinishReason = choice.finish_reason;
583
609
  }
584
610
 
585
611
  if (choice?.delta == null) {
@@ -646,6 +672,16 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
646
672
  }
647
673
  }
648
674
 
675
+ // When the model refuses to answer, the API streams the refusal in
676
+ // `refusal` instead of `content`. Content and refusal deltas can
677
+ // both occur, so append each to the same text part instead of
678
+ // dropping one:
679
+ const refusal = delta.refusal;
680
+ if (refusal != null && refusal.length > 0) {
681
+ hasRefusal = true;
682
+ enqueueTextDelta(refusal);
683
+ }
684
+
649
685
  if (delta.tool_calls != null && delta.tool_calls.length > 0) {
650
686
  // end active reasoning block before tool calls start
651
687
  if (isActiveReasoning) {
@@ -684,6 +720,18 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV3 {
684
720
 
685
721
  toolCallTracker.flush();
686
722
 
723
+ // map the stored finish reason now that all deltas (including
724
+ // refusals that arrived in the same chunk as the finish reason)
725
+ // have been read:
726
+ if (rawFinishReason != null) {
727
+ finishReason = {
728
+ unified: mapOpenAICompatibleFinishReason(rawFinishReason, {
729
+ hasRefusal,
730
+ }),
731
+ raw: rawFinishReason,
732
+ };
733
+ }
734
+
687
735
  if (finishReason == null) {
688
736
  finishReason = { unified: 'error', raw: undefined };
689
737
  controller.enqueue({
@@ -819,6 +867,15 @@ const OpenAICompatibleChatResponseSchema = lazySchema(() =>
819
867
  message: z.object({
820
868
  role: z.literal('assistant').nullish(),
821
869
  content: openAICompatibleContentSchema(),
870
+ // set instead of `content` when the model refuses to answer. Some
871
+ // OpenAI-compatible servers send non-string values (e.g. `false`),
872
+ // which are ignored instead of failing the whole response:
873
+ refusal: z
874
+ .unknown()
875
+ .nullish()
876
+ .transform(value =>
877
+ typeof value === 'string' ? value : undefined,
878
+ ),
822
879
  reasoning_content: z.string().nullish(),
823
880
  reasoning: z.string().nullish(),
824
881
  tool_calls: z
@@ -862,6 +919,15 @@ const chunkBaseSchema = () =>
862
919
  .object({
863
920
  role: z.enum(['assistant', '']).nullish(),
864
921
  content: openAICompatibleContentSchema(),
922
+ // set instead of `content` when the model refuses to answer. Some
923
+ // OpenAI-compatible servers send non-string values (e.g. `false`),
924
+ // which are ignored instead of failing the whole response:
925
+ refusal: z
926
+ .unknown()
927
+ .nullish()
928
+ .transform(value =>
929
+ typeof value === 'string' ? value : undefined,
930
+ ),
865
931
  // Most openai-compatible models set `reasoning_content`, but some
866
932
  // providers serving `gpt-oss` set `reasoning`. See #7866
867
933
  reasoning_content: z.string().nullish(),