@ai-sdk/huggingface 1.0.88 → 1.0.90

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.
@@ -10,6 +10,9 @@ import {
10
10
  type SharedV3Warning,
11
11
  } from '@ai-sdk/provider';
12
12
  import {
13
+ lazySchema,
14
+ zodSchema,
15
+ type InferSchema,
13
16
  combineHeaders,
14
17
  createEventSourceResponseHandler,
15
18
  createJsonResponseHandler,
@@ -174,7 +177,7 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
174
177
  body,
175
178
  failedResponseHandler: huggingfaceFailedResponseHandler,
176
179
  successfulResponseHandler: createJsonResponseHandler(
177
- huggingfaceResponsesResponseSchema,
180
+ huggingfaceResponsesResponseSchemaValidator,
178
181
  ),
179
182
  abortSignal: options.abortSignal,
180
183
  fetch: this.config.fetch,
@@ -365,7 +368,7 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
365
368
  return {
366
369
  stream: response.pipeThrough(
367
370
  new TransformStream<
368
- ParseResult<z.infer<typeof huggingfaceResponsesChunkSchema>>,
371
+ ParseResult<InferSchema<typeof huggingfaceResponsesChunkSchema>>,
369
372
  LanguageModelV3StreamPart
370
373
  >({
371
374
  start(controller) {
@@ -523,149 +526,52 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
523
526
  }
524
527
  }
525
528
 
526
- const huggingfaceResponsesProviderOptionsSchema = z.object({
527
- metadata: z.record(z.string(), z.string()).optional(),
528
- instructions: z.string().optional(),
529
- strictJsonSchema: z.boolean().optional(),
530
- reasoningEffort: z.string().optional(),
531
- });
529
+ const huggingfaceResponsesProviderOptionsSchema = lazySchema(() =>
530
+ zodSchema(
531
+ z.object({
532
+ metadata: z.record(z.string(), z.string()).optional(),
533
+ instructions: z.string().optional(),
534
+ strictJsonSchema: z.boolean().optional(),
535
+ reasoningEffort: z.string().optional(),
536
+ }),
537
+ ),
538
+ );
532
539
 
533
- const huggingfaceResponsesOutputSchema = z.discriminatedUnion('type', [
534
- z.object({
535
- type: z.literal('message'),
536
- id: z.string(),
537
- role: z.string().optional(),
538
- status: z.string().optional(),
539
- content: z.array(
540
- z.object({
541
- type: z.literal('output_text'),
542
- text: z.string(),
543
- annotations: z.array(z.any()).optional(),
544
- }),
545
- ),
546
- }),
547
- z.object({
548
- type: z.literal('reasoning'),
549
- id: z.string(),
550
- status: z.string().optional(),
551
- content: z.array(
552
- z.object({
553
- type: z.literal('reasoning_text'),
554
- text: z.string(),
555
- }),
556
- ),
557
- summary: z
558
- .array(
559
- z
560
- .object({
561
- type: z.literal('reasoning_summary'),
562
- text: z.string(),
563
- })
564
- .optional(),
565
- )
566
- .optional(),
567
- }),
568
- z.object({
569
- type: z.literal('function_call'),
570
- id: z.string(),
571
- call_id: z.string(),
572
- name: z.string(),
573
- arguments: z.string(),
574
- output: z.string().optional(),
575
- status: z.string().optional(),
576
- }),
577
- z.object({
578
- type: z.literal('mcp_call'),
579
- id: z.string(),
580
- name: z.string(),
581
- arguments: z.string(),
582
- output: z.string().optional(),
583
- status: z.string().optional(),
584
- }),
585
- z.object({
586
- type: z.literal('mcp_list_tools'),
587
- id: z.string(),
588
- server_label: z.string(),
589
- tools: z.array(z.any()).optional(),
590
- status: z.string().optional(),
591
- }),
592
- ]);
593
-
594
- const huggingfaceResponsesResponseSchema = z.object({
595
- id: z.string(),
596
- model: z.string(),
597
- object: z.string(),
598
- created_at: z.number(),
599
- status: z.string(),
600
- error: z.any().nullable(),
601
- instructions: z.any().nullable(),
602
- max_output_tokens: z.any().nullable(),
603
- metadata: z.any().nullable(),
604
- tool_choice: z.any(),
605
- tools: z.array(z.any()),
606
- temperature: z.number(),
607
- top_p: z.number(),
608
- incomplete_details: z
609
- .object({
610
- reason: z.string(),
611
- })
612
- .nullable()
613
- .optional(),
614
- usage: z
615
- .object({
616
- input_tokens: z.number(),
617
- input_tokens_details: z
618
- .object({
619
- cached_tokens: z.number(),
620
- })
621
- .optional(),
622
- output_tokens: z.number(),
623
- output_tokens_details: z
624
- .object({
625
- reasoning_tokens: z.number(),
626
- })
627
- .optional(),
628
- total_tokens: z.number(),
629
- })
630
- .nullable()
631
- .optional(),
632
- output: z.array(huggingfaceResponsesOutputSchema),
633
- output_text: z.string().nullable().optional(),
634
- });
635
-
636
- const responseOutputItemAddedSchema = z.object({
637
- type: z.literal('response.output_item.added'),
638
- output_index: z.number(),
639
- item: z.discriminatedUnion('type', [
540
+ const huggingfaceResponsesOutputSchema = () =>
541
+ z.discriminatedUnion('type', [
640
542
  z.object({
641
543
  type: z.literal('message'),
642
544
  id: z.string(),
643
545
  role: z.string().optional(),
644
546
  status: z.string().optional(),
645
- content: z.array(z.any()).optional(),
547
+ content: z.array(
548
+ z.object({
549
+ type: z.literal('output_text'),
550
+ text: z.string(),
551
+ annotations: z.array(z.any()).optional(),
552
+ }),
553
+ ),
646
554
  }),
647
555
  z.object({
648
556
  type: z.literal('reasoning'),
649
557
  id: z.string(),
650
558
  status: z.string().optional(),
651
- content: z.array(z.any()).optional(),
652
- summary: z.array(z.any()).optional(),
653
- }),
654
- z.object({
655
- type: z.literal('mcp_list_tools'),
656
- id: z.string(),
657
- server_label: z.string(),
658
- tools: z.array(z.any()).optional(),
659
- error: z.string().optional(),
660
- }),
661
- z.object({
662
- type: z.literal('mcp_call'),
663
- id: z.string(),
664
- server_label: z.string(),
665
- name: z.string(),
666
- arguments: z.string(),
667
- output: z.string().optional(),
668
- error: z.string().optional(),
559
+ content: z.array(
560
+ z.object({
561
+ type: z.literal('reasoning_text'),
562
+ text: z.string(),
563
+ }),
564
+ ),
565
+ summary: z
566
+ .array(
567
+ z
568
+ .object({
569
+ type: z.literal('reasoning_summary'),
570
+ text: z.string(),
571
+ })
572
+ .optional(),
573
+ )
574
+ .optional(),
669
575
  }),
670
576
  z.object({
671
577
  type: z.literal('function_call'),
@@ -674,152 +580,269 @@ const responseOutputItemAddedSchema = z.object({
674
580
  name: z.string(),
675
581
  arguments: z.string(),
676
582
  output: z.string().optional(),
677
- error: z.string().optional(),
678
- }),
679
- ]),
680
- sequence_number: z.number(),
681
- });
682
-
683
- const responseOutputItemDoneSchema = z.object({
684
- type: z.literal('response.output_item.done'),
685
- output_index: z.number(),
686
- item: z.discriminatedUnion('type', [
687
- z.object({
688
- type: z.literal('message'),
689
- id: z.string(),
690
- role: z.string().optional(),
691
583
  status: z.string().optional(),
692
- content: z.array(z.any()).optional(),
693
- }),
694
- z.object({
695
- type: z.literal('mcp_list_tools'),
696
- id: z.string(),
697
- server_label: z.string(),
698
- tools: z.array(z.any()).optional(),
699
- error: z.string().optional(),
700
584
  }),
701
585
  z.object({
702
586
  type: z.literal('mcp_call'),
703
587
  id: z.string(),
704
- server_label: z.string(),
705
- name: z.string(),
706
- arguments: z.string(),
707
- output: z.string().optional(),
708
- error: z.string().optional(),
709
- }),
710
- z.object({
711
- type: z.literal('function_call'),
712
- id: z.string(),
713
- call_id: z.string(),
714
588
  name: z.string(),
715
589
  arguments: z.string(),
716
590
  output: z.string().optional(),
717
- error: z.string().optional(),
591
+ status: z.string().optional(),
718
592
  }),
719
593
  z.object({
720
- type: z.literal('reasoning'),
594
+ type: z.literal('mcp_list_tools'),
721
595
  id: z.string(),
596
+ server_label: z.string(),
597
+ tools: z.array(z.any()).optional(),
722
598
  status: z.string().optional(),
723
- content: z.array(z.any()).optional(),
724
- summary: z.array(z.any()).optional(),
725
599
  }),
726
- ]),
727
- sequence_number: z.number(),
728
- });
729
-
730
- const textDeltaChunkSchema = z.object({
731
- type: z.literal('response.output_text.delta'),
732
- item_id: z.string(),
733
- output_index: z.number(),
734
- content_index: z.number(),
735
- delta: z.string(),
736
- sequence_number: z.number(),
737
- });
738
-
739
- const reasoningTextDeltaChunkSchema = z.object({
740
- type: z.literal('response.reasoning_text.delta'),
741
- item_id: z.string(),
742
- output_index: z.number(),
743
- content_index: z.number(),
744
- delta: z.string(),
745
- sequence_number: z.number(),
746
- });
747
-
748
- const reasoningTextEndChunkSchema = z.object({
749
- type: z.literal('response.reasoning_text.done'),
750
- item_id: z.string(),
751
- output_index: z.number(),
752
- content_index: z.number(),
753
- text: z.string(),
754
- sequence_number: z.number(),
755
- });
756
-
757
- const responseCompletedChunkSchema = z.object({
758
- type: z.literal('response.completed'),
759
- response: huggingfaceResponsesResponseSchema,
760
- sequence_number: z.number(),
761
- });
762
-
763
- const responseCreatedChunkSchema = z.object({
764
- type: z.literal('response.created'),
765
- response: z.object({
600
+ ]);
601
+
602
+ const huggingfaceResponsesResponseSchema = () =>
603
+ z.object({
766
604
  id: z.string(),
605
+ model: z.string(),
767
606
  object: z.string(),
768
607
  created_at: z.number(),
769
608
  status: z.string(),
770
- model: z.string(),
771
- }),
772
- });
773
-
774
- const huggingfaceResponsesChunkSchema = z.union([
775
- responseOutputItemAddedSchema,
776
- responseOutputItemDoneSchema,
777
- reasoningTextDeltaChunkSchema,
778
- reasoningTextEndChunkSchema,
779
- textDeltaChunkSchema,
780
- responseCompletedChunkSchema,
781
- responseCreatedChunkSchema,
782
- z.object({ type: z.string() }).loose(), // fallback for unknown chunks
783
- ]);
609
+ error: z.any().nullable(),
610
+ instructions: z.any().nullable(),
611
+ max_output_tokens: z.any().nullable(),
612
+ metadata: z.any().nullable(),
613
+ tool_choice: z.any(),
614
+ tools: z.array(z.any()),
615
+ temperature: z.number(),
616
+ top_p: z.number(),
617
+ incomplete_details: z
618
+ .object({
619
+ reason: z.string(),
620
+ })
621
+ .nullable()
622
+ .optional(),
623
+ usage: z
624
+ .object({
625
+ input_tokens: z.number(),
626
+ input_tokens_details: z
627
+ .object({
628
+ cached_tokens: z.number(),
629
+ })
630
+ .optional(),
631
+ output_tokens: z.number(),
632
+ output_tokens_details: z
633
+ .object({
634
+ reasoning_tokens: z.number(),
635
+ })
636
+ .optional(),
637
+ total_tokens: z.number(),
638
+ })
639
+ .nullable()
640
+ .optional(),
641
+ output: z.array(huggingfaceResponsesOutputSchema()),
642
+ output_text: z.string().nullable().optional(),
643
+ });
644
+ const huggingfaceResponsesResponseSchemaValidator = lazySchema(() =>
645
+ zodSchema(huggingfaceResponsesResponseSchema()),
646
+ );
647
+
648
+ const responseOutputItemAddedSchema = () =>
649
+ z.object({
650
+ type: z.literal('response.output_item.added'),
651
+ output_index: z.number(),
652
+ item: z.discriminatedUnion('type', [
653
+ z.object({
654
+ type: z.literal('message'),
655
+ id: z.string(),
656
+ role: z.string().optional(),
657
+ status: z.string().optional(),
658
+ content: z.array(z.any()).optional(),
659
+ }),
660
+ z.object({
661
+ type: z.literal('reasoning'),
662
+ id: z.string(),
663
+ status: z.string().optional(),
664
+ content: z.array(z.any()).optional(),
665
+ summary: z.array(z.any()).optional(),
666
+ }),
667
+ z.object({
668
+ type: z.literal('mcp_list_tools'),
669
+ id: z.string(),
670
+ server_label: z.string(),
671
+ tools: z.array(z.any()).optional(),
672
+ error: z.string().optional(),
673
+ }),
674
+ z.object({
675
+ type: z.literal('mcp_call'),
676
+ id: z.string(),
677
+ server_label: z.string(),
678
+ name: z.string(),
679
+ arguments: z.string(),
680
+ output: z.string().optional(),
681
+ error: z.string().optional(),
682
+ }),
683
+ z.object({
684
+ type: z.literal('function_call'),
685
+ id: z.string(),
686
+ call_id: z.string(),
687
+ name: z.string(),
688
+ arguments: z.string(),
689
+ output: z.string().optional(),
690
+ error: z.string().optional(),
691
+ }),
692
+ ]),
693
+ sequence_number: z.number(),
694
+ });
695
+
696
+ const responseOutputItemDoneSchema = () =>
697
+ z.object({
698
+ type: z.literal('response.output_item.done'),
699
+ output_index: z.number(),
700
+ item: z.discriminatedUnion('type', [
701
+ z.object({
702
+ type: z.literal('message'),
703
+ id: z.string(),
704
+ role: z.string().optional(),
705
+ status: z.string().optional(),
706
+ content: z.array(z.any()).optional(),
707
+ }),
708
+ z.object({
709
+ type: z.literal('mcp_list_tools'),
710
+ id: z.string(),
711
+ server_label: z.string(),
712
+ tools: z.array(z.any()).optional(),
713
+ error: z.string().optional(),
714
+ }),
715
+ z.object({
716
+ type: z.literal('mcp_call'),
717
+ id: z.string(),
718
+ server_label: z.string(),
719
+ name: z.string(),
720
+ arguments: z.string(),
721
+ output: z.string().optional(),
722
+ error: z.string().optional(),
723
+ }),
724
+ z.object({
725
+ type: z.literal('function_call'),
726
+ id: z.string(),
727
+ call_id: z.string(),
728
+ name: z.string(),
729
+ arguments: z.string(),
730
+ output: z.string().optional(),
731
+ error: z.string().optional(),
732
+ }),
733
+ z.object({
734
+ type: z.literal('reasoning'),
735
+ id: z.string(),
736
+ status: z.string().optional(),
737
+ content: z.array(z.any()).optional(),
738
+ summary: z.array(z.any()).optional(),
739
+ }),
740
+ ]),
741
+ sequence_number: z.number(),
742
+ });
743
+
744
+ const textDeltaChunkSchema = () =>
745
+ z.object({
746
+ type: z.literal('response.output_text.delta'),
747
+ item_id: z.string(),
748
+ output_index: z.number(),
749
+ content_index: z.number(),
750
+ delta: z.string(),
751
+ sequence_number: z.number(),
752
+ });
753
+
754
+ const reasoningTextDeltaChunkSchema = () =>
755
+ z.object({
756
+ type: z.literal('response.reasoning_text.delta'),
757
+ item_id: z.string(),
758
+ output_index: z.number(),
759
+ content_index: z.number(),
760
+ delta: z.string(),
761
+ sequence_number: z.number(),
762
+ });
763
+
764
+ const reasoningTextEndChunkSchema = () =>
765
+ z.object({
766
+ type: z.literal('response.reasoning_text.done'),
767
+ item_id: z.string(),
768
+ output_index: z.number(),
769
+ content_index: z.number(),
770
+ text: z.string(),
771
+ sequence_number: z.number(),
772
+ });
773
+
774
+ const responseCompletedChunkSchema = () =>
775
+ z.object({
776
+ type: z.literal('response.completed'),
777
+ response: huggingfaceResponsesResponseSchema(),
778
+ sequence_number: z.number(),
779
+ });
780
+
781
+ const responseCreatedChunkSchema = () =>
782
+ z.object({
783
+ type: z.literal('response.created'),
784
+ response: z.object({
785
+ id: z.string(),
786
+ object: z.string(),
787
+ created_at: z.number(),
788
+ status: z.string(),
789
+ model: z.string(),
790
+ }),
791
+ });
792
+
793
+ const huggingfaceResponsesChunkSchema = lazySchema(() =>
794
+ zodSchema(
795
+ z.union([
796
+ responseOutputItemAddedSchema(),
797
+ responseOutputItemDoneSchema(),
798
+ reasoningTextDeltaChunkSchema(),
799
+ reasoningTextEndChunkSchema(),
800
+ textDeltaChunkSchema(),
801
+ responseCompletedChunkSchema(),
802
+ responseCreatedChunkSchema(),
803
+ z.object({ type: z.string() }).loose(), // fallback for unknown chunks
804
+ ]),
805
+ ),
806
+ );
784
807
 
785
808
  function isResponseOutputItemAddedChunk(
786
- chunk: z.infer<typeof huggingfaceResponsesChunkSchema>,
787
- ): chunk is z.infer<typeof responseOutputItemAddedSchema> {
809
+ chunk: InferSchema<typeof huggingfaceResponsesChunkSchema>,
810
+ ): chunk is z.infer<ReturnType<typeof responseOutputItemAddedSchema>> {
788
811
  return chunk.type === 'response.output_item.added';
789
812
  }
790
813
 
791
814
  function isResponseOutputItemDoneChunk(
792
- chunk: z.infer<typeof huggingfaceResponsesChunkSchema>,
793
- ): chunk is z.infer<typeof responseOutputItemDoneSchema> {
815
+ chunk: InferSchema<typeof huggingfaceResponsesChunkSchema>,
816
+ ): chunk is z.infer<ReturnType<typeof responseOutputItemDoneSchema>> {
794
817
  return chunk.type === 'response.output_item.done';
795
818
  }
796
819
 
797
820
  function isTextDeltaChunk(
798
- chunk: z.infer<typeof huggingfaceResponsesChunkSchema>,
799
- ): chunk is z.infer<typeof textDeltaChunkSchema> {
821
+ chunk: InferSchema<typeof huggingfaceResponsesChunkSchema>,
822
+ ): chunk is z.infer<ReturnType<typeof textDeltaChunkSchema>> {
800
823
  return chunk.type === 'response.output_text.delta';
801
824
  }
802
825
 
803
826
  function isReasoningDeltaChunk(
804
- chunk: z.infer<typeof huggingfaceResponsesChunkSchema>,
805
- ): chunk is z.infer<typeof reasoningTextDeltaChunkSchema> {
827
+ chunk: InferSchema<typeof huggingfaceResponsesChunkSchema>,
828
+ ): chunk is z.infer<ReturnType<typeof reasoningTextDeltaChunkSchema>> {
806
829
  return chunk.type === 'response.reasoning_text.delta';
807
830
  }
808
831
 
809
832
  function isReasoningEndChunk(
810
- chunk: z.infer<typeof huggingfaceResponsesChunkSchema>,
811
- ): chunk is z.infer<typeof reasoningTextEndChunkSchema> {
833
+ chunk: InferSchema<typeof huggingfaceResponsesChunkSchema>,
834
+ ): chunk is z.infer<ReturnType<typeof reasoningTextEndChunkSchema>> {
812
835
  return chunk.type === 'response.reasoning_text.done';
813
836
  }
814
837
 
815
838
  function isResponseCompletedChunk(
816
- chunk: z.infer<typeof huggingfaceResponsesChunkSchema>,
817
- ): chunk is z.infer<typeof responseCompletedChunkSchema> {
839
+ chunk: InferSchema<typeof huggingfaceResponsesChunkSchema>,
840
+ ): chunk is z.infer<ReturnType<typeof responseCompletedChunkSchema>> {
818
841
  return chunk.type === 'response.completed';
819
842
  }
820
843
 
821
844
  function isResponseCreatedChunk(
822
- chunk: z.infer<typeof huggingfaceResponsesChunkSchema>,
823
- ): chunk is z.infer<typeof responseCreatedChunkSchema> {
845
+ chunk: InferSchema<typeof huggingfaceResponsesChunkSchema>,
846
+ ): chunk is z.infer<ReturnType<typeof responseCreatedChunkSchema>> {
824
847
  return chunk.type === 'response.created';
825
848
  }
@@ -12,6 +12,7 @@ export type HuggingFaceResponsesTool = {
12
12
 
13
13
  export type HuggingFaceResponsesToolChoice =
14
14
  | 'auto'
15
+ | 'none'
15
16
  | 'required'
16
17
  | { type: 'function'; function: { name: string } };
17
18
 
@@ -71,7 +72,7 @@ export function prepareResponsesTools({
71
72
  mappedToolChoice = 'required';
72
73
  break;
73
74
  case 'none':
74
- // not supported, ignore
75
+ mappedToolChoice = 'none';
75
76
  break;
76
77
  case 'tool':
77
78
  mappedToolChoice = {