@ai-sdk/google-vertex 4.0.146 → 4.0.148

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.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/google-vertex-provider-node.ts
2
- import { loadOptionalSetting as loadOptionalSetting2, resolve as resolve5 } from "@ai-sdk/provider-utils";
2
+ import { loadOptionalSetting as loadOptionalSetting2, resolve as resolve6 } from "@ai-sdk/provider-utils";
3
3
 
4
4
  // src/google-vertex-auth-google-auth-library.ts
5
5
  import { GoogleAuth } from "google-auth-library";
@@ -23,13 +23,13 @@ import {
23
23
  loadOptionalSetting,
24
24
  loadSetting,
25
25
  normalizeHeaders,
26
- resolve as resolve4,
26
+ resolve as resolve5,
27
27
  withoutTrailingSlash,
28
28
  withUserAgentSuffix
29
29
  } from "@ai-sdk/provider-utils";
30
30
 
31
31
  // src/version.ts
32
- var VERSION = true ? "4.0.146" : "0.0.0-test";
32
+ var VERSION = true ? "4.0.148" : "0.0.0-test";
33
33
 
34
34
  // src/google-vertex-embedding-model.ts
35
35
  import {
@@ -108,7 +108,6 @@ var googleVertexEmbeddingModelOptions = z2.object({
108
108
  var GoogleVertexEmbeddingModel = class {
109
109
  constructor(modelId, config) {
110
110
  this.specificationVersion = "v3";
111
- this.maxEmbeddingsPerCall = 2048;
112
111
  this.supportsParallelCalls = true;
113
112
  this.modelId = modelId;
114
113
  this.config = config;
@@ -116,12 +115,18 @@ var GoogleVertexEmbeddingModel = class {
116
115
  get provider() {
117
116
  return this.config.provider;
118
117
  }
118
+ // gemini-embedding-2 models only support :embedContent (one value per call),
119
+ // not the :predict batch endpoint. https://github.com/vercel/ai/issues/15853
120
+ get maxEmbeddingsPerCall() {
121
+ return usesEmbedContentEndpoint(this.modelId) ? 1 : 2048;
122
+ }
119
123
  async doEmbed({
120
124
  values,
121
125
  headers,
122
126
  abortSignal,
123
127
  providerOptions
124
128
  }) {
129
+ var _a;
125
130
  let googleOptions = await parseProviderOptions({
126
131
  provider: "vertex",
127
132
  providerOptions,
@@ -147,6 +152,37 @@ var GoogleVertexEmbeddingModel = class {
147
152
  await resolve(this.config.headers),
148
153
  headers
149
154
  );
155
+ if (usesEmbedContentEndpoint(this.modelId)) {
156
+ const {
157
+ responseHeaders: responseHeaders2,
158
+ value: response2,
159
+ rawValue: rawValue2
160
+ } = await postJsonToApi({
161
+ url: `${this.config.baseURL}/models/${this.modelId}:embedContent`,
162
+ headers: mergedHeaders,
163
+ body: {
164
+ content: { parts: [{ text: values[0] }] },
165
+ embedContentConfig: {
166
+ outputDimensionality: googleOptions.outputDimensionality,
167
+ taskType: googleOptions.taskType,
168
+ title: googleOptions.title,
169
+ autoTruncate: googleOptions.autoTruncate
170
+ }
171
+ },
172
+ failedResponseHandler: googleVertexFailedResponseHandler,
173
+ successfulResponseHandler: createJsonResponseHandler(
174
+ googleVertexEmbedContentResponseSchema
175
+ ),
176
+ abortSignal,
177
+ fetch: this.config.fetch
178
+ });
179
+ return {
180
+ warnings: [],
181
+ embeddings: [response2.embedding.values],
182
+ usage: ((_a = response2.usageMetadata) == null ? void 0 : _a.promptTokenCount) == null ? void 0 : { tokens: response2.usageMetadata.promptTokenCount },
183
+ response: { headers: responseHeaders2, body: rawValue2 }
184
+ };
185
+ }
150
186
  const url = `${this.config.baseURL}/models/${this.modelId}:predict`;
151
187
  const {
152
188
  responseHeaders,
@@ -200,6 +236,17 @@ var googleVertexTextEmbeddingResponseSchema = z3.object({
200
236
  })
201
237
  )
202
238
  });
239
+ var googleVertexEmbedContentResponseSchema = z3.object({
240
+ embedding: z3.object({
241
+ values: z3.array(z3.number())
242
+ }),
243
+ usageMetadata: z3.object({
244
+ promptTokenCount: z3.number().nullish()
245
+ }).nullish()
246
+ });
247
+ function usesEmbedContentEndpoint(modelId) {
248
+ return modelId === "gemini-embedding-2" || modelId === "gemini-embedding-2-preview";
249
+ }
203
250
 
204
251
  // src/google-vertex-image-model.ts
205
252
  import { GoogleGenerativeAILanguageModel } from "@ai-sdk/google/internal";
@@ -552,22 +599,201 @@ var googleVertexTools = {
552
599
  vertexRagStore: googleTools.vertexRagStore
553
600
  };
554
601
 
602
+ // src/google-vertex-transcription-model.ts
603
+ import {
604
+ combineHeaders as combineHeaders3,
605
+ convertUint8ArrayToBase64 as convertUint8ArrayToBase642,
606
+ createJsonResponseHandler as createJsonResponseHandler3,
607
+ parseProviderOptions as parseProviderOptions3,
608
+ postJsonToApi as postJsonToApi3,
609
+ resolve as resolve3
610
+ } from "@ai-sdk/provider-utils";
611
+ import { z as z6 } from "zod/v4";
612
+
613
+ // src/google-vertex-transcription-model-options.ts
614
+ import { z as z5 } from "zod/v4";
615
+ var googleVertexTranscriptionProviderOptionsSchema = z5.object({
616
+ /**
617
+ * BCP-47 language codes to recognize (e.g. `['en-US']`), or `['auto']` to let
618
+ * Chirp auto-detect the spoken language. Defaults to `['auto']`. For
619
+ * `telephony`, pass a supported explicit language code.
620
+ */
621
+ languageCodes: z5.array(z5.string()).optional(),
622
+ /**
623
+ * Whether to add punctuation to the transcript. Defaults to `true`.
624
+ */
625
+ enableAutomaticPunctuation: z5.boolean().optional(),
626
+ /**
627
+ * Whether to include word-level timestamps. Defaults to `true` so the
628
+ * transcription result can include segments.
629
+ *
630
+ * Enabling word-level timestamps can reduce transcription quality and speed
631
+ * for Chirp models.
632
+ */
633
+ enableWordTimeOffsets: z5.boolean().optional(),
634
+ /**
635
+ * The Cloud Speech-to-Text region for the request (e.g. `'us'`, `'eu'`,
636
+ * `'us-central1'`). Defaults to the provider `location`.
637
+ *
638
+ * Note: Speech-to-Text regions differ from Vertex AI regions. Chirp is only
639
+ * available in specific Speech-to-Text regions and is not available in the
640
+ * `global` location.
641
+ */
642
+ region: z5.string().optional()
643
+ });
644
+
645
+ // src/google-vertex-transcription-model.ts
646
+ function parseDurationSeconds(value) {
647
+ if (value == null) {
648
+ return void 0;
649
+ }
650
+ const seconds = Number.parseFloat(value);
651
+ return Number.isFinite(seconds) ? seconds : void 0;
652
+ }
653
+ function convertBcp47ToIso6391(value) {
654
+ if (value == null) {
655
+ return void 0;
656
+ }
657
+ try {
658
+ const language = new Intl.Locale(value).language;
659
+ return language.length === 2 ? language : void 0;
660
+ } catch (e) {
661
+ return void 0;
662
+ }
663
+ }
664
+ var GoogleVertexTranscriptionModel = class {
665
+ constructor(modelId, config) {
666
+ this.modelId = modelId;
667
+ this.config = config;
668
+ this.specificationVersion = "v3";
669
+ }
670
+ get provider() {
671
+ return this.config.provider;
672
+ }
673
+ async doGenerate(options) {
674
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
675
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
676
+ const warnings = [];
677
+ let googleOptions;
678
+ for (const provider of ["googleVertex", "vertex", "google"]) {
679
+ googleOptions = await parseProviderOptions3({
680
+ provider,
681
+ providerOptions: options.providerOptions,
682
+ schema: googleVertexTranscriptionProviderOptionsSchema
683
+ });
684
+ if (googleOptions != null) {
685
+ break;
686
+ }
687
+ }
688
+ const region = (_d = googleOptions == null ? void 0 : googleOptions.region) != null ? _d : this.config.location;
689
+ const languageCodes = (_e = googleOptions == null ? void 0 : googleOptions.languageCodes) != null ? _e : ["auto"];
690
+ const content = typeof options.audio === "string" ? options.audio : convertUint8ArrayToBase642(options.audio);
691
+ const requestBody = {
692
+ config: {
693
+ model: this.modelId,
694
+ languageCodes,
695
+ // Let Speech-to-Text auto-detect the audio encoding (wav/mp3/flac/…).
696
+ autoDecodingConfig: {},
697
+ features: {
698
+ // Word timing populates `segments`.
699
+ enableWordTimeOffsets: (_f = googleOptions == null ? void 0 : googleOptions.enableWordTimeOffsets) != null ? _f : true,
700
+ enableAutomaticPunctuation: (_g = googleOptions == null ? void 0 : googleOptions.enableAutomaticPunctuation) != null ? _g : true
701
+ }
702
+ },
703
+ content
704
+ };
705
+ const host = region === "global" ? "speech.googleapis.com" : `${region}-speech.googleapis.com`;
706
+ const url = `https://${host}/v2/projects/${this.config.project}/locations/${region}/recognizers/_:recognize`;
707
+ const {
708
+ value: response,
709
+ responseHeaders,
710
+ rawValue: rawResponse
711
+ } = await postJsonToApi3({
712
+ url,
713
+ headers: combineHeaders3(
714
+ this.config.headers ? await resolve3(this.config.headers) : void 0,
715
+ options.headers
716
+ ),
717
+ body: requestBody,
718
+ failedResponseHandler: googleVertexFailedResponseHandler,
719
+ successfulResponseHandler: createJsonResponseHandler3(
720
+ googleVertexTranscriptionResponseSchema
721
+ ),
722
+ abortSignal: options.abortSignal,
723
+ fetch: this.config.fetch
724
+ });
725
+ const results = (_h = response.results) != null ? _h : [];
726
+ const text = results.map((result) => {
727
+ var _a2, _b2, _c2;
728
+ return (_c2 = (_b2 = (_a2 = result.alternatives) == null ? void 0 : _a2[0]) == null ? void 0 : _b2.transcript) != null ? _c2 : "";
729
+ }).join(" ").trim();
730
+ const segments = results.flatMap(
731
+ (result) => {
732
+ var _a2, _b2, _c2, _d2;
733
+ return (_d2 = (_c2 = (_b2 = (_a2 = result.alternatives) == null ? void 0 : _a2[0]) == null ? void 0 : _b2.words) == null ? void 0 : _c2.flatMap((word) => {
734
+ const startSecond = parseDurationSeconds(word.startOffset);
735
+ const endSecond = parseDurationSeconds(word.endOffset);
736
+ return word.word == null || startSecond == null || endSecond == null ? [] : [{ text: word.word, startSecond, endSecond }];
737
+ })) != null ? _d2 : [];
738
+ }
739
+ );
740
+ const language = convertBcp47ToIso6391((_i = results[0]) == null ? void 0 : _i.languageCode);
741
+ return {
742
+ text,
743
+ segments,
744
+ language,
745
+ durationInSeconds: parseDurationSeconds(
746
+ (_j = response.metadata) == null ? void 0 : _j.totalBilledDuration
747
+ ),
748
+ warnings,
749
+ response: {
750
+ timestamp: currentDate,
751
+ modelId: this.modelId,
752
+ headers: responseHeaders,
753
+ body: rawResponse
754
+ }
755
+ };
756
+ }
757
+ };
758
+ var googleVertexTranscriptionResponseSchema = z6.object({
759
+ results: z6.array(
760
+ z6.object({
761
+ alternatives: z6.array(
762
+ z6.object({
763
+ transcript: z6.string().nullish(),
764
+ words: z6.array(
765
+ z6.object({
766
+ word: z6.string().nullish(),
767
+ startOffset: z6.string().nullish(),
768
+ endOffset: z6.string().nullish()
769
+ })
770
+ ).nullish()
771
+ })
772
+ ).nullish(),
773
+ languageCode: z6.string().nullish()
774
+ })
775
+ ).nullish(),
776
+ metadata: z6.object({
777
+ totalBilledDuration: z6.string().nullish()
778
+ }).nullish()
779
+ });
780
+
555
781
  // src/google-vertex-video-model.ts
556
782
  import {
557
783
  AISDKError
558
784
  } from "@ai-sdk/provider";
559
785
  import {
560
- combineHeaders as combineHeaders3,
561
- convertUint8ArrayToBase64 as convertUint8ArrayToBase642,
562
- createJsonResponseHandler as createJsonResponseHandler3,
786
+ combineHeaders as combineHeaders4,
787
+ convertUint8ArrayToBase64 as convertUint8ArrayToBase643,
788
+ createJsonResponseHandler as createJsonResponseHandler4,
563
789
  delay,
564
790
  lazySchema,
565
- parseProviderOptions as parseProviderOptions3,
566
- postJsonToApi as postJsonToApi3,
567
- resolve as resolve3,
791
+ parseProviderOptions as parseProviderOptions4,
792
+ postJsonToApi as postJsonToApi4,
793
+ resolve as resolve4,
568
794
  zodSchema
569
795
  } from "@ai-sdk/provider-utils";
570
- import { z as z5 } from "zod/v4";
796
+ import { z as z7 } from "zod/v4";
571
797
  var GoogleVertexVideoModel = class {
572
798
  constructor(modelId, config) {
573
799
  this.modelId = modelId;
@@ -584,7 +810,7 @@ var GoogleVertexVideoModel = class {
584
810
  var _a, _b, _c, _d, _e, _f;
585
811
  const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
586
812
  const warnings = [];
587
- const vertexOptions = await parseProviderOptions3({
813
+ const vertexOptions = await parseProviderOptions4({
588
814
  provider: "vertex",
589
815
  providerOptions: options.providerOptions,
590
816
  schema: googleVertexVideoModelOptionsSchema
@@ -602,7 +828,7 @@ var GoogleVertexVideoModel = class {
602
828
  details: "Vertex AI video models require base64-encoded images or GCS URIs. URL will be ignored."
603
829
  });
604
830
  } else {
605
- const base64Data = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase642(options.image.data);
831
+ const base64Data = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase643(options.image.data);
606
832
  instance.image = {
607
833
  bytesBase64Encoded: base64Data,
608
834
  mimeType: options.image.mediaType
@@ -660,17 +886,17 @@ var GoogleVertexVideoModel = class {
660
886
  }
661
887
  }
662
888
  }
663
- const { value: operation } = await postJsonToApi3({
889
+ const { value: operation } = await postJsonToApi4({
664
890
  url: `${this.config.baseURL}/models/${this.modelId}:predictLongRunning`,
665
- headers: combineHeaders3(
666
- await resolve3(this.config.headers),
891
+ headers: combineHeaders4(
892
+ await resolve4(this.config.headers),
667
893
  options.headers
668
894
  ),
669
895
  body: {
670
896
  instances,
671
897
  parameters
672
898
  },
673
- successfulResponseHandler: createJsonResponseHandler3(
899
+ successfulResponseHandler: createJsonResponseHandler4(
674
900
  vertexOperationSchema
675
901
  ),
676
902
  failedResponseHandler: googleVertexFailedResponseHandler,
@@ -703,16 +929,16 @@ var GoogleVertexVideoModel = class {
703
929
  message: "Video generation request was aborted"
704
930
  });
705
931
  }
706
- const { value: statusOperation, responseHeaders: pollHeaders } = await postJsonToApi3({
932
+ const { value: statusOperation, responseHeaders: pollHeaders } = await postJsonToApi4({
707
933
  url: `${this.config.baseURL}/models/${this.modelId}:fetchPredictOperation`,
708
- headers: combineHeaders3(
709
- await resolve3(this.config.headers),
934
+ headers: combineHeaders4(
935
+ await resolve4(this.config.headers),
710
936
  options.headers
711
937
  ),
712
938
  body: {
713
939
  operationName
714
940
  },
715
- successfulResponseHandler: createJsonResponseHandler3(
941
+ successfulResponseHandler: createJsonResponseHandler4(
716
942
  vertexOperationSchema
717
943
  ),
718
944
  failedResponseHandler: googleVertexFailedResponseHandler,
@@ -781,38 +1007,38 @@ var GoogleVertexVideoModel = class {
781
1007
  };
782
1008
  }
783
1009
  };
784
- var vertexOperationSchema = z5.object({
785
- name: z5.string().nullish(),
786
- done: z5.boolean().nullish(),
787
- error: z5.object({
788
- code: z5.number().nullish(),
789
- message: z5.string(),
790
- status: z5.string().nullish()
1010
+ var vertexOperationSchema = z7.object({
1011
+ name: z7.string().nullish(),
1012
+ done: z7.boolean().nullish(),
1013
+ error: z7.object({
1014
+ code: z7.number().nullish(),
1015
+ message: z7.string(),
1016
+ status: z7.string().nullish()
791
1017
  }).nullish(),
792
- response: z5.object({
793
- videos: z5.array(
794
- z5.object({
795
- bytesBase64Encoded: z5.string().nullish(),
796
- gcsUri: z5.string().nullish(),
797
- mimeType: z5.string().nullish()
1018
+ response: z7.object({
1019
+ videos: z7.array(
1020
+ z7.object({
1021
+ bytesBase64Encoded: z7.string().nullish(),
1022
+ gcsUri: z7.string().nullish(),
1023
+ mimeType: z7.string().nullish()
798
1024
  })
799
1025
  ).nullish(),
800
- raiMediaFilteredCount: z5.number().nullish()
1026
+ raiMediaFilteredCount: z7.number().nullish()
801
1027
  }).nullish()
802
1028
  });
803
1029
  var googleVertexVideoModelOptionsSchema = lazySchema(
804
1030
  () => zodSchema(
805
- z5.object({
806
- pollIntervalMs: z5.number().positive().nullish(),
807
- pollTimeoutMs: z5.number().positive().nullish(),
808
- personGeneration: z5.enum(["dont_allow", "allow_adult", "allow_all"]).nullish(),
809
- negativePrompt: z5.string().nullish(),
810
- generateAudio: z5.boolean().nullish(),
811
- gcsOutputDirectory: z5.string().nullish(),
812
- referenceImages: z5.array(
813
- z5.object({
814
- bytesBase64Encoded: z5.string().nullish(),
815
- gcsUri: z5.string().nullish()
1031
+ z7.object({
1032
+ pollIntervalMs: z7.number().positive().nullish(),
1033
+ pollTimeoutMs: z7.number().positive().nullish(),
1034
+ personGeneration: z7.enum(["dont_allow", "allow_adult", "allow_all"]).nullish(),
1035
+ negativePrompt: z7.string().nullish(),
1036
+ generateAudio: z7.boolean().nullish(),
1037
+ gcsOutputDirectory: z7.string().nullish(),
1038
+ referenceImages: z7.array(
1039
+ z7.object({
1040
+ bytesBase64Encoded: z7.string().nullish(),
1041
+ gcsUri: z7.string().nullish()
816
1042
  })
817
1043
  ).nullish()
818
1044
  }).passthrough()
@@ -871,7 +1097,7 @@ function createVertex(options = {}) {
871
1097
  const createConfig = (name) => {
872
1098
  const getHeaders = async () => {
873
1099
  var _a;
874
- const originalHeaders = await resolve4((_a = options.headers) != null ? _a : {});
1100
+ const originalHeaders = await resolve5((_a = options.headers) != null ? _a : {});
875
1101
  return withUserAgentSuffix(
876
1102
  originalHeaders,
877
1103
  `ai-sdk/google-vertex/${VERSION}`
@@ -914,6 +1140,21 @@ function createVertex(options = {}) {
914
1140
  generateId: (_a = options.generateId) != null ? _a : generateId
915
1141
  });
916
1142
  };
1143
+ const createTranscriptionModel = (modelId) => {
1144
+ if (apiKey) {
1145
+ throw new Error(
1146
+ "Google Vertex transcription models do not support Express Mode API keys. Use standard Google Cloud credentials instead."
1147
+ );
1148
+ }
1149
+ const config = createConfig("transcription");
1150
+ return new GoogleVertexTranscriptionModel(modelId, {
1151
+ provider: config.provider,
1152
+ headers: config.headers,
1153
+ fetch: config.fetch,
1154
+ project: loadVertexProject(),
1155
+ location: loadVertexLocation()
1156
+ });
1157
+ };
917
1158
  const provider = function(modelId) {
918
1159
  if (new.target) {
919
1160
  throw new Error(
@@ -930,6 +1171,8 @@ function createVertex(options = {}) {
930
1171
  provider.imageModel = createImageModel;
931
1172
  provider.video = createVideoModel;
932
1173
  provider.videoModel = createVideoModel;
1174
+ provider.transcription = createTranscriptionModel;
1175
+ provider.transcriptionModel = createTranscriptionModel;
933
1176
  provider.tools = googleVertexTools;
934
1177
  return provider;
935
1178
  }
@@ -952,7 +1195,7 @@ function createVertex2(options = {}) {
952
1195
  ...options,
953
1196
  headers: async () => ({
954
1197
  Authorization: `Bearer ${await generateAuthToken()}`,
955
- ...await resolve5(options.headers)
1198
+ ...await resolve6(options.headers)
956
1199
  })
957
1200
  });
958
1201
  }