@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.
@@ -1,5 +1,5 @@
1
1
  // src/edge/google-vertex-provider-edge.ts
2
- import { loadOptionalSetting as loadOptionalSetting3, resolve as resolve5 } from "@ai-sdk/provider-utils";
2
+ import { loadOptionalSetting as loadOptionalSetting3, resolve as resolve6 } from "@ai-sdk/provider-utils";
3
3
 
4
4
  // src/google-vertex-provider.ts
5
5
  import { GoogleGenerativeAILanguageModel as GoogleGenerativeAILanguageModel2 } from "@ai-sdk/google/internal";
@@ -8,13 +8,13 @@ import {
8
8
  loadOptionalSetting,
9
9
  loadSetting,
10
10
  normalizeHeaders,
11
- resolve as resolve4,
11
+ resolve as resolve5,
12
12
  withoutTrailingSlash,
13
13
  withUserAgentSuffix
14
14
  } from "@ai-sdk/provider-utils";
15
15
 
16
16
  // src/version.ts
17
- var VERSION = true ? "4.0.146" : "0.0.0-test";
17
+ var VERSION = true ? "4.0.148" : "0.0.0-test";
18
18
 
19
19
  // src/google-vertex-embedding-model.ts
20
20
  import {
@@ -93,7 +93,6 @@ var googleVertexEmbeddingModelOptions = z2.object({
93
93
  var GoogleVertexEmbeddingModel = class {
94
94
  constructor(modelId, config) {
95
95
  this.specificationVersion = "v3";
96
- this.maxEmbeddingsPerCall = 2048;
97
96
  this.supportsParallelCalls = true;
98
97
  this.modelId = modelId;
99
98
  this.config = config;
@@ -101,12 +100,18 @@ var GoogleVertexEmbeddingModel = class {
101
100
  get provider() {
102
101
  return this.config.provider;
103
102
  }
103
+ // gemini-embedding-2 models only support :embedContent (one value per call),
104
+ // not the :predict batch endpoint. https://github.com/vercel/ai/issues/15853
105
+ get maxEmbeddingsPerCall() {
106
+ return usesEmbedContentEndpoint(this.modelId) ? 1 : 2048;
107
+ }
104
108
  async doEmbed({
105
109
  values,
106
110
  headers,
107
111
  abortSignal,
108
112
  providerOptions
109
113
  }) {
114
+ var _a;
110
115
  let googleOptions = await parseProviderOptions({
111
116
  provider: "vertex",
112
117
  providerOptions,
@@ -132,6 +137,37 @@ var GoogleVertexEmbeddingModel = class {
132
137
  await resolve(this.config.headers),
133
138
  headers
134
139
  );
140
+ if (usesEmbedContentEndpoint(this.modelId)) {
141
+ const {
142
+ responseHeaders: responseHeaders2,
143
+ value: response2,
144
+ rawValue: rawValue2
145
+ } = await postJsonToApi({
146
+ url: `${this.config.baseURL}/models/${this.modelId}:embedContent`,
147
+ headers: mergedHeaders,
148
+ body: {
149
+ content: { parts: [{ text: values[0] }] },
150
+ embedContentConfig: {
151
+ outputDimensionality: googleOptions.outputDimensionality,
152
+ taskType: googleOptions.taskType,
153
+ title: googleOptions.title,
154
+ autoTruncate: googleOptions.autoTruncate
155
+ }
156
+ },
157
+ failedResponseHandler: googleVertexFailedResponseHandler,
158
+ successfulResponseHandler: createJsonResponseHandler(
159
+ googleVertexEmbedContentResponseSchema
160
+ ),
161
+ abortSignal,
162
+ fetch: this.config.fetch
163
+ });
164
+ return {
165
+ warnings: [],
166
+ embeddings: [response2.embedding.values],
167
+ usage: ((_a = response2.usageMetadata) == null ? void 0 : _a.promptTokenCount) == null ? void 0 : { tokens: response2.usageMetadata.promptTokenCount },
168
+ response: { headers: responseHeaders2, body: rawValue2 }
169
+ };
170
+ }
135
171
  const url = `${this.config.baseURL}/models/${this.modelId}:predict`;
136
172
  const {
137
173
  responseHeaders,
@@ -185,6 +221,17 @@ var googleVertexTextEmbeddingResponseSchema = z3.object({
185
221
  })
186
222
  )
187
223
  });
224
+ var googleVertexEmbedContentResponseSchema = z3.object({
225
+ embedding: z3.object({
226
+ values: z3.array(z3.number())
227
+ }),
228
+ usageMetadata: z3.object({
229
+ promptTokenCount: z3.number().nullish()
230
+ }).nullish()
231
+ });
232
+ function usesEmbedContentEndpoint(modelId) {
233
+ return modelId === "gemini-embedding-2" || modelId === "gemini-embedding-2-preview";
234
+ }
188
235
 
189
236
  // src/google-vertex-image-model.ts
190
237
  import { GoogleGenerativeAILanguageModel } from "@ai-sdk/google/internal";
@@ -537,22 +584,201 @@ var googleVertexTools = {
537
584
  vertexRagStore: googleTools.vertexRagStore
538
585
  };
539
586
 
587
+ // src/google-vertex-transcription-model.ts
588
+ import {
589
+ combineHeaders as combineHeaders3,
590
+ convertUint8ArrayToBase64 as convertUint8ArrayToBase642,
591
+ createJsonResponseHandler as createJsonResponseHandler3,
592
+ parseProviderOptions as parseProviderOptions3,
593
+ postJsonToApi as postJsonToApi3,
594
+ resolve as resolve3
595
+ } from "@ai-sdk/provider-utils";
596
+ import { z as z6 } from "zod/v4";
597
+
598
+ // src/google-vertex-transcription-model-options.ts
599
+ import { z as z5 } from "zod/v4";
600
+ var googleVertexTranscriptionProviderOptionsSchema = z5.object({
601
+ /**
602
+ * BCP-47 language codes to recognize (e.g. `['en-US']`), or `['auto']` to let
603
+ * Chirp auto-detect the spoken language. Defaults to `['auto']`. For
604
+ * `telephony`, pass a supported explicit language code.
605
+ */
606
+ languageCodes: z5.array(z5.string()).optional(),
607
+ /**
608
+ * Whether to add punctuation to the transcript. Defaults to `true`.
609
+ */
610
+ enableAutomaticPunctuation: z5.boolean().optional(),
611
+ /**
612
+ * Whether to include word-level timestamps. Defaults to `true` so the
613
+ * transcription result can include segments.
614
+ *
615
+ * Enabling word-level timestamps can reduce transcription quality and speed
616
+ * for Chirp models.
617
+ */
618
+ enableWordTimeOffsets: z5.boolean().optional(),
619
+ /**
620
+ * The Cloud Speech-to-Text region for the request (e.g. `'us'`, `'eu'`,
621
+ * `'us-central1'`). Defaults to the provider `location`.
622
+ *
623
+ * Note: Speech-to-Text regions differ from Vertex AI regions. Chirp is only
624
+ * available in specific Speech-to-Text regions and is not available in the
625
+ * `global` location.
626
+ */
627
+ region: z5.string().optional()
628
+ });
629
+
630
+ // src/google-vertex-transcription-model.ts
631
+ function parseDurationSeconds(value) {
632
+ if (value == null) {
633
+ return void 0;
634
+ }
635
+ const seconds = Number.parseFloat(value);
636
+ return Number.isFinite(seconds) ? seconds : void 0;
637
+ }
638
+ function convertBcp47ToIso6391(value) {
639
+ if (value == null) {
640
+ return void 0;
641
+ }
642
+ try {
643
+ const language = new Intl.Locale(value).language;
644
+ return language.length === 2 ? language : void 0;
645
+ } catch (e) {
646
+ return void 0;
647
+ }
648
+ }
649
+ var GoogleVertexTranscriptionModel = class {
650
+ constructor(modelId, config) {
651
+ this.modelId = modelId;
652
+ this.config = config;
653
+ this.specificationVersion = "v3";
654
+ }
655
+ get provider() {
656
+ return this.config.provider;
657
+ }
658
+ async doGenerate(options) {
659
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
660
+ const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
661
+ const warnings = [];
662
+ let googleOptions;
663
+ for (const provider of ["googleVertex", "vertex", "google"]) {
664
+ googleOptions = await parseProviderOptions3({
665
+ provider,
666
+ providerOptions: options.providerOptions,
667
+ schema: googleVertexTranscriptionProviderOptionsSchema
668
+ });
669
+ if (googleOptions != null) {
670
+ break;
671
+ }
672
+ }
673
+ const region = (_d = googleOptions == null ? void 0 : googleOptions.region) != null ? _d : this.config.location;
674
+ const languageCodes = (_e = googleOptions == null ? void 0 : googleOptions.languageCodes) != null ? _e : ["auto"];
675
+ const content = typeof options.audio === "string" ? options.audio : convertUint8ArrayToBase642(options.audio);
676
+ const requestBody = {
677
+ config: {
678
+ model: this.modelId,
679
+ languageCodes,
680
+ // Let Speech-to-Text auto-detect the audio encoding (wav/mp3/flac/…).
681
+ autoDecodingConfig: {},
682
+ features: {
683
+ // Word timing populates `segments`.
684
+ enableWordTimeOffsets: (_f = googleOptions == null ? void 0 : googleOptions.enableWordTimeOffsets) != null ? _f : true,
685
+ enableAutomaticPunctuation: (_g = googleOptions == null ? void 0 : googleOptions.enableAutomaticPunctuation) != null ? _g : true
686
+ }
687
+ },
688
+ content
689
+ };
690
+ const host = region === "global" ? "speech.googleapis.com" : `${region}-speech.googleapis.com`;
691
+ const url = `https://${host}/v2/projects/${this.config.project}/locations/${region}/recognizers/_:recognize`;
692
+ const {
693
+ value: response,
694
+ responseHeaders,
695
+ rawValue: rawResponse
696
+ } = await postJsonToApi3({
697
+ url,
698
+ headers: combineHeaders3(
699
+ this.config.headers ? await resolve3(this.config.headers) : void 0,
700
+ options.headers
701
+ ),
702
+ body: requestBody,
703
+ failedResponseHandler: googleVertexFailedResponseHandler,
704
+ successfulResponseHandler: createJsonResponseHandler3(
705
+ googleVertexTranscriptionResponseSchema
706
+ ),
707
+ abortSignal: options.abortSignal,
708
+ fetch: this.config.fetch
709
+ });
710
+ const results = (_h = response.results) != null ? _h : [];
711
+ const text = results.map((result) => {
712
+ var _a2, _b2, _c2;
713
+ return (_c2 = (_b2 = (_a2 = result.alternatives) == null ? void 0 : _a2[0]) == null ? void 0 : _b2.transcript) != null ? _c2 : "";
714
+ }).join(" ").trim();
715
+ const segments = results.flatMap(
716
+ (result) => {
717
+ var _a2, _b2, _c2, _d2;
718
+ return (_d2 = (_c2 = (_b2 = (_a2 = result.alternatives) == null ? void 0 : _a2[0]) == null ? void 0 : _b2.words) == null ? void 0 : _c2.flatMap((word) => {
719
+ const startSecond = parseDurationSeconds(word.startOffset);
720
+ const endSecond = parseDurationSeconds(word.endOffset);
721
+ return word.word == null || startSecond == null || endSecond == null ? [] : [{ text: word.word, startSecond, endSecond }];
722
+ })) != null ? _d2 : [];
723
+ }
724
+ );
725
+ const language = convertBcp47ToIso6391((_i = results[0]) == null ? void 0 : _i.languageCode);
726
+ return {
727
+ text,
728
+ segments,
729
+ language,
730
+ durationInSeconds: parseDurationSeconds(
731
+ (_j = response.metadata) == null ? void 0 : _j.totalBilledDuration
732
+ ),
733
+ warnings,
734
+ response: {
735
+ timestamp: currentDate,
736
+ modelId: this.modelId,
737
+ headers: responseHeaders,
738
+ body: rawResponse
739
+ }
740
+ };
741
+ }
742
+ };
743
+ var googleVertexTranscriptionResponseSchema = z6.object({
744
+ results: z6.array(
745
+ z6.object({
746
+ alternatives: z6.array(
747
+ z6.object({
748
+ transcript: z6.string().nullish(),
749
+ words: z6.array(
750
+ z6.object({
751
+ word: z6.string().nullish(),
752
+ startOffset: z6.string().nullish(),
753
+ endOffset: z6.string().nullish()
754
+ })
755
+ ).nullish()
756
+ })
757
+ ).nullish(),
758
+ languageCode: z6.string().nullish()
759
+ })
760
+ ).nullish(),
761
+ metadata: z6.object({
762
+ totalBilledDuration: z6.string().nullish()
763
+ }).nullish()
764
+ });
765
+
540
766
  // src/google-vertex-video-model.ts
541
767
  import {
542
768
  AISDKError
543
769
  } from "@ai-sdk/provider";
544
770
  import {
545
- combineHeaders as combineHeaders3,
546
- convertUint8ArrayToBase64 as convertUint8ArrayToBase642,
547
- createJsonResponseHandler as createJsonResponseHandler3,
771
+ combineHeaders as combineHeaders4,
772
+ convertUint8ArrayToBase64 as convertUint8ArrayToBase643,
773
+ createJsonResponseHandler as createJsonResponseHandler4,
548
774
  delay,
549
775
  lazySchema,
550
- parseProviderOptions as parseProviderOptions3,
551
- postJsonToApi as postJsonToApi3,
552
- resolve as resolve3,
776
+ parseProviderOptions as parseProviderOptions4,
777
+ postJsonToApi as postJsonToApi4,
778
+ resolve as resolve4,
553
779
  zodSchema
554
780
  } from "@ai-sdk/provider-utils";
555
- import { z as z5 } from "zod/v4";
781
+ import { z as z7 } from "zod/v4";
556
782
  var GoogleVertexVideoModel = class {
557
783
  constructor(modelId, config) {
558
784
  this.modelId = modelId;
@@ -569,7 +795,7 @@ var GoogleVertexVideoModel = class {
569
795
  var _a, _b, _c, _d, _e, _f;
570
796
  const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
571
797
  const warnings = [];
572
- const vertexOptions = await parseProviderOptions3({
798
+ const vertexOptions = await parseProviderOptions4({
573
799
  provider: "vertex",
574
800
  providerOptions: options.providerOptions,
575
801
  schema: googleVertexVideoModelOptionsSchema
@@ -587,7 +813,7 @@ var GoogleVertexVideoModel = class {
587
813
  details: "Vertex AI video models require base64-encoded images or GCS URIs. URL will be ignored."
588
814
  });
589
815
  } else {
590
- const base64Data = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase642(options.image.data);
816
+ const base64Data = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase643(options.image.data);
591
817
  instance.image = {
592
818
  bytesBase64Encoded: base64Data,
593
819
  mimeType: options.image.mediaType
@@ -645,17 +871,17 @@ var GoogleVertexVideoModel = class {
645
871
  }
646
872
  }
647
873
  }
648
- const { value: operation } = await postJsonToApi3({
874
+ const { value: operation } = await postJsonToApi4({
649
875
  url: `${this.config.baseURL}/models/${this.modelId}:predictLongRunning`,
650
- headers: combineHeaders3(
651
- await resolve3(this.config.headers),
876
+ headers: combineHeaders4(
877
+ await resolve4(this.config.headers),
652
878
  options.headers
653
879
  ),
654
880
  body: {
655
881
  instances,
656
882
  parameters
657
883
  },
658
- successfulResponseHandler: createJsonResponseHandler3(
884
+ successfulResponseHandler: createJsonResponseHandler4(
659
885
  vertexOperationSchema
660
886
  ),
661
887
  failedResponseHandler: googleVertexFailedResponseHandler,
@@ -688,16 +914,16 @@ var GoogleVertexVideoModel = class {
688
914
  message: "Video generation request was aborted"
689
915
  });
690
916
  }
691
- const { value: statusOperation, responseHeaders: pollHeaders } = await postJsonToApi3({
917
+ const { value: statusOperation, responseHeaders: pollHeaders } = await postJsonToApi4({
692
918
  url: `${this.config.baseURL}/models/${this.modelId}:fetchPredictOperation`,
693
- headers: combineHeaders3(
694
- await resolve3(this.config.headers),
919
+ headers: combineHeaders4(
920
+ await resolve4(this.config.headers),
695
921
  options.headers
696
922
  ),
697
923
  body: {
698
924
  operationName
699
925
  },
700
- successfulResponseHandler: createJsonResponseHandler3(
926
+ successfulResponseHandler: createJsonResponseHandler4(
701
927
  vertexOperationSchema
702
928
  ),
703
929
  failedResponseHandler: googleVertexFailedResponseHandler,
@@ -766,38 +992,38 @@ var GoogleVertexVideoModel = class {
766
992
  };
767
993
  }
768
994
  };
769
- var vertexOperationSchema = z5.object({
770
- name: z5.string().nullish(),
771
- done: z5.boolean().nullish(),
772
- error: z5.object({
773
- code: z5.number().nullish(),
774
- message: z5.string(),
775
- status: z5.string().nullish()
995
+ var vertexOperationSchema = z7.object({
996
+ name: z7.string().nullish(),
997
+ done: z7.boolean().nullish(),
998
+ error: z7.object({
999
+ code: z7.number().nullish(),
1000
+ message: z7.string(),
1001
+ status: z7.string().nullish()
776
1002
  }).nullish(),
777
- response: z5.object({
778
- videos: z5.array(
779
- z5.object({
780
- bytesBase64Encoded: z5.string().nullish(),
781
- gcsUri: z5.string().nullish(),
782
- mimeType: z5.string().nullish()
1003
+ response: z7.object({
1004
+ videos: z7.array(
1005
+ z7.object({
1006
+ bytesBase64Encoded: z7.string().nullish(),
1007
+ gcsUri: z7.string().nullish(),
1008
+ mimeType: z7.string().nullish()
783
1009
  })
784
1010
  ).nullish(),
785
- raiMediaFilteredCount: z5.number().nullish()
1011
+ raiMediaFilteredCount: z7.number().nullish()
786
1012
  }).nullish()
787
1013
  });
788
1014
  var googleVertexVideoModelOptionsSchema = lazySchema(
789
1015
  () => zodSchema(
790
- z5.object({
791
- pollIntervalMs: z5.number().positive().nullish(),
792
- pollTimeoutMs: z5.number().positive().nullish(),
793
- personGeneration: z5.enum(["dont_allow", "allow_adult", "allow_all"]).nullish(),
794
- negativePrompt: z5.string().nullish(),
795
- generateAudio: z5.boolean().nullish(),
796
- gcsOutputDirectory: z5.string().nullish(),
797
- referenceImages: z5.array(
798
- z5.object({
799
- bytesBase64Encoded: z5.string().nullish(),
800
- gcsUri: z5.string().nullish()
1016
+ z7.object({
1017
+ pollIntervalMs: z7.number().positive().nullish(),
1018
+ pollTimeoutMs: z7.number().positive().nullish(),
1019
+ personGeneration: z7.enum(["dont_allow", "allow_adult", "allow_all"]).nullish(),
1020
+ negativePrompt: z7.string().nullish(),
1021
+ generateAudio: z7.boolean().nullish(),
1022
+ gcsOutputDirectory: z7.string().nullish(),
1023
+ referenceImages: z7.array(
1024
+ z7.object({
1025
+ bytesBase64Encoded: z7.string().nullish(),
1026
+ gcsUri: z7.string().nullish()
801
1027
  })
802
1028
  ).nullish()
803
1029
  }).passthrough()
@@ -856,7 +1082,7 @@ function createVertex(options = {}) {
856
1082
  const createConfig = (name) => {
857
1083
  const getHeaders = async () => {
858
1084
  var _a;
859
- const originalHeaders = await resolve4((_a = options.headers) != null ? _a : {});
1085
+ const originalHeaders = await resolve5((_a = options.headers) != null ? _a : {});
860
1086
  return withUserAgentSuffix(
861
1087
  originalHeaders,
862
1088
  `ai-sdk/google-vertex/${VERSION}`
@@ -899,6 +1125,21 @@ function createVertex(options = {}) {
899
1125
  generateId: (_a = options.generateId) != null ? _a : generateId
900
1126
  });
901
1127
  };
1128
+ const createTranscriptionModel = (modelId) => {
1129
+ if (apiKey) {
1130
+ throw new Error(
1131
+ "Google Vertex transcription models do not support Express Mode API keys. Use standard Google Cloud credentials instead."
1132
+ );
1133
+ }
1134
+ const config = createConfig("transcription");
1135
+ return new GoogleVertexTranscriptionModel(modelId, {
1136
+ provider: config.provider,
1137
+ headers: config.headers,
1138
+ fetch: config.fetch,
1139
+ project: loadVertexProject(),
1140
+ location: loadVertexLocation()
1141
+ });
1142
+ };
902
1143
  const provider = function(modelId) {
903
1144
  if (new.target) {
904
1145
  throw new Error(
@@ -915,6 +1156,8 @@ function createVertex(options = {}) {
915
1156
  provider.imageModel = createImageModel;
916
1157
  provider.video = createVideoModel;
917
1158
  provider.videoModel = createVideoModel;
1159
+ provider.transcription = createTranscriptionModel;
1160
+ provider.transcriptionModel = createTranscriptionModel;
918
1161
  provider.tools = googleVertexTools;
919
1162
  return provider;
920
1163
  }
@@ -1045,7 +1288,7 @@ function createVertex2(options = {}) {
1045
1288
  Authorization: `Bearer ${await generateAuthToken(
1046
1289
  options.googleCredentials
1047
1290
  )}`,
1048
- ...await resolve5(options.headers)
1291
+ ...await resolve6(options.headers)
1049
1292
  })
1050
1293
  });
1051
1294
  }