@ai-sdk/provider 1.1.3 → 2.0.0-alpha.1

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.
package/dist/index.d.mts CHANGED
@@ -1,11 +1,69 @@
1
1
  import { JSONSchema7 } from 'json-schema';
2
2
  export { JSONSchema7, JSONSchema7Definition } from 'json-schema';
3
3
 
4
+ type SharedV2Headers = Record<string, string>;
5
+
6
+ /**
7
+ A JSON value can be a string, number, boolean, object, array, or null.
8
+ JSON values can be serialized and deserialized by the JSON.stringify and JSON.parse methods.
9
+ */
10
+ type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
11
+ type JSONObject = {
12
+ [key: string]: JSONValue;
13
+ };
14
+ type JSONArray = JSONValue[];
15
+
16
+ /**
17
+ * Additional provider-specific metadata.
18
+ * Metadata are additional outputs from the provider.
19
+ * They are passed through to the provider from the AI SDK
20
+ * and enable provider-specific functionality
21
+ * that can be fully encapsulated in the provider.
22
+ *
23
+ * This enables us to quickly ship provider-specific functionality
24
+ * without affecting the core AI SDK.
25
+ *
26
+ * The outer record is keyed by the provider name, and the inner
27
+ * record is keyed by the provider-specific metadata key.
28
+ *
29
+ * ```ts
30
+ * {
31
+ * "anthropic": {
32
+ * "cacheControl": { "type": "ephemeral" }
33
+ * }
34
+ * }
35
+ * ```
36
+ */
37
+ type SharedV2ProviderMetadata = Record<string, Record<string, JSONValue>>;
38
+
39
+ /**
40
+ * Additional provider-specific options.
41
+ * Options are additional input to the provider.
42
+ * They are passed through to the provider from the AI SDK
43
+ * and enable provider-specific functionality
44
+ * that can be fully encapsulated in the provider.
45
+ *
46
+ * This enables us to quickly ship provider-specific functionality
47
+ * without affecting the core AI SDK.
48
+ *
49
+ * The outer record is keyed by the provider name, and the inner
50
+ * record is keyed by the provider-specific metadata key.
51
+ *
52
+ * ```ts
53
+ * {
54
+ * "anthropic": {
55
+ * "cacheControl": { "type": "ephemeral" }
56
+ * }
57
+ * }
58
+ * ```
59
+ */
60
+ type SharedV2ProviderOptions = Record<string, Record<string, JSONValue>>;
61
+
4
62
  /**
5
63
  An embedding is a vector, i.e. an array of numbers.
6
64
  It is e.g. used to represent a text as a vector of word embeddings.
7
65
  */
8
- type EmbeddingModelV1Embedding = Array<number>;
66
+ type EmbeddingModelV2Embedding = Array<number>;
9
67
 
10
68
  /**
11
69
  Specification for an embedding model that implements the embedding model
@@ -15,7 +73,7 @@ VALUE is the type of the values that the model can embed.
15
73
  This will allow us to go beyond text embeddings in the future,
16
74
  e.g. to support image embeddings
17
75
  */
18
- type EmbeddingModelV1<VALUE> = {
76
+ type EmbeddingModelV2<VALUE> = {
19
77
  /**
20
78
  The embedding model must specify which embedding model interface
21
79
  version it implements. This will allow us to evolve the embedding
@@ -23,7 +81,7 @@ type EmbeddingModelV1<VALUE> = {
23
81
  implementation versions can be handled as a discriminated union
24
82
  on our side.
25
83
  */
26
- readonly specificationVersion: 'v1';
84
+ readonly specificationVersion: 'v2';
27
85
  /**
28
86
  Name of the provider for logging purposes.
29
87
  */
@@ -34,12 +92,14 @@ type EmbeddingModelV1<VALUE> = {
34
92
  readonly modelId: string;
35
93
  /**
36
94
  Limit of how many embeddings can be generated in a single API call.
95
+
96
+ Use Infinity for models that do not have a limit.
37
97
  */
38
- readonly maxEmbeddingsPerCall: number | undefined;
98
+ readonly maxEmbeddingsPerCall: PromiseLike<number | undefined> | number | undefined;
39
99
  /**
40
100
  True if the model can handle multiple embedding calls in parallel.
41
101
  */
42
- readonly supportsParallelCalls: boolean;
102
+ readonly supportsParallelCalls: PromiseLike<boolean> | boolean;
43
103
  /**
44
104
  Generates a list of embeddings for the given input text.
45
105
 
@@ -56,6 +116,12 @@ type EmbeddingModelV1<VALUE> = {
56
116
  */
57
117
  abortSignal?: AbortSignal;
58
118
  /**
119
+ Additional provider-specific options. They are passed through
120
+ to the provider from the AI SDK and enable provider-specific
121
+ functionality that can be fully encapsulated in the provider.
122
+ */
123
+ providerOptions?: SharedV2ProviderOptions;
124
+ /**
59
125
  Additional HTTP headers to be sent with the request.
60
126
  Only applicable for HTTP-based providers.
61
127
  */
@@ -64,7 +130,7 @@ type EmbeddingModelV1<VALUE> = {
64
130
  /**
65
131
  Generated embeddings. They are in the same order as the input values.
66
132
  */
67
- embeddings: Array<EmbeddingModelV1Embedding>;
133
+ embeddings: Array<EmbeddingModelV2Embedding>;
68
134
  /**
69
135
  Token usage. We only have input tokens for embeddings.
70
136
  */
@@ -72,13 +138,17 @@ type EmbeddingModelV1<VALUE> = {
72
138
  tokens: number;
73
139
  };
74
140
  /**
75
- Optional raw response information for debugging purposes.
141
+ Optional response information for debugging purposes.
76
142
  */
77
- rawResponse?: {
143
+ response?: {
78
144
  /**
79
145
  Response headers.
80
146
  */
81
- headers?: Record<string, string>;
147
+ headers?: SharedV2Headers;
148
+ /**
149
+ The response body.
150
+ */
151
+ body?: unknown;
82
152
  };
83
153
  }>;
84
154
  };
@@ -305,13 +375,11 @@ declare class UnsupportedFunctionalityError extends AISDKError {
305
375
  static isInstance(error: unknown): error is UnsupportedFunctionalityError;
306
376
  }
307
377
 
308
- type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
309
- type JSONObject = {
310
- [key: string]: JSONValue;
311
- };
312
- type JSONArray = JSONValue[];
378
+ declare function isJSONValue(value: unknown): value is JSONValue;
379
+ declare function isJSONArray(value: unknown): value is JSONArray;
380
+ declare function isJSONObject(value: unknown): value is JSONObject;
313
381
 
314
- type ImageModelV1CallOptions = {
382
+ type ImageModelV2CallOptions = {
315
383
  /**
316
384
  Prompt for the image generation.
317
385
  */
@@ -345,13 +413,435 @@ type ImageModelV1CallOptions = {
345
413
  record is keyed by the provider-specific metadata key.
346
414
  ```ts
347
415
  {
348
- "openai": {
349
- "style": "vivid"
350
- }
416
+ "openai": {
417
+ "style": "vivid"
418
+ }
351
419
  }
352
420
  ```
353
421
  */
354
- providerOptions: Record<string, Record<string, JSONValue>>;
422
+ providerOptions: SharedV2ProviderOptions;
423
+ /**
424
+ Abort signal for cancelling the operation.
425
+ */
426
+ abortSignal?: AbortSignal;
427
+ /**
428
+ Additional HTTP headers to be sent with the request.
429
+ Only applicable for HTTP-based providers.
430
+ */
431
+ headers?: Record<string, string | undefined>;
432
+ };
433
+
434
+ /**
435
+ Warning from the model provider for this call. The call will proceed, but e.g.
436
+ some settings might not be supported, which can lead to suboptimal results.
437
+ */
438
+ type ImageModelV2CallWarning = {
439
+ type: 'unsupported-setting';
440
+ setting: keyof ImageModelV2CallOptions;
441
+ details?: string;
442
+ } | {
443
+ type: 'other';
444
+ message: string;
445
+ };
446
+
447
+ type ImageModelV2ProviderMetadata = Record<string, {
448
+ images: JSONArray;
449
+ } & JSONValue>;
450
+ type GetMaxImagesPerCallFunction = (options: {
451
+ modelId: string;
452
+ }) => PromiseLike<number | undefined> | number | undefined;
453
+ /**
454
+ Image generation model specification version 2.
455
+ */
456
+ type ImageModelV2 = {
457
+ /**
458
+ The image model must specify which image model interface
459
+ version it implements. This will allow us to evolve the image
460
+ model interface and retain backwards compatibility. The different
461
+ implementation versions can be handled as a discriminated union
462
+ on our side.
463
+ */
464
+ readonly specificationVersion: 'v2';
465
+ /**
466
+ Name of the provider for logging purposes.
467
+ */
468
+ readonly provider: string;
469
+ /**
470
+ Provider-specific model ID for logging purposes.
471
+ */
472
+ readonly modelId: string;
473
+ /**
474
+ Limit of how many images can be generated in a single API call.
475
+ Can be set to a number for a fixed limit, to undefined to use
476
+ the global limit, or a function that returns a number or undefined,
477
+ optionally as a promise.
478
+ */
479
+ readonly maxImagesPerCall: number | undefined | GetMaxImagesPerCallFunction;
480
+ /**
481
+ Generates an array of images.
482
+ */
483
+ doGenerate(options: ImageModelV2CallOptions): PromiseLike<{
484
+ /**
485
+ Generated images as base64 encoded strings or binary data.
486
+ The images should be returned without any unnecessary conversion.
487
+ If the API returns base64 encoded strings, the images should be returned
488
+ as base64 encoded strings. If the API returns binary data, the images should
489
+ be returned as binary data.
490
+ */
491
+ images: Array<string> | Array<Uint8Array>;
492
+ /**
493
+ Warnings for the call, e.g. unsupported settings.
494
+ */
495
+ warnings: Array<ImageModelV2CallWarning>;
496
+ /**
497
+ Additional provider-specific metadata. They are passed through
498
+ from the provider to the AI SDK and enable provider-specific
499
+ results that can be fully encapsulated in the provider.
500
+
501
+ The outer record is keyed by the provider name, and the inner
502
+ record is provider-specific metadata. It always includes an
503
+ `images` key with image-specific metadata
504
+
505
+ ```ts
506
+ {
507
+ "openai": {
508
+ "images": ["revisedPrompt": "Revised prompt here."]
509
+ }
510
+ }
511
+ ```
512
+ */
513
+ providerMetadata?: ImageModelV2ProviderMetadata;
514
+ /**
515
+ Response information for telemetry and debugging purposes.
516
+ */
517
+ response: {
518
+ /**
519
+ Timestamp for the start of the generated response.
520
+ */
521
+ timestamp: Date;
522
+ /**
523
+ The ID of the response model that was used to generate the response.
524
+ */
525
+ modelId: string;
526
+ /**
527
+ Response headers.
528
+ */
529
+ headers: Record<string, string> | undefined;
530
+ };
531
+ }>;
532
+ };
533
+
534
+ /**
535
+ A tool has a name, a description, and a set of parameters.
536
+
537
+ Note: this is **not** the user-facing tool definition. The AI SDK methods will
538
+ map the user-facing tool definitions to this format.
539
+ */
540
+ type LanguageModelV2FunctionTool = {
541
+ /**
542
+ The type of the tool (always 'function').
543
+ */
544
+ type: 'function';
545
+ /**
546
+ The name of the tool. Unique within this model call.
547
+ */
548
+ name: string;
549
+ /**
550
+ A description of the tool. The language model uses this to understand the
551
+ tool's purpose and to provide better completion suggestions.
552
+ */
553
+ description?: string;
554
+ /**
555
+ The parameters that the tool expects. The language model uses this to
556
+ understand the tool's input requirements and to provide matching suggestions.
557
+ */
558
+ parameters: JSONSchema7;
559
+ };
560
+
561
+ /**
562
+ Data content. Can be a Uint8Array, base64 encoded data as a string or a URL.
563
+ */
564
+ type LanguageModelV2DataContent = Uint8Array | string | URL;
565
+
566
+ /**
567
+ A prompt is a list of messages.
568
+
569
+ Note: Not all models and prompt formats support multi-modal inputs and
570
+ tool calls. The validation happens at runtime.
571
+
572
+ Note: This is not a user-facing prompt. The AI SDK methods will map the
573
+ user-facing prompt types such as chat or instruction prompts to this format.
574
+ */
575
+ type LanguageModelV2Prompt = Array<LanguageModelV2Message>;
576
+ type LanguageModelV2Message = ({
577
+ role: 'system';
578
+ content: string;
579
+ } | {
580
+ role: 'user';
581
+ content: Array<LanguageModelV2TextPart | LanguageModelV2FilePart>;
582
+ } | {
583
+ role: 'assistant';
584
+ content: Array<LanguageModelV2TextPart | LanguageModelV2FilePart | LanguageModelV2ReasoningPart | LanguageModelV2ToolCallPart>;
585
+ } | {
586
+ role: 'tool';
587
+ content: Array<LanguageModelV2ToolResultPart>;
588
+ }) & {
589
+ /**
590
+ * Additional provider-specific options. They are passed through
591
+ * to the provider from the AI SDK and enable provider-specific
592
+ * functionality that can be fully encapsulated in the provider.
593
+ */
594
+ providerOptions?: SharedV2ProviderOptions;
595
+ };
596
+ /**
597
+ Text content part of a prompt. It contains a string of text.
598
+ */
599
+ interface LanguageModelV2TextPart {
600
+ type: 'text';
601
+ /**
602
+ The text content.
603
+ */
604
+ text: string;
605
+ /**
606
+ * Additional provider-specific options. They are passed through
607
+ * to the provider from the AI SDK and enable provider-specific
608
+ * functionality that can be fully encapsulated in the provider.
609
+ */
610
+ providerOptions?: SharedV2ProviderOptions;
611
+ }
612
+ /**
613
+ Reasoning content part of a prompt. It contains a string of reasoning text.
614
+ */
615
+ interface LanguageModelV2ReasoningPart {
616
+ type: 'reasoning';
617
+ /**
618
+ The reasoning text.
619
+ */
620
+ text: string;
621
+ /**
622
+ * Additional provider-specific options. They are passed through
623
+ * to the provider from the AI SDK and enable provider-specific
624
+ * functionality that can be fully encapsulated in the provider.
625
+ */
626
+ providerOptions?: SharedV2ProviderOptions;
627
+ }
628
+ /**
629
+ File content part of a prompt. It contains a file.
630
+ */
631
+ interface LanguageModelV2FilePart {
632
+ type: 'file';
633
+ /**
634
+ * Optional filename of the file.
635
+ */
636
+ filename?: string;
637
+ /**
638
+ File data. Can be a Uint8Array, base64 encoded data as a string or a URL.
639
+ */
640
+ data: LanguageModelV2DataContent;
641
+ /**
642
+ IANA media type of the file.
643
+
644
+ Can support wildcards, e.g. `image/*` (in which case the provider needs to take appropriate action).
645
+
646
+ @see https://www.iana.org/assignments/media-types/media-types.xhtml
647
+ */
648
+ mediaType: string;
649
+ /**
650
+ * Additional provider-specific options. They are passed through
651
+ * to the provider from the AI SDK and enable provider-specific
652
+ * functionality that can be fully encapsulated in the provider.
653
+ */
654
+ providerOptions?: SharedV2ProviderOptions;
655
+ }
656
+ /**
657
+ Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
658
+ */
659
+ interface LanguageModelV2ToolCallPart {
660
+ type: 'tool-call';
661
+ /**
662
+ ID of the tool call. This ID is used to match the tool call with the tool result.
663
+ */
664
+ toolCallId: string;
665
+ /**
666
+ Name of the tool that is being called.
667
+ */
668
+ toolName: string;
669
+ /**
670
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
671
+ */
672
+ args: unknown;
673
+ /**
674
+ * Additional provider-specific options. They are passed through
675
+ * to the provider from the AI SDK and enable provider-specific
676
+ * functionality that can be fully encapsulated in the provider.
677
+ */
678
+ providerOptions?: SharedV2ProviderOptions;
679
+ }
680
+ /**
681
+ Tool result content part of a prompt. It contains the result of the tool call with the matching ID.
682
+ */
683
+ interface LanguageModelV2ToolResultPart {
684
+ type: 'tool-result';
685
+ /**
686
+ ID of the tool call that this result is associated with.
687
+ */
688
+ toolCallId: string;
689
+ /**
690
+ Name of the tool that generated this result.
691
+ */
692
+ toolName: string;
693
+ /**
694
+ Result of the tool call. This is a JSON-serializable object.
695
+ */
696
+ result: unknown;
697
+ /**
698
+ Optional flag if the result is an error or an error message.
699
+ */
700
+ isError?: boolean;
701
+ /**
702
+ Tool results as an array of parts. This enables advanced tool results including images.
703
+ When this is used, the `result` field should be ignored (if the provider supports content).
704
+ */
705
+ content?: Array<{
706
+ type: 'text';
707
+ /**
708
+ Text content.
709
+ */
710
+ text: string;
711
+ } | {
712
+ type: 'image';
713
+ /**
714
+ base-64 encoded image data
715
+ */
716
+ data: string;
717
+ /**
718
+ IANA media type of the image.
719
+
720
+ @see https://www.iana.org/assignments/media-types/media-types.xhtml
721
+ */
722
+ mediaType?: string;
723
+ }>;
724
+ /**
725
+ * Additional provider-specific options. They are passed through
726
+ * to the provider from the AI SDK and enable provider-specific
727
+ * functionality that can be fully encapsulated in the provider.
728
+ */
729
+ providerOptions?: SharedV2ProviderOptions;
730
+ }
731
+
732
+ /**
733
+ The configuration of a tool that is defined by the provider.
734
+ */
735
+ type LanguageModelV2ProviderDefinedTool = {
736
+ /**
737
+ The type of the tool (always 'provider-defined').
738
+ */
739
+ type: 'provider-defined';
740
+ /**
741
+ The ID of the tool. Should follow the format `<provider-name>.<tool-name>`.
742
+ */
743
+ id: `${string}.${string}`;
744
+ /**
745
+ The name of the tool. Unique within this model call.
746
+ */
747
+ name: string;
748
+ /**
749
+ The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
750
+ */
751
+ args: Record<string, unknown>;
752
+ };
753
+
754
+ type LanguageModelV2ToolChoice = {
755
+ type: 'auto';
756
+ } | {
757
+ type: 'none';
758
+ } | {
759
+ type: 'required';
760
+ } | {
761
+ type: 'tool';
762
+ toolName: string;
763
+ };
764
+
765
+ type LanguageModelV2CallOptions = {
766
+ /**
767
+ A language mode prompt is a standardized prompt type.
768
+
769
+ Note: This is **not** the user-facing prompt. The AI SDK methods will map the
770
+ user-facing prompt types such as chat or instruction prompts to this format.
771
+ That approach allows us to evolve the user facing prompts without breaking
772
+ the language model interface.
773
+ */
774
+ prompt: LanguageModelV2Prompt;
775
+ /**
776
+ Maximum number of tokens to generate.
777
+ */
778
+ maxOutputTokens?: number;
779
+ /**
780
+ Temperature setting. The range depends on the provider and model.
781
+ */
782
+ temperature?: number;
783
+ /**
784
+ Stop sequences.
785
+ If set, the model will stop generating text when one of the stop sequences is generated.
786
+ Providers may have limits on the number of stop sequences.
787
+ */
788
+ stopSequences?: string[];
789
+ /**
790
+ Nucleus sampling.
791
+ */
792
+ topP?: number;
793
+ /**
794
+ Only sample from the top K options for each subsequent token.
795
+
796
+ Used to remove "long tail" low probability responses.
797
+ Recommended for advanced use cases only. You usually only need to use temperature.
798
+ */
799
+ topK?: number;
800
+ /**
801
+ Presence penalty setting. It affects the likelihood of the model to
802
+ repeat information that is already in the prompt.
803
+ */
804
+ presencePenalty?: number;
805
+ /**
806
+ Frequency penalty setting. It affects the likelihood of the model
807
+ to repeatedly use the same words or phrases.
808
+ */
809
+ frequencyPenalty?: number;
810
+ /**
811
+ Response format. The output can either be text or JSON. Default is text.
812
+
813
+ If JSON is selected, a schema can optionally be provided to guide the LLM.
814
+ */
815
+ responseFormat?: {
816
+ type: 'text';
817
+ } | {
818
+ type: 'json';
819
+ /**
820
+ * JSON schema that the generated output should conform to.
821
+ */
822
+ schema?: JSONSchema7;
823
+ /**
824
+ * Name of output that should be generated. Used by some providers for additional LLM guidance.
825
+ */
826
+ name?: string;
827
+ /**
828
+ * Description of the output that should be generated. Used by some providers for additional LLM guidance.
829
+ */
830
+ description?: string;
831
+ };
832
+ /**
833
+ The seed (integer) to use for random sampling. If set and supported
834
+ by the model, calls will generate deterministic results.
835
+ */
836
+ seed?: number;
837
+ /**
838
+ The tools that are available for the model.
839
+ */
840
+ tools?: Array<LanguageModelV2FunctionTool | LanguageModelV2ProviderDefinedTool>;
841
+ /**
842
+ Specifies how the tool should be selected. Defaults to 'auto'.
843
+ */
844
+ toolChoice?: LanguageModelV2ToolChoice;
355
845
  /**
356
846
  Abort signal for cancelling the operation.
357
847
  */
@@ -361,15 +851,25 @@ type ImageModelV1CallOptions = {
361
851
  Only applicable for HTTP-based providers.
362
852
  */
363
853
  headers?: Record<string, string | undefined>;
854
+ /**
855
+ * Additional provider-specific options. They are passed through
856
+ * to the provider from the AI SDK and enable provider-specific
857
+ * functionality that can be fully encapsulated in the provider.
858
+ */
859
+ providerOptions?: SharedV2ProviderOptions;
364
860
  };
365
861
 
366
862
  /**
367
863
  Warning from the model provider for this call. The call will proceed, but e.g.
368
864
  some settings might not be supported, which can lead to suboptimal results.
369
865
  */
370
- type ImageModelV1CallWarning = {
866
+ type LanguageModelV2CallWarning = {
371
867
  type: 'unsupported-setting';
372
- setting: keyof ImageModelV1CallOptions;
868
+ setting: Omit<keyof LanguageModelV2CallOptions, 'prompt'>;
869
+ details?: string;
870
+ } | {
871
+ type: 'unsupported-tool';
872
+ tool: LanguageModelV2FunctionTool | LanguageModelV2ProviderDefinedTool;
373
873
  details?: string;
374
874
  } | {
375
875
  type: 'other';
@@ -377,17 +877,189 @@ type ImageModelV1CallWarning = {
377
877
  };
378
878
 
379
879
  /**
380
- Image generation model specification version 1.
880
+ A file that has been generated by the model.
881
+ Generated files as base64 encoded strings or binary data.
882
+ The files should be returned without any unnecessary conversion.
381
883
  */
382
- type ImageModelV1 = {
884
+ type LanguageModelV2File = {
885
+ type: 'file';
383
886
  /**
384
- The image model must specify which image model interface
385
- version it implements. This will allow us to evolve the image
386
- model interface and retain backwards compatibility. The different
387
- implementation versions can be handled as a discriminated union
388
- on our side.
887
+ The IANA media type of the file, e.g. `image/png` or `audio/mp3`.
888
+
889
+ @see https://www.iana.org/assignments/media-types/media-types.xhtml
890
+ */
891
+ mediaType: string;
892
+ /**
893
+ Generated file data as base64 encoded strings or binary data.
894
+
895
+ The file data should be returned without any unnecessary conversion.
896
+ If the API returns base64 encoded strings, the file data should be returned
897
+ as base64 encoded strings. If the API returns binary data, the file data should
898
+ be returned as binary data.
899
+ */
900
+ data: string | Uint8Array;
901
+ };
902
+
903
+ /**
904
+ Reasoning that the model has generated.
905
+ */
906
+ type LanguageModelV2Reasoning = {
907
+ type: 'reasoning';
908
+ text: string;
909
+ /**
910
+ * Optional provider-specific metadata for the reasoning part.
389
911
  */
390
- readonly specificationVersion: 'v1';
912
+ providerMetadata?: SharedV2ProviderMetadata;
913
+ };
914
+
915
+ /**
916
+ A source that has been used as input to generate the response.
917
+ */
918
+ type LanguageModelV2Source = {
919
+ type: 'source';
920
+ /**
921
+ * A URL source. This is return by web search RAG models.
922
+ */
923
+ sourceType: 'url';
924
+ /**
925
+ * The ID of the source.
926
+ */
927
+ id: string;
928
+ /**
929
+ * The URL of the source.
930
+ */
931
+ url: string;
932
+ /**
933
+ * The title of the source.
934
+ */
935
+ title?: string;
936
+ /**
937
+ * Additional provider metadata for the source.
938
+ */
939
+ providerMetadata?: SharedV2ProviderMetadata;
940
+ };
941
+
942
+ /**
943
+ Text that the model has generated.
944
+ */
945
+ type LanguageModelV2Text = {
946
+ type: 'text';
947
+ /**
948
+ The text content.
949
+ */
950
+ text: string;
951
+ };
952
+
953
+ /**
954
+ Tool calls that the model has generated.
955
+ */
956
+ type LanguageModelV2ToolCall = {
957
+ type: 'tool-call';
958
+ toolCallType: 'function';
959
+ toolCallId: string;
960
+ toolName: string;
961
+ /**
962
+ Stringified JSON object with the tool call arguments. Must match the
963
+ parameters schema of the tool.
964
+ */
965
+ args: string;
966
+ };
967
+
968
+ type LanguageModelV2Content = LanguageModelV2Text | LanguageModelV2Reasoning | LanguageModelV2File | LanguageModelV2Source | LanguageModelV2ToolCall;
969
+
970
+ /**
971
+ Reason why a language model finished generating a response.
972
+
973
+ Can be one of the following:
974
+ - `stop`: model generated stop sequence
975
+ - `length`: model generated maximum number of tokens
976
+ - `content-filter`: content filter violation stopped the model
977
+ - `tool-calls`: model triggered tool calls
978
+ - `error`: model stopped because of an error
979
+ - `other`: model stopped for other reasons
980
+ - `unknown`: the model has not transmitted a finish reason
981
+ */
982
+ type LanguageModelV2FinishReason = 'stop' | 'length' | 'content-filter' | 'tool-calls' | 'error' | 'other' | 'unknown';
983
+
984
+ interface LanguageModelV2ResponseMetadata {
985
+ /**
986
+ ID for the generated response, if the provider sends one.
987
+ */
988
+ id?: string;
989
+ /**
990
+ Timestamp for the start of the generated response, if the provider sends one.
991
+ */
992
+ timestamp?: Date;
993
+ /**
994
+ The ID of the response model that was used to generate the response, if the provider sends one.
995
+ */
996
+ modelId?: string;
997
+ }
998
+
999
+ type LanguageModelV2ToolCallDelta = {
1000
+ type: 'tool-call-delta';
1001
+ toolCallType: 'function';
1002
+ toolCallId: string;
1003
+ toolName: string;
1004
+ argsTextDelta: string;
1005
+ };
1006
+
1007
+ /**
1008
+ Usage information for a language model call.
1009
+
1010
+ If your API return additional usage information, you can add it to the
1011
+ provider metadata under your provider's key.
1012
+ */
1013
+ type LanguageModelV2Usage = {
1014
+ /**
1015
+ The number of input (prompt) tokens used.
1016
+ */
1017
+ inputTokens: number | undefined;
1018
+ /**
1019
+ The number of output (completion) tokens used.
1020
+ */
1021
+ outputTokens: number | undefined;
1022
+ /**
1023
+ The total number of tokens as reported by the provider.
1024
+ This number might be different from the sum of `inputTokens` and `outputTokens`
1025
+ and e.g. include reasoning tokens or other overhead.
1026
+ */
1027
+ totalTokens: number | undefined;
1028
+ /**
1029
+ The number of reasoning tokens used.
1030
+ */
1031
+ reasoningTokens?: number | undefined;
1032
+ /**
1033
+ The number of cached input tokens.
1034
+ */
1035
+ cachedInputTokens?: number | undefined;
1036
+ };
1037
+
1038
+ type LanguageModelV2StreamPart = LanguageModelV2Content | {
1039
+ type: 'reasoning-part-finish';
1040
+ } | LanguageModelV2ToolCallDelta | {
1041
+ type: 'stream-start';
1042
+ warnings: Array<LanguageModelV2CallWarning>;
1043
+ } | ({
1044
+ type: 'response-metadata';
1045
+ } & LanguageModelV2ResponseMetadata) | {
1046
+ type: 'finish';
1047
+ usage: LanguageModelV2Usage;
1048
+ finishReason: LanguageModelV2FinishReason;
1049
+ providerMetadata?: SharedV2ProviderMetadata;
1050
+ } | {
1051
+ type: 'error';
1052
+ error: unknown;
1053
+ };
1054
+
1055
+ /**
1056
+ Specification for a language model that implements the language model interface version 2.
1057
+ */
1058
+ type LanguageModelV2 = {
1059
+ /**
1060
+ The language model must specify which language model interface version it implements.
1061
+ */
1062
+ readonly specificationVersion: 'v2';
391
1063
  /**
392
1064
  Name of the provider for logging purposes.
393
1065
  */
@@ -397,49 +1069,156 @@ type ImageModelV1 = {
397
1069
  */
398
1070
  readonly modelId: string;
399
1071
  /**
400
- Limit of how many images can be generated in a single API call.
401
- If undefined, we will max generate one image per call.
1072
+ Supported URL patterns by media type for the provider.
1073
+
1074
+ The keys are media type patterns or full media types (e.g. `*\/*` for everything, `audio/*`, `video/*`, or `application/pdf`).
1075
+ and the values are arrays of regular expressions that match the URL paths.
1076
+
1077
+ The matching should be against lower-case URLs.
1078
+
1079
+ Matched URLs are supported natively by the model and are not downloaded.
1080
+
1081
+ @returns A map of supported URL patterns by media type (as a promise or a plain object).
402
1082
  */
403
- readonly maxImagesPerCall: number | undefined;
1083
+ supportedUrls: PromiseLike<Record<string, RegExp[]>> | Record<string, RegExp[]>;
404
1084
  /**
405
- Generates an array of images.
1085
+ Generates a language model output (non-streaming).
1086
+
1087
+ Naming: "do" prefix to prevent accidental direct usage of the method
1088
+ by the user.
406
1089
  */
407
- doGenerate(options: ImageModelV1CallOptions): PromiseLike<{
1090
+ doGenerate(options: LanguageModelV2CallOptions): PromiseLike<{
408
1091
  /**
409
- Generated images as base64 encoded strings or binary data.
410
- The images should be returned without any unnecessary conversion.
411
- If the API returns base64 encoded strings, the images should be returned
412
- as base64 encoded strings. If the API returns binary data, the images should
413
- be returned as binary data.
1092
+ Ordered content that the model has generated.
414
1093
  */
415
- images: Array<string> | Array<Uint8Array>;
1094
+ content: Array<LanguageModelV2Content>;
416
1095
  /**
417
- Warnings for the call, e.g. unsupported settings.
1096
+ Finish reason.
418
1097
  */
419
- warnings: Array<ImageModelV1CallWarning>;
1098
+ finishReason: LanguageModelV2FinishReason;
420
1099
  /**
421
- Response information for telemetry and debugging purposes.
1100
+ Usage information.
422
1101
  */
423
- response: {
1102
+ usage: LanguageModelV2Usage;
1103
+ /**
1104
+ Additional provider-specific metadata. They are passed through
1105
+ from the provider to the AI SDK and enable provider-specific
1106
+ results that can be fully encapsulated in the provider.
1107
+ */
1108
+ providerMetadata?: SharedV2ProviderMetadata;
1109
+ /**
1110
+ Optional request information for telemetry and debugging purposes.
1111
+ */
1112
+ request?: {
424
1113
  /**
425
- Timestamp for the start of the generated response.
426
- */
427
- timestamp: Date;
1114
+ Request HTTP body that was sent to the provider API.
1115
+ */
1116
+ body?: unknown;
1117
+ };
1118
+ /**
1119
+ Optional response information for telemetry and debugging purposes.
1120
+ */
1121
+ response?: LanguageModelV2ResponseMetadata & {
428
1122
  /**
429
- The ID of the response model that was used to generate the response.
1123
+ Response headers.
430
1124
  */
431
- modelId: string;
1125
+ headers?: SharedV2Headers;
1126
+ /**
1127
+ Response HTTP body.
1128
+ */
1129
+ body?: unknown;
1130
+ };
1131
+ /**
1132
+ Warnings for the call, e.g. unsupported settings.
1133
+ */
1134
+ warnings: Array<LanguageModelV2CallWarning>;
1135
+ }>;
1136
+ /**
1137
+ Generates a language model output (streaming).
1138
+
1139
+ Naming: "do" prefix to prevent accidental direct usage of the method
1140
+ by the user.
1141
+ *
1142
+ @return A stream of higher-level language model output parts.
1143
+ */
1144
+ doStream(options: LanguageModelV2CallOptions): PromiseLike<{
1145
+ stream: ReadableStream<LanguageModelV2StreamPart>;
1146
+ /**
1147
+ Optional request information for telemetry and debugging purposes.
1148
+ */
1149
+ request?: {
1150
+ /**
1151
+ Request HTTP body that was sent to the provider API.
1152
+ */
1153
+ body?: unknown;
1154
+ };
1155
+ /**
1156
+ Optional response data.
1157
+ */
1158
+ response?: {
432
1159
  /**
433
1160
  Response headers.
434
- */
435
- headers: Record<string, string> | undefined;
1161
+ */
1162
+ headers?: SharedV2Headers;
436
1163
  };
437
1164
  }>;
438
1165
  };
439
1166
 
440
- declare function isJSONValue(value: unknown): value is JSONValue;
441
- declare function isJSONArray(value: unknown): value is JSONArray;
442
- declare function isJSONObject(value: unknown): value is JSONObject;
1167
+ /**
1168
+ * Experimental middleware for LanguageModelV2.
1169
+ * This type defines the structure for middleware that can be used to modify
1170
+ * the behavior of LanguageModelV2 operations.
1171
+ */
1172
+ type LanguageModelV2Middleware = {
1173
+ /**
1174
+ * Middleware specification version. Use `v2` for the current version.
1175
+ */
1176
+ middlewareVersion?: 'v2' | undefined;
1177
+ /**
1178
+ * Transforms the parameters before they are passed to the language model.
1179
+ * @param options - Object containing the type of operation and the parameters.
1180
+ * @param options.type - The type of operation ('generate' or 'stream').
1181
+ * @param options.params - The original parameters for the language model call.
1182
+ * @returns A promise that resolves to the transformed parameters.
1183
+ */
1184
+ transformParams?: (options: {
1185
+ type: 'generate' | 'stream';
1186
+ params: LanguageModelV2CallOptions;
1187
+ }) => PromiseLike<LanguageModelV2CallOptions>;
1188
+ /**
1189
+ * Wraps the generate operation of the language model.
1190
+ * @param options - Object containing the generate function, parameters, and model.
1191
+ * @param options.doGenerate - The original generate function.
1192
+ * @param options.doStream - The original stream function.
1193
+ * @param options.params - The parameters for the generate call. If the
1194
+ * `transformParams` middleware is used, this will be the transformed parameters.
1195
+ * @param options.model - The language model instance.
1196
+ * @returns A promise that resolves to the result of the generate operation.
1197
+ */
1198
+ wrapGenerate?: (options: {
1199
+ doGenerate: () => ReturnType<LanguageModelV2['doGenerate']>;
1200
+ doStream: () => ReturnType<LanguageModelV2['doStream']>;
1201
+ params: LanguageModelV2CallOptions;
1202
+ model: LanguageModelV2;
1203
+ }) => Promise<Awaited<ReturnType<LanguageModelV2['doGenerate']>>>;
1204
+ /**
1205
+ * Wraps the stream operation of the language model.
1206
+ *
1207
+ * @param options - Object containing the stream function, parameters, and model.
1208
+ * @param options.doGenerate - The original generate function.
1209
+ * @param options.doStream - The original stream function.
1210
+ * @param options.params - The parameters for the stream call. If the
1211
+ * `transformParams` middleware is used, this will be the transformed parameters.
1212
+ * @param options.model - The language model instance.
1213
+ * @returns A promise that resolves to the result of the stream operation.
1214
+ */
1215
+ wrapStream?: (options: {
1216
+ doGenerate: () => ReturnType<LanguageModelV2['doGenerate']>;
1217
+ doStream: () => ReturnType<LanguageModelV2['doStream']>;
1218
+ params: LanguageModelV2CallOptions;
1219
+ model: LanguageModelV2;
1220
+ }) => PromiseLike<Awaited<ReturnType<LanguageModelV2['doStream']>>>;
1221
+ };
443
1222
 
444
1223
  /**
445
1224
  * Additional provider-specific metadata. They are passed through
@@ -1379,7 +2158,7 @@ type TranscriptionModelV1 = {
1379
2158
  /**
1380
2159
  Response headers.
1381
2160
  */
1382
- headers: Record<string, string> | undefined;
2161
+ headers?: SharedV2Headers;
1383
2162
  /**
1384
2163
  Response body.
1385
2164
  */
@@ -1514,7 +2293,7 @@ type SpeechModelV1 = {
1514
2293
  /**
1515
2294
  * Response headers.
1516
2295
  */
1517
- headers: Record<string, string> | undefined;
2296
+ headers?: SharedV2Headers;
1518
2297
  /**
1519
2298
  * Response body.
1520
2299
  */
@@ -1554,7 +2333,7 @@ interface ProviderV1 {
1554
2333
 
1555
2334
  @throws {NoSuchModelError} If no such model exists.
1556
2335
  */
1557
- textEmbeddingModel(modelId: string): EmbeddingModelV1<string>;
2336
+ textEmbeddingModel(modelId: string): EmbeddingModelV2<string>;
1558
2337
  /**
1559
2338
  Returns the image model with the given id.
1560
2339
  The model id is then passed to the provider function to get the model.
@@ -1563,7 +2342,7 @@ interface ProviderV1 {
1563
2342
 
1564
2343
  @returns {ImageModel} The image model associated with the id
1565
2344
  */
1566
- readonly imageModel?: (modelId: string) => ImageModelV1;
2345
+ readonly imageModel?: (modelId: string) => ImageModelV2;
1567
2346
  /**
1568
2347
  Returns the transcription model with the given id.
1569
2348
  The model id is then passed to the provider function to get the model.
@@ -1584,4 +2363,41 @@ interface ProviderV1 {
1584
2363
  readonly speechModel?: (modelId: string) => SpeechModelV1;
1585
2364
  }
1586
2365
 
1587
- export { AISDKError, APICallError, type EmbeddingModelV1, type EmbeddingModelV1Embedding, EmptyResponseBodyError, type ImageModelV1, type ImageModelV1CallOptions, type ImageModelV1CallWarning, InvalidArgumentError, InvalidPromptError, InvalidResponseDataError, type JSONArray, type JSONObject, JSONParseError, type JSONValue, type LanguageModelV1, type LanguageModelV1CallOptions, type LanguageModelV1CallWarning, type LanguageModelV1FilePart, type LanguageModelV1FinishReason, type LanguageModelV1FunctionTool, type LanguageModelV1FunctionToolCall, type LanguageModelV1ImagePart, type LanguageModelV1LogProbs, type LanguageModelV1Message, type LanguageModelV1ObjectGenerationMode, type LanguageModelV1Prompt, type LanguageModelV1ProviderDefinedTool, type LanguageModelV1ProviderMetadata, type LanguageModelV1ReasoningPart, type LanguageModelV1RedactedReasoningPart, type LanguageModelV1Source, type LanguageModelV1StreamPart, type LanguageModelV1TextPart, type LanguageModelV1ToolCallPart, type LanguageModelV1ToolChoice, type LanguageModelV1ToolResultPart, LoadAPIKeyError, LoadSettingError, NoContentGeneratedError, NoSuchModelError, type ProviderV1, type SpeechModelV1, type SpeechModelV1CallOptions, type SpeechModelV1CallWarning, TooManyEmbeddingValuesForCallError, type TranscriptionModelV1, type TranscriptionModelV1CallOptions, type TranscriptionModelV1CallWarning, TypeValidationError, UnsupportedFunctionalityError, getErrorMessage, isJSONArray, isJSONObject, isJSONValue };
2366
+ /**
2367
+ * Provider for language, text embedding, and image generation models.
2368
+ */
2369
+ interface ProviderV2 {
2370
+ /**
2371
+ Returns the language model with the given id.
2372
+ The model id is then passed to the provider function to get the model.
2373
+
2374
+ @param {string} modelId - The id of the model to return.
2375
+
2376
+ @returns {LanguageModel} The language model associated with the id
2377
+
2378
+ @throws {NoSuchModelError} If no such model exists.
2379
+ */
2380
+ languageModel(modelId: string): LanguageModelV2;
2381
+ /**
2382
+ Returns the text embedding model with the given id.
2383
+ The model id is then passed to the provider function to get the model.
2384
+
2385
+ @param {string} modelId - The id of the model to return.
2386
+
2387
+ @returns {LanguageModel} The language model associated with the id
2388
+
2389
+ @throws {NoSuchModelError} If no such model exists.
2390
+ */
2391
+ textEmbeddingModel(modelId: string): EmbeddingModelV2<string>;
2392
+ /**
2393
+ Returns the image model with the given id.
2394
+ The model id is then passed to the provider function to get the model.
2395
+
2396
+ @param {string} modelId - The id of the model to return.
2397
+
2398
+ @returns {ImageModel} The image model associated with the id
2399
+ */
2400
+ readonly imageModel: (modelId: string) => ImageModelV2;
2401
+ }
2402
+
2403
+ export { AISDKError, APICallError, type EmbeddingModelV2, type EmbeddingModelV2Embedding, EmptyResponseBodyError, type ImageModelV2, type ImageModelV2CallOptions, type ImageModelV2CallWarning, type ImageModelV2ProviderMetadata, InvalidArgumentError, InvalidPromptError, InvalidResponseDataError, type JSONArray, type JSONObject, JSONParseError, type JSONValue, type LanguageModelV1, type LanguageModelV1CallOptions, type LanguageModelV1CallWarning, type LanguageModelV1FilePart, type LanguageModelV1FinishReason, type LanguageModelV1FunctionTool, type LanguageModelV1FunctionToolCall, type LanguageModelV1ImagePart, type LanguageModelV1LogProbs, type LanguageModelV1Message, type LanguageModelV1ObjectGenerationMode, type LanguageModelV1Prompt, type LanguageModelV1ProviderDefinedTool, type LanguageModelV1ProviderMetadata, type LanguageModelV1ReasoningPart, type LanguageModelV1RedactedReasoningPart, type LanguageModelV1Source, type LanguageModelV1StreamPart, type LanguageModelV1TextPart, type LanguageModelV1ToolCallPart, type LanguageModelV1ToolChoice, type LanguageModelV1ToolResultPart, type LanguageModelV2, type LanguageModelV2CallOptions, type LanguageModelV2CallWarning, type LanguageModelV2Content, type LanguageModelV2DataContent, type LanguageModelV2File, type LanguageModelV2FilePart, type LanguageModelV2FinishReason, type LanguageModelV2FunctionTool, type LanguageModelV2Message, type LanguageModelV2Middleware, type LanguageModelV2Prompt, type LanguageModelV2ProviderDefinedTool, type LanguageModelV2Reasoning, type LanguageModelV2ReasoningPart, type LanguageModelV2ResponseMetadata, type LanguageModelV2Source, type LanguageModelV2StreamPart, type LanguageModelV2Text, type LanguageModelV2TextPart, type LanguageModelV2ToolCall, type LanguageModelV2ToolCallDelta, type LanguageModelV2ToolCallPart, type LanguageModelV2ToolChoice, type LanguageModelV2ToolResultPart, type LanguageModelV2Usage, LoadAPIKeyError, LoadSettingError, NoContentGeneratedError, NoSuchModelError, type ProviderV1, type ProviderV2, type SharedV2Headers, type SharedV2ProviderMetadata, type SharedV2ProviderOptions, type SpeechModelV1, type SpeechModelV1CallOptions, type SpeechModelV1CallWarning, TooManyEmbeddingValuesForCallError, type TranscriptionModelV1, type TranscriptionModelV1CallOptions, type TranscriptionModelV1CallWarning, TypeValidationError, UnsupportedFunctionalityError, getErrorMessage, isJSONArray, isJSONObject, isJSONValue };