@ai-sdk/google 3.0.140 → 3.0.142

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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @ai-sdk/google
2
2
 
3
+ ## 3.0.142
4
+
5
+ ### Patch Changes
6
+
7
+ - 5ba2f75: Omit Google provider-defined tools, including File Search and Vertex RAG Store, when `toolChoice` is `none` so disabled tools cannot retrieve data or execute server-side. Preserve function declarations needed by conversation history while disabling function calling.
8
+
9
+ ## 3.0.141
10
+
11
+ ### Patch Changes
12
+
13
+ - d900e58: fix(google): handle cycles and BigInt in functionResponse and parse string functionCall arguments
14
+ - d900e58: Omit all Google Interactions tools when `toolChoice` is `none`, including provider-defined tools.
15
+ - d900e58: Map Google RPC status names to HTTP status codes when streamed errors omit a numeric code, so transient failures such as `UNAVAILABLE` and `RESOURCE_EXHAUSTED` are marked retryable.
16
+ - Updated dependencies [d900e58]
17
+ - Updated dependencies [d900e58]
18
+ - Updated dependencies [d900e58]
19
+ - @ai-sdk/provider-utils@4.0.64
20
+
3
21
  ## 3.0.140
4
22
 
5
23
  ### Patch Changes
package/dist/index.js CHANGED
@@ -35,15 +35,16 @@ var import_v43 = require("zod/v4");
35
35
  // src/google-error.ts
36
36
  var import_provider_utils = require("@ai-sdk/provider-utils");
37
37
  var import_v4 = require("zod/v4");
38
+ var getGoogleErrorSchema = () => import_v4.z.object({
39
+ code: import_v4.z.number().nullable(),
40
+ message: import_v4.z.string(),
41
+ status: import_v4.z.string(),
42
+ details: import_v4.z.array(import_v4.z.unknown()).nullish()
43
+ });
38
44
  var googleErrorDataSchema = (0, import_provider_utils.lazySchema)(
39
45
  () => (0, import_provider_utils.zodSchema)(
40
46
  import_v4.z.object({
41
- error: import_v4.z.object({
42
- code: import_v4.z.number().nullable(),
43
- message: import_v4.z.string(),
44
- status: import_v4.z.string(),
45
- details: import_v4.z.array(import_v4.z.unknown()).nullish()
46
- })
47
+ error: getGoogleErrorSchema()
47
48
  })
48
49
  )
49
50
  );
@@ -51,6 +52,39 @@ var googleFailedResponseHandler = (0, import_provider_utils.createJsonErrorRespo
51
52
  errorSchema: googleErrorDataSchema,
52
53
  errorToMessage: (data) => data.error.message
53
54
  });
55
+ function mapGoogleErrorStatusToHttpStatusCode(status) {
56
+ switch (status) {
57
+ case "CANCELLED":
58
+ return 499;
59
+ case "INVALID_ARGUMENT":
60
+ case "FAILED_PRECONDITION":
61
+ case "OUT_OF_RANGE":
62
+ return 400;
63
+ case "DEADLINE_EXCEEDED":
64
+ return 504;
65
+ case "NOT_FOUND":
66
+ return 404;
67
+ case "ALREADY_EXISTS":
68
+ case "ABORTED":
69
+ return 409;
70
+ case "PERMISSION_DENIED":
71
+ return 403;
72
+ case "UNAUTHENTICATED":
73
+ return 401;
74
+ case "RESOURCE_EXHAUSTED":
75
+ return 429;
76
+ case "UNIMPLEMENTED":
77
+ return 501;
78
+ case "UNKNOWN":
79
+ case "INTERNAL":
80
+ case "DATA_LOSS":
81
+ return 500;
82
+ case "UNAVAILABLE":
83
+ return 503;
84
+ default:
85
+ return void 0;
86
+ }
87
+ }
54
88
 
55
89
  // src/transcription/google-transcription-model-options.ts
56
90
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
@@ -244,7 +278,7 @@ var googleInteractionsTranscriptionResponseSchema = (0, import_provider_utils3.l
244
278
  var import_provider_utils26 = require("@ai-sdk/provider-utils");
245
279
 
246
280
  // src/version.ts
247
- var VERSION = true ? "3.0.140" : "0.0.0-test";
281
+ var VERSION = true ? "3.0.142" : "0.0.0-test";
248
282
 
249
283
  // src/google-generative-ai-embedding-model.ts
250
284
  var import_provider2 = require("@ai-sdk/provider");
@@ -466,6 +500,7 @@ function validateMultimodalContentLength({
466
500
  }
467
501
 
468
502
  // src/google-generative-ai-language-model.ts
503
+ var import_provider6 = require("@ai-sdk/provider");
469
504
  var import_provider_utils9 = require("@ai-sdk/provider-utils");
470
505
  var import_v48 = require("zod/v4");
471
506
 
@@ -952,6 +987,31 @@ function convertUrlToolResultPart(url, mediaType, supportedFunctionResponseUrls)
952
987
  }
953
988
  } : void 0;
954
989
  }
990
+ function serializeFunctionResponseContent(value) {
991
+ return containsNonJsonValue(value) ? (0, import_provider_utils6.secureJsonParse)((0, import_provider_utils6.stringifyWithBigIntAndCycles)(value)) : value;
992
+ }
993
+ function containsNonJsonValue(value, ancestors = /* @__PURE__ */ new Set()) {
994
+ if (typeof value === "bigint") {
995
+ return true;
996
+ }
997
+ if (typeof value !== "object" || value === null) {
998
+ return false;
999
+ }
1000
+ if (typeof value.toJSON === "function") {
1001
+ return true;
1002
+ }
1003
+ if (ancestors.has(value)) {
1004
+ return true;
1005
+ }
1006
+ ancestors.add(value);
1007
+ try {
1008
+ return Object.values(value).some(
1009
+ (item) => containsNonJsonValue(item, ancestors)
1010
+ );
1011
+ } finally {
1012
+ ancestors.delete(value);
1013
+ }
1014
+ }
955
1015
  function appendToolResultParts(parts, toolName, outputValue, toolCallId, includeFunctionCallIds = true, supportedFunctionResponseUrls = {}) {
956
1016
  const functionResponseParts = [];
957
1017
  const responseTextParts = [];
@@ -981,12 +1041,12 @@ function appendToolResultParts(parts, toolName, outputValue, toolCallId, include
981
1041
  if (functionResponsePart != null) {
982
1042
  functionResponseParts.push(functionResponsePart);
983
1043
  } else {
984
- responseTextParts.push(JSON.stringify(contentPart));
1044
+ responseTextParts.push((0, import_provider_utils6.stringifyWithBigIntAndCycles)(contentPart));
985
1045
  }
986
1046
  break;
987
1047
  }
988
1048
  default: {
989
- responseTextParts.push(JSON.stringify(contentPart));
1049
+ responseTextParts.push((0, import_provider_utils6.stringifyWithBigIntAndCycles)(contentPart));
990
1050
  break;
991
1051
  }
992
1052
  }
@@ -1035,7 +1095,7 @@ function appendLegacyToolResultParts(parts, toolName, outputValue, toolCallId, i
1035
1095
  break;
1036
1096
  }
1037
1097
  default:
1038
- parts.push({ text: JSON.stringify(contentPart) });
1098
+ parts.push({ text: (0, import_provider_utils6.stringifyWithBigIntAndCycles)(contentPart) });
1039
1099
  break;
1040
1100
  }
1041
1101
  }
@@ -1177,7 +1237,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
1177
1237
  functionCall: {
1178
1238
  ...includeFunctionCallIds && part.toolCallId != null ? { id: part.toolCallId } : {},
1179
1239
  name: part.toolName,
1180
- args: part.input
1240
+ args: parseToolCallInput(part.input)
1181
1241
  },
1182
1242
  thoughtSignature: effectiveThoughtSignature
1183
1243
  };
@@ -1259,7 +1319,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
1259
1319
  name: part.toolName,
1260
1320
  response: {
1261
1321
  name: part.toolName,
1262
- content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool call execution denied." : output.value
1322
+ content: output.type === "execution-denied" ? (_h = output.reason) != null ? _h : "Tool call execution denied." : serializeFunctionResponseContent(output.value)
1263
1323
  }
1264
1324
  }
1265
1325
  });
@@ -1289,6 +1349,16 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
1289
1349
  contents
1290
1350
  };
1291
1351
  }
1352
+ function parseToolCallInput(input) {
1353
+ if (typeof input !== "string") {
1354
+ return input;
1355
+ }
1356
+ try {
1357
+ return (0, import_provider_utils6.secureJsonParse)(input);
1358
+ } catch (e) {
1359
+ return input;
1360
+ }
1361
+ }
1292
1362
 
1293
1363
  // src/download-tool-result-files.ts
1294
1364
  var import_provider_utils7 = require("@ai-sdk/provider-utils");
@@ -1588,6 +1658,9 @@ function prepareTools({
1588
1658
  modelId,
1589
1659
  isVertexProvider = false
1590
1660
  }) {
1661
+ if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
1662
+ tools = tools == null ? void 0 : tools.filter((tool) => tool.type === "function");
1663
+ }
1591
1664
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
1592
1665
  const toolWarnings = [];
1593
1666
  const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
@@ -2583,10 +2656,11 @@ var GoogleGenerativeAILanguageModel = class {
2583
2656
  options.headers,
2584
2657
  extraHeaders
2585
2658
  );
2659
+ const url = `${this.config.baseURL}/${getModelPath(
2660
+ this.modelId
2661
+ )}:streamGenerateContent?alt=sse`;
2586
2662
  const streamGenerateContent = (body) => (0, import_provider_utils9.postJsonToApi)({
2587
- url: `${this.config.baseURL}/${getModelPath(
2588
- this.modelId
2589
- )}:streamGenerateContent?alt=sse`,
2663
+ url,
2590
2664
  headers,
2591
2665
  body,
2592
2666
  failedResponseHandler: googleFailedResponseHandler,
@@ -2676,7 +2750,7 @@ var GoogleGenerativeAILanguageModel = class {
2676
2750
  controller.enqueue({ type: "stream-start", warnings });
2677
2751
  },
2678
2752
  transform(chunk, controller) {
2679
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2753
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
2680
2754
  if (options.includeRawChunks) {
2681
2755
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2682
2756
  }
@@ -2685,6 +2759,23 @@ var GoogleGenerativeAILanguageModel = class {
2685
2759
  return;
2686
2760
  }
2687
2761
  const value = chunk.value;
2762
+ if (value.error != null) {
2763
+ finishReason = { unified: "error", raw: void 0 };
2764
+ const error = value.error;
2765
+ controller.enqueue({
2766
+ type: "error",
2767
+ error: new import_provider6.APICallError({
2768
+ message: error.message,
2769
+ url,
2770
+ requestBodyValues: args,
2771
+ statusCode: (_a2 = error.code) != null ? _a2 : mapGoogleErrorStatusToHttpStatusCode(error.status),
2772
+ responseHeaders,
2773
+ responseBody: JSON.stringify({ error }),
2774
+ data: error
2775
+ })
2776
+ });
2777
+ return;
2778
+ }
2688
2779
  if (!hasEmittedResponseMetadata && value.responseId != null) {
2689
2780
  hasEmittedResponseMetadata = true;
2690
2781
  controller.enqueue({
@@ -2706,7 +2797,7 @@ var GoogleGenerativeAILanguageModel = class {
2706
2797
  };
2707
2798
  }
2708
2799
  }
2709
- const candidate = (_a2 = value.candidates) == null ? void 0 : _a2[0];
2800
+ const candidate = (_b = value.candidates) == null ? void 0 : _b[0];
2710
2801
  if (candidate != null) {
2711
2802
  if (candidate.groundingMetadata != null) {
2712
2803
  lastGroundingMetadata = candidate.groundingMetadata;
@@ -2741,9 +2832,9 @@ var GoogleGenerativeAILanguageModel = class {
2741
2832
  }
2742
2833
  }
2743
2834
  if (content != null) {
2744
- const parts = (_b = content.parts) != null ? _b : [];
2835
+ const parts = (_c = content.parts) != null ? _c : [];
2745
2836
  for (const part of parts) {
2746
- if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
2837
+ if ("executableCode" in part && ((_d = part.executableCode) == null ? void 0 : _d.code)) {
2747
2838
  const toolCallId = generateId3();
2748
2839
  lastCodeExecutionToolCallId = toolCallId;
2749
2840
  controller.enqueue({
@@ -2762,7 +2853,7 @@ var GoogleGenerativeAILanguageModel = class {
2762
2853
  toolName: "code_execution",
2763
2854
  result: {
2764
2855
  outcome: part.codeExecutionResult.outcome,
2765
- output: (_d = part.codeExecutionResult.output) != null ? _d : ""
2856
+ output: (_e = part.codeExecutionResult.output) != null ? _e : ""
2766
2857
  }
2767
2858
  });
2768
2859
  }
@@ -2856,7 +2947,7 @@ var GoogleGenerativeAILanguageModel = class {
2856
2947
  providerMetadata: fileMeta
2857
2948
  });
2858
2949
  } else if ("toolCall" in part && part.toolCall) {
2859
- const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId3();
2950
+ const toolCallId = (_f = part.toolCall.id) != null ? _f : generateId3();
2860
2951
  lastServerToolCallId = toolCallId;
2861
2952
  const serverMeta = {
2862
2953
  [providerOptionsName]: {
@@ -2869,13 +2960,13 @@ var GoogleGenerativeAILanguageModel = class {
2869
2960
  type: "tool-call",
2870
2961
  toolCallId,
2871
2962
  toolName: `server:${part.toolCall.toolType}`,
2872
- input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
2963
+ input: JSON.stringify((_g = part.toolCall.args) != null ? _g : {}),
2873
2964
  providerExecuted: true,
2874
2965
  dynamic: true,
2875
2966
  providerMetadata: serverMeta
2876
2967
  });
2877
2968
  } else if ("toolResponse" in part && part.toolResponse) {
2878
- const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId3();
2969
+ const responseToolCallId = (_h = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _h : generateId3();
2879
2970
  const serverMeta = {
2880
2971
  [providerOptionsName]: {
2881
2972
  ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
@@ -2887,7 +2978,7 @@ var GoogleGenerativeAILanguageModel = class {
2887
2978
  type: "tool-result",
2888
2979
  toolCallId: responseToolCallId,
2889
2980
  toolName: `server:${part.toolResponse.toolType}`,
2890
- result: (_h = part.toolResponse.response) != null ? _h : {},
2981
+ result: (_i = part.toolResponse.response) != null ? _i : {},
2891
2982
  providerMetadata: serverMeta
2892
2983
  });
2893
2984
  lastServerToolCallId = void 0;
@@ -2906,7 +2997,7 @@ var GoogleGenerativeAILanguageModel = class {
2906
2997
  const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
2907
2998
  if (isStreamingChunk) {
2908
2999
  if (part.functionCall.name != null) {
2909
- const toolCallId = (_i = part.functionCall.id) != null ? _i : generateId3();
3000
+ const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId3();
2910
3001
  const accumulator = new GoogleJSONAccumulator();
2911
3002
  activeStreamingToolCalls.push({
2912
3003
  toolCallId,
@@ -2954,9 +3045,9 @@ var GoogleGenerativeAILanguageModel = class {
2954
3045
  } else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
2955
3046
  finishActiveStreamingToolCall(controller);
2956
3047
  } else if (isCompleteCall) {
2957
- const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId3();
3048
+ const toolCallId = (_k = part.functionCall.id) != null ? _k : generateId3();
2958
3049
  const toolName = part.functionCall.name;
2959
- const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_k = part.functionCall.args) != null ? _k : {});
3050
+ const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_l = part.functionCall.args) != null ? _l : {});
2960
3051
  controller.enqueue({
2961
3052
  type: "tool-input-start",
2962
3053
  id: toolCallId,
@@ -2983,7 +3074,7 @@ var GoogleGenerativeAILanguageModel = class {
2983
3074
  });
2984
3075
  hasToolCalls = true;
2985
3076
  } else if (isNoArgsCompleteCall) {
2986
- const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId3();
3077
+ const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId3();
2987
3078
  const toolName = part.functionCall.name;
2988
3079
  controller.enqueue({
2989
3080
  type: "tool-input-start",
@@ -3354,6 +3445,7 @@ var responseSchema = (0, import_provider_utils9.lazySchema)(
3354
3445
  var chunkSchema = (0, import_provider_utils9.lazySchema)(
3355
3446
  () => (0, import_provider_utils9.zodSchema)(
3356
3447
  import_v48.z.object({
3448
+ error: getGoogleErrorSchema().nullish(),
3357
3449
  responseId: import_v48.z.string().nullish(),
3358
3450
  candidates: import_v48.z.array(
3359
3451
  import_v48.z.object({
@@ -3826,7 +3918,7 @@ var googleImageModelOptionsSchema = (0, import_provider_utils17.lazySchema)(
3826
3918
  );
3827
3919
 
3828
3920
  // src/google-generative-ai-video-model.ts
3829
- var import_provider6 = require("@ai-sdk/provider");
3921
+ var import_provider7 = require("@ai-sdk/provider");
3830
3922
  var import_provider_utils18 = require("@ai-sdk/provider-utils");
3831
3923
  var import_v417 = require("zod/v4");
3832
3924
  function getFirstFrameImage(options) {
@@ -4001,7 +4093,7 @@ var GoogleGenerativeAIVideoModel = class {
4001
4093
  });
4002
4094
  const operationName = operation.name;
4003
4095
  if (!operationName) {
4004
- throw new import_provider6.AISDKError({
4096
+ throw new import_provider7.AISDKError({
4005
4097
  name: "GOOGLE_VIDEO_GENERATION_ERROR",
4006
4098
  message: "No operation name returned from API"
4007
4099
  });
@@ -4013,14 +4105,14 @@ var GoogleGenerativeAIVideoModel = class {
4013
4105
  let responseHeaders;
4014
4106
  while (!finalOperation.done) {
4015
4107
  if (Date.now() - startTime > pollTimeoutMs) {
4016
- throw new import_provider6.AISDKError({
4108
+ throw new import_provider7.AISDKError({
4017
4109
  name: "GOOGLE_VIDEO_GENERATION_TIMEOUT",
4018
4110
  message: `Video generation timed out after ${pollTimeoutMs}ms`
4019
4111
  });
4020
4112
  }
4021
4113
  await (0, import_provider_utils18.delay)(pollIntervalMs);
4022
4114
  if ((_f = options.abortSignal) == null ? void 0 : _f.aborted) {
4023
- throw new import_provider6.AISDKError({
4115
+ throw new import_provider7.AISDKError({
4024
4116
  name: "GOOGLE_VIDEO_GENERATION_ABORTED",
4025
4117
  message: "Video generation request was aborted"
4026
4118
  });
@@ -4042,14 +4134,14 @@ var GoogleGenerativeAIVideoModel = class {
4042
4134
  responseHeaders = pollHeaders;
4043
4135
  }
4044
4136
  if (finalOperation.error) {
4045
- throw new import_provider6.AISDKError({
4137
+ throw new import_provider7.AISDKError({
4046
4138
  name: "GOOGLE_VIDEO_GENERATION_FAILED",
4047
4139
  message: `Video generation failed: ${finalOperation.error.message}`
4048
4140
  });
4049
4141
  }
4050
4142
  const response = finalOperation.response;
4051
4143
  if (!((_g = response == null ? void 0 : response.generateVideoResponse) == null ? void 0 : _g.generatedSamples) || response.generateVideoResponse.generatedSamples.length === 0) {
4052
- throw new import_provider6.AISDKError({
4144
+ throw new import_provider7.AISDKError({
4053
4145
  name: "GOOGLE_VIDEO_GENERATION_ERROR",
4054
4146
  message: `No videos in response. Response: ${JSON.stringify(finalOperation)}`
4055
4147
  });
@@ -4072,7 +4164,7 @@ var GoogleGenerativeAIVideoModel = class {
4072
4164
  }
4073
4165
  }
4074
4166
  if (videos.length === 0) {
4075
- throw new import_provider6.AISDKError({
4167
+ throw new import_provider7.AISDKError({
4076
4168
  name: "GOOGLE_VIDEO_GENERATION_ERROR",
4077
4169
  message: "No valid videos in response"
4078
4170
  });
@@ -6109,6 +6201,9 @@ function prepareGoogleInteractionsTools({
6109
6201
  }) {
6110
6202
  var _a2, _b, _c, _d;
6111
6203
  const toolWarnings = [];
6204
+ if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
6205
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
6206
+ }
6112
6207
  const normalized = (tools == null ? void 0 : tools.length) ? tools : void 0;
6113
6208
  if (normalized == null) {
6114
6209
  return { tools: void 0, toolChoice: void 0, toolWarnings };
@@ -6230,9 +6325,6 @@ function prepareGoogleInteractionsTools({
6230
6325
  case "required":
6231
6326
  mappedToolChoice = "any";
6232
6327
  break;
6233
- case "none":
6234
- mappedToolChoice = "none";
6235
- break;
6236
6328
  case "tool":
6237
6329
  mappedToolChoice = {
6238
6330
  allowed_tools: {