@ai-sdk/google 3.0.140 → 3.0.141

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,17 @@
1
1
  # @ai-sdk/google
2
2
 
3
+ ## 3.0.141
4
+
5
+ ### Patch Changes
6
+
7
+ - d900e58: fix(google): handle cycles and BigInt in functionResponse and parse string functionCall arguments
8
+ - d900e58: Omit all Google Interactions tools when `toolChoice` is `none`, including provider-defined tools.
9
+ - 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.
10
+ - Updated dependencies [d900e58]
11
+ - Updated dependencies [d900e58]
12
+ - Updated dependencies [d900e58]
13
+ - @ai-sdk/provider-utils@4.0.64
14
+
3
15
  ## 3.0.140
4
16
 
5
17
  ### 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.141" : "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");
@@ -2583,10 +2653,11 @@ var GoogleGenerativeAILanguageModel = class {
2583
2653
  options.headers,
2584
2654
  extraHeaders
2585
2655
  );
2656
+ const url = `${this.config.baseURL}/${getModelPath(
2657
+ this.modelId
2658
+ )}:streamGenerateContent?alt=sse`;
2586
2659
  const streamGenerateContent = (body) => (0, import_provider_utils9.postJsonToApi)({
2587
- url: `${this.config.baseURL}/${getModelPath(
2588
- this.modelId
2589
- )}:streamGenerateContent?alt=sse`,
2660
+ url,
2590
2661
  headers,
2591
2662
  body,
2592
2663
  failedResponseHandler: googleFailedResponseHandler,
@@ -2676,7 +2747,7 @@ var GoogleGenerativeAILanguageModel = class {
2676
2747
  controller.enqueue({ type: "stream-start", warnings });
2677
2748
  },
2678
2749
  transform(chunk, controller) {
2679
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2750
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
2680
2751
  if (options.includeRawChunks) {
2681
2752
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2682
2753
  }
@@ -2685,6 +2756,23 @@ var GoogleGenerativeAILanguageModel = class {
2685
2756
  return;
2686
2757
  }
2687
2758
  const value = chunk.value;
2759
+ if (value.error != null) {
2760
+ finishReason = { unified: "error", raw: void 0 };
2761
+ const error = value.error;
2762
+ controller.enqueue({
2763
+ type: "error",
2764
+ error: new import_provider6.APICallError({
2765
+ message: error.message,
2766
+ url,
2767
+ requestBodyValues: args,
2768
+ statusCode: (_a2 = error.code) != null ? _a2 : mapGoogleErrorStatusToHttpStatusCode(error.status),
2769
+ responseHeaders,
2770
+ responseBody: JSON.stringify({ error }),
2771
+ data: error
2772
+ })
2773
+ });
2774
+ return;
2775
+ }
2688
2776
  if (!hasEmittedResponseMetadata && value.responseId != null) {
2689
2777
  hasEmittedResponseMetadata = true;
2690
2778
  controller.enqueue({
@@ -2706,7 +2794,7 @@ var GoogleGenerativeAILanguageModel = class {
2706
2794
  };
2707
2795
  }
2708
2796
  }
2709
- const candidate = (_a2 = value.candidates) == null ? void 0 : _a2[0];
2797
+ const candidate = (_b = value.candidates) == null ? void 0 : _b[0];
2710
2798
  if (candidate != null) {
2711
2799
  if (candidate.groundingMetadata != null) {
2712
2800
  lastGroundingMetadata = candidate.groundingMetadata;
@@ -2741,9 +2829,9 @@ var GoogleGenerativeAILanguageModel = class {
2741
2829
  }
2742
2830
  }
2743
2831
  if (content != null) {
2744
- const parts = (_b = content.parts) != null ? _b : [];
2832
+ const parts = (_c = content.parts) != null ? _c : [];
2745
2833
  for (const part of parts) {
2746
- if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
2834
+ if ("executableCode" in part && ((_d = part.executableCode) == null ? void 0 : _d.code)) {
2747
2835
  const toolCallId = generateId3();
2748
2836
  lastCodeExecutionToolCallId = toolCallId;
2749
2837
  controller.enqueue({
@@ -2762,7 +2850,7 @@ var GoogleGenerativeAILanguageModel = class {
2762
2850
  toolName: "code_execution",
2763
2851
  result: {
2764
2852
  outcome: part.codeExecutionResult.outcome,
2765
- output: (_d = part.codeExecutionResult.output) != null ? _d : ""
2853
+ output: (_e = part.codeExecutionResult.output) != null ? _e : ""
2766
2854
  }
2767
2855
  });
2768
2856
  }
@@ -2856,7 +2944,7 @@ var GoogleGenerativeAILanguageModel = class {
2856
2944
  providerMetadata: fileMeta
2857
2945
  });
2858
2946
  } else if ("toolCall" in part && part.toolCall) {
2859
- const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId3();
2947
+ const toolCallId = (_f = part.toolCall.id) != null ? _f : generateId3();
2860
2948
  lastServerToolCallId = toolCallId;
2861
2949
  const serverMeta = {
2862
2950
  [providerOptionsName]: {
@@ -2869,13 +2957,13 @@ var GoogleGenerativeAILanguageModel = class {
2869
2957
  type: "tool-call",
2870
2958
  toolCallId,
2871
2959
  toolName: `server:${part.toolCall.toolType}`,
2872
- input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
2960
+ input: JSON.stringify((_g = part.toolCall.args) != null ? _g : {}),
2873
2961
  providerExecuted: true,
2874
2962
  dynamic: true,
2875
2963
  providerMetadata: serverMeta
2876
2964
  });
2877
2965
  } else if ("toolResponse" in part && part.toolResponse) {
2878
- const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId3();
2966
+ const responseToolCallId = (_h = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _h : generateId3();
2879
2967
  const serverMeta = {
2880
2968
  [providerOptionsName]: {
2881
2969
  ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
@@ -2887,7 +2975,7 @@ var GoogleGenerativeAILanguageModel = class {
2887
2975
  type: "tool-result",
2888
2976
  toolCallId: responseToolCallId,
2889
2977
  toolName: `server:${part.toolResponse.toolType}`,
2890
- result: (_h = part.toolResponse.response) != null ? _h : {},
2978
+ result: (_i = part.toolResponse.response) != null ? _i : {},
2891
2979
  providerMetadata: serverMeta
2892
2980
  });
2893
2981
  lastServerToolCallId = void 0;
@@ -2906,7 +2994,7 @@ var GoogleGenerativeAILanguageModel = class {
2906
2994
  const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
2907
2995
  if (isStreamingChunk) {
2908
2996
  if (part.functionCall.name != null) {
2909
- const toolCallId = (_i = part.functionCall.id) != null ? _i : generateId3();
2997
+ const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId3();
2910
2998
  const accumulator = new GoogleJSONAccumulator();
2911
2999
  activeStreamingToolCalls.push({
2912
3000
  toolCallId,
@@ -2954,9 +3042,9 @@ var GoogleGenerativeAILanguageModel = class {
2954
3042
  } else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
2955
3043
  finishActiveStreamingToolCall(controller);
2956
3044
  } else if (isCompleteCall) {
2957
- const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId3();
3045
+ const toolCallId = (_k = part.functionCall.id) != null ? _k : generateId3();
2958
3046
  const toolName = part.functionCall.name;
2959
- const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_k = part.functionCall.args) != null ? _k : {});
3047
+ const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_l = part.functionCall.args) != null ? _l : {});
2960
3048
  controller.enqueue({
2961
3049
  type: "tool-input-start",
2962
3050
  id: toolCallId,
@@ -2983,7 +3071,7 @@ var GoogleGenerativeAILanguageModel = class {
2983
3071
  });
2984
3072
  hasToolCalls = true;
2985
3073
  } else if (isNoArgsCompleteCall) {
2986
- const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId3();
3074
+ const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId3();
2987
3075
  const toolName = part.functionCall.name;
2988
3076
  controller.enqueue({
2989
3077
  type: "tool-input-start",
@@ -3354,6 +3442,7 @@ var responseSchema = (0, import_provider_utils9.lazySchema)(
3354
3442
  var chunkSchema = (0, import_provider_utils9.lazySchema)(
3355
3443
  () => (0, import_provider_utils9.zodSchema)(
3356
3444
  import_v48.z.object({
3445
+ error: getGoogleErrorSchema().nullish(),
3357
3446
  responseId: import_v48.z.string().nullish(),
3358
3447
  candidates: import_v48.z.array(
3359
3448
  import_v48.z.object({
@@ -3826,7 +3915,7 @@ var googleImageModelOptionsSchema = (0, import_provider_utils17.lazySchema)(
3826
3915
  );
3827
3916
 
3828
3917
  // src/google-generative-ai-video-model.ts
3829
- var import_provider6 = require("@ai-sdk/provider");
3918
+ var import_provider7 = require("@ai-sdk/provider");
3830
3919
  var import_provider_utils18 = require("@ai-sdk/provider-utils");
3831
3920
  var import_v417 = require("zod/v4");
3832
3921
  function getFirstFrameImage(options) {
@@ -4001,7 +4090,7 @@ var GoogleGenerativeAIVideoModel = class {
4001
4090
  });
4002
4091
  const operationName = operation.name;
4003
4092
  if (!operationName) {
4004
- throw new import_provider6.AISDKError({
4093
+ throw new import_provider7.AISDKError({
4005
4094
  name: "GOOGLE_VIDEO_GENERATION_ERROR",
4006
4095
  message: "No operation name returned from API"
4007
4096
  });
@@ -4013,14 +4102,14 @@ var GoogleGenerativeAIVideoModel = class {
4013
4102
  let responseHeaders;
4014
4103
  while (!finalOperation.done) {
4015
4104
  if (Date.now() - startTime > pollTimeoutMs) {
4016
- throw new import_provider6.AISDKError({
4105
+ throw new import_provider7.AISDKError({
4017
4106
  name: "GOOGLE_VIDEO_GENERATION_TIMEOUT",
4018
4107
  message: `Video generation timed out after ${pollTimeoutMs}ms`
4019
4108
  });
4020
4109
  }
4021
4110
  await (0, import_provider_utils18.delay)(pollIntervalMs);
4022
4111
  if ((_f = options.abortSignal) == null ? void 0 : _f.aborted) {
4023
- throw new import_provider6.AISDKError({
4112
+ throw new import_provider7.AISDKError({
4024
4113
  name: "GOOGLE_VIDEO_GENERATION_ABORTED",
4025
4114
  message: "Video generation request was aborted"
4026
4115
  });
@@ -4042,14 +4131,14 @@ var GoogleGenerativeAIVideoModel = class {
4042
4131
  responseHeaders = pollHeaders;
4043
4132
  }
4044
4133
  if (finalOperation.error) {
4045
- throw new import_provider6.AISDKError({
4134
+ throw new import_provider7.AISDKError({
4046
4135
  name: "GOOGLE_VIDEO_GENERATION_FAILED",
4047
4136
  message: `Video generation failed: ${finalOperation.error.message}`
4048
4137
  });
4049
4138
  }
4050
4139
  const response = finalOperation.response;
4051
4140
  if (!((_g = response == null ? void 0 : response.generateVideoResponse) == null ? void 0 : _g.generatedSamples) || response.generateVideoResponse.generatedSamples.length === 0) {
4052
- throw new import_provider6.AISDKError({
4141
+ throw new import_provider7.AISDKError({
4053
4142
  name: "GOOGLE_VIDEO_GENERATION_ERROR",
4054
4143
  message: `No videos in response. Response: ${JSON.stringify(finalOperation)}`
4055
4144
  });
@@ -4072,7 +4161,7 @@ var GoogleGenerativeAIVideoModel = class {
4072
4161
  }
4073
4162
  }
4074
4163
  if (videos.length === 0) {
4075
- throw new import_provider6.AISDKError({
4164
+ throw new import_provider7.AISDKError({
4076
4165
  name: "GOOGLE_VIDEO_GENERATION_ERROR",
4077
4166
  message: "No valid videos in response"
4078
4167
  });
@@ -6109,6 +6198,9 @@ function prepareGoogleInteractionsTools({
6109
6198
  }) {
6110
6199
  var _a2, _b, _c, _d;
6111
6200
  const toolWarnings = [];
6201
+ if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
6202
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
6203
+ }
6112
6204
  const normalized = (tools == null ? void 0 : tools.length) ? tools : void 0;
6113
6205
  if (normalized == null) {
6114
6206
  return { tools: void 0, toolChoice: void 0, toolWarnings };
@@ -6230,9 +6322,6 @@ function prepareGoogleInteractionsTools({
6230
6322
  case "required":
6231
6323
  mappedToolChoice = "any";
6232
6324
  break;
6233
- case "none":
6234
- mappedToolChoice = "none";
6235
- break;
6236
6325
  case "tool":
6237
6326
  mappedToolChoice = {
6238
6327
  allowed_tools: {