@ai-sdk/google 2.0.90 → 2.0.94

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
@@ -7,7 +7,7 @@ import {
7
7
  } from "@ai-sdk/provider-utils";
8
8
 
9
9
  // src/version.ts
10
- var VERSION = true ? "2.0.90" : "0.0.0-test";
10
+ var VERSION = true ? "2.0.94" : "0.0.0-test";
11
11
 
12
12
  // src/google-generative-ai-embedding-model.ts
13
13
  import {
@@ -249,6 +249,10 @@ import { z as z5 } from "zod/v4";
249
249
  import {
250
250
  UnsupportedFunctionalityError
251
251
  } from "@ai-sdk/provider";
252
+ var recursiveReferenceFunctionalityPrefix = "recursive JSON Schema reference:";
253
+ function isRecursiveJSONSchemaReferenceError(error) {
254
+ return UnsupportedFunctionalityError.isInstance(error) && error.functionality.startsWith(recursiveReferenceFunctionalityPrefix);
255
+ }
252
256
  function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
253
257
  const rootSchema = typeof jsonSchema === "object" ? jsonSchema : void 0;
254
258
  return convertJSONSchemaDefinition(jsonSchema, isRoot, {
@@ -299,9 +303,6 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
299
303
  if (description) result.description = description;
300
304
  if (required) result.required = required;
301
305
  if (format) result.format = format;
302
- if (constValue !== void 0) {
303
- result.enum = [constValue];
304
- }
305
306
  if (type) {
306
307
  if (Array.isArray(type)) {
307
308
  const hasNull = type.includes("null");
@@ -318,8 +319,9 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
318
319
  result.type = type;
319
320
  }
320
321
  }
321
- if (enumValues !== void 0) {
322
- result.enum = enumValues;
322
+ const values = enumValues != null ? enumValues : constValue !== void 0 ? [constValue] : void 0;
323
+ if (values !== void 0) {
324
+ addEnumToSchema({ values, type, result });
323
325
  }
324
326
  if (properties != null) {
325
327
  result.properties = Object.entries(properties).reduce(
@@ -379,6 +381,68 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
379
381
  }
380
382
  return result;
381
383
  }
384
+ function addEnumToSchema({
385
+ values,
386
+ type,
387
+ result
388
+ }) {
389
+ const nullable = Array.isArray(type) && type.includes("null") || type === void 0 && values.includes(null);
390
+ const enumValues = nullable ? values.filter((value) => value !== null) : values;
391
+ if (values.length > 0 && values.every((value) => value === null)) {
392
+ const typeAllowsNull = type === void 0 || type === "null" || Array.isArray(type) && type.includes("null");
393
+ if (typeAllowsNull) {
394
+ result.type = "null";
395
+ if (Array.isArray(type)) {
396
+ delete result.anyOf;
397
+ }
398
+ return;
399
+ }
400
+ }
401
+ const enumType = getEnumType({ values: enumValues, type });
402
+ if (enumType === void 0) {
403
+ throw new UnsupportedFunctionalityError({
404
+ functionality: "JSON Schema enum with mixed or unsupported values",
405
+ message: "Google does not support this JSON Schema enum. Enum values must share one supported primitive type and match the schema type."
406
+ });
407
+ }
408
+ result.type = enumType;
409
+ if (Array.isArray(type)) {
410
+ delete result.anyOf;
411
+ }
412
+ if (nullable) {
413
+ result.nullable = true;
414
+ }
415
+ if (enumType === "string") {
416
+ result.enum = enumValues;
417
+ } else {
418
+ result.format = "enum";
419
+ result.enum = enumValues.map(String);
420
+ }
421
+ }
422
+ function getEnumType({
423
+ values,
424
+ type
425
+ }) {
426
+ if (values.length === 0) {
427
+ return void 0;
428
+ }
429
+ const typeAllows = (enumType) => type === void 0 || type === enumType || Array.isArray(type) && type.includes(enumType);
430
+ if (typeAllows("string") && values.every((value) => typeof value === "string")) {
431
+ return "string";
432
+ }
433
+ if ((typeAllows("number") || typeAllows("integer")) && values.every((value) => typeof value === "number" && Number.isFinite(value))) {
434
+ if (typeAllows("number")) {
435
+ return "number";
436
+ }
437
+ if (values.every((value) => Number.isInteger(value))) {
438
+ return "integer";
439
+ }
440
+ }
441
+ if (typeAllows("boolean") && values.every((value) => typeof value === "boolean")) {
442
+ return "boolean";
443
+ }
444
+ return void 0;
445
+ }
382
446
  function convertJSONSchemaReference({
383
447
  jsonSchema,
384
448
  reference,
@@ -391,7 +455,7 @@ function convertJSONSchemaReference({
391
455
  );
392
456
  if (referenceContext.resolvingReferences.has(referenceKey)) {
393
457
  throw new UnsupportedFunctionalityError({
394
- functionality: `recursive JSON Schema reference: ${reference}`,
458
+ functionality: `${recursiveReferenceFunctionalityPrefix} ${reference}`,
395
459
  message: "Google schema conversion does not support recursive JSON Schema references."
396
460
  });
397
461
  }
@@ -884,7 +948,6 @@ function prepareTools({
884
948
  modelId,
885
949
  isVertexProvider = false
886
950
  }) {
887
- var _a, _b;
888
951
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
889
952
  const toolWarnings = [];
890
953
  const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
@@ -1005,11 +1068,7 @@ function prepareTools({
1005
1068
  const functionDeclarations2 = [];
1006
1069
  for (const tool of tools) {
1007
1070
  if (tool.type === "function") {
1008
- functionDeclarations2.push({
1009
- name: tool.name,
1010
- description: (_a = tool.description) != null ? _a : "",
1011
- parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
1012
- });
1071
+ functionDeclarations2.push(prepareFunctionDeclaration(tool));
1013
1072
  }
1014
1073
  }
1015
1074
  const combinedToolConfig = {
@@ -1053,11 +1112,7 @@ function prepareTools({
1053
1112
  for (const tool of tools) {
1054
1113
  switch (tool.type) {
1055
1114
  case "function":
1056
- functionDeclarations.push({
1057
- name: tool.name,
1058
- description: (_b = tool.description) != null ? _b : "",
1059
- parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
1060
- });
1115
+ functionDeclarations.push(prepareFunctionDeclaration(tool));
1061
1116
  if (tool.strict === true) {
1062
1117
  hasStrictTools = true;
1063
1118
  }
@@ -1121,6 +1176,27 @@ function prepareTools({
1121
1176
  }
1122
1177
  }
1123
1178
  }
1179
+ function prepareFunctionDeclaration(tool) {
1180
+ var _a;
1181
+ const declaration = {
1182
+ name: tool.name,
1183
+ description: (_a = tool.description) != null ? _a : ""
1184
+ };
1185
+ try {
1186
+ return {
1187
+ ...declaration,
1188
+ parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
1189
+ };
1190
+ } catch (error) {
1191
+ if (!isRecursiveJSONSchemaReferenceError(error)) {
1192
+ throw error;
1193
+ }
1194
+ return {
1195
+ ...declaration,
1196
+ parametersJsonSchema: tool.inputSchema
1197
+ };
1198
+ }
1199
+ }
1124
1200
 
1125
1201
  // src/map-google-generative-ai-finish-reason.ts
1126
1202
  function mapGoogleGenerativeAIFinishReason({
@@ -1156,6 +1232,7 @@ var configurableSafetySettingCategories = [
1156
1232
  "HARM_CATEGORY_HARASSMENT",
1157
1233
  "HARM_CATEGORY_SEXUALLY_EXPLICIT"
1158
1234
  ];
1235
+ var gemini25ModelPattern2 = /(^|\/)gemini-2\.5(?:[.-]|$)/i;
1159
1236
  var GoogleGenerativeAILanguageModel = class {
1160
1237
  constructor(modelId, config) {
1161
1238
  this.specificationVersion = "v2";
@@ -1233,6 +1310,19 @@ var GoogleGenerativeAILanguageModel = class {
1233
1310
  threshold: safetyThreshold
1234
1311
  })) : void 0;
1235
1312
  const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
1313
+ const isGemini25DeveloperApiModel = !isVertexProvider && gemini25ModelPattern2.test(this.modelId);
1314
+ if (isGemini25DeveloperApiModel && frequencyPenalty != null) {
1315
+ warnings.push({
1316
+ type: "unsupported-setting",
1317
+ setting: "frequencyPenalty"
1318
+ });
1319
+ }
1320
+ if (isGemini25DeveloperApiModel && presencePenalty != null) {
1321
+ warnings.push({
1322
+ type: "unsupported-setting",
1323
+ setting: "presencePenalty"
1324
+ });
1325
+ }
1236
1326
  const { usesGemini3Features } = getGoogleModelCapabilities(this.modelId);
1237
1327
  const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
1238
1328
  prompt,
@@ -1260,8 +1350,8 @@ var GoogleGenerativeAILanguageModel = class {
1260
1350
  temperature,
1261
1351
  topK,
1262
1352
  topP,
1263
- frequencyPenalty,
1264
- presencePenalty,
1353
+ frequencyPenalty: isGemini25DeveloperApiModel ? void 0 : frequencyPenalty,
1354
+ presencePenalty: isGemini25DeveloperApiModel ? void 0 : presencePenalty,
1265
1355
  stopSequences,
1266
1356
  seed,
1267
1357
  // response format:
@@ -1297,7 +1387,7 @@ var GoogleGenerativeAILanguageModel = class {
1297
1387
  };
1298
1388
  }
1299
1389
  async doGenerate(options) {
1300
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1390
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1301
1391
  const { args, warnings } = await this.getArgs(options);
1302
1392
  const body = JSON.stringify(args);
1303
1393
  const mergedHeaders = combineHeaders2(
@@ -1319,13 +1409,15 @@ var GoogleGenerativeAILanguageModel = class {
1319
1409
  abortSignal: options.abortSignal,
1320
1410
  fetch: this.config.fetch
1321
1411
  });
1322
- const candidate = response.candidates[0];
1412
+ const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
1413
+ const promptBlockReason = (_b = response.promptFeedback) == null ? void 0 : _b.blockReason;
1414
+ const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && promptBlockReason != null;
1323
1415
  const content = [];
1324
- const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
1416
+ const parts = (_d = (_c = candidate == null ? void 0 : candidate.content) == null ? void 0 : _c.parts) != null ? _d : [];
1325
1417
  const usageMetadata = response.usageMetadata;
1326
1418
  let lastCodeExecutionToolCallId;
1327
1419
  for (const part of parts) {
1328
- if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
1420
+ if ("executableCode" in part && ((_e = part.executableCode) == null ? void 0 : _e.code)) {
1329
1421
  const toolCallId = this.config.generateId();
1330
1422
  lastCodeExecutionToolCallId = toolCallId;
1331
1423
  content.push({
@@ -1366,7 +1458,7 @@ var GoogleGenerativeAILanguageModel = class {
1366
1458
  } else if ("functionCall" in part) {
1367
1459
  content.push({
1368
1460
  type: "tool-call",
1369
- toolCallId: (_d = part.functionCall.id) != null ? _d : this.config.generateId(),
1461
+ toolCallId: (_f = part.functionCall.id) != null ? _f : this.config.generateId(),
1370
1462
  toolName: part.functionCall.name,
1371
1463
  input: JSON.stringify(part.functionCall.args),
1372
1464
  providerMetadata: part.thoughtSignature ? { google: { thoughtSignature: part.thoughtSignature } } : void 0
@@ -1379,34 +1471,34 @@ var GoogleGenerativeAILanguageModel = class {
1379
1471
  });
1380
1472
  }
1381
1473
  }
1382
- const sources = (_e = extractSources({
1383
- groundingMetadata: candidate.groundingMetadata,
1474
+ const sources = (_g = extractSources({
1475
+ groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
1384
1476
  generateId: this.config.generateId
1385
- })) != null ? _e : [];
1477
+ })) != null ? _g : [];
1386
1478
  for (const source of sources) {
1387
1479
  content.push(source);
1388
1480
  }
1389
1481
  return {
1390
1482
  content,
1391
- finishReason: mapGoogleGenerativeAIFinishReason({
1392
- finishReason: candidate.finishReason,
1483
+ finishReason: isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
1484
+ finishReason: candidate == null ? void 0 : candidate.finishReason,
1393
1485
  hasToolCalls: content.some((part) => part.type === "tool-call")
1394
1486
  }),
1395
1487
  usage: {
1396
- inputTokens: (_f = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _f : void 0,
1397
- outputTokens: (_g = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _g : void 0,
1398
- totalTokens: (_h = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _h : void 0,
1399
- reasoningTokens: (_i = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _i : void 0,
1400
- cachedInputTokens: (_j = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _j : void 0
1488
+ inputTokens: (_h = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _h : void 0,
1489
+ outputTokens: (_i = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _i : void 0,
1490
+ totalTokens: (_j = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _j : void 0,
1491
+ reasoningTokens: (_k = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _k : void 0,
1492
+ cachedInputTokens: (_l = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _l : void 0
1401
1493
  },
1402
1494
  warnings,
1403
1495
  providerMetadata: {
1404
1496
  google: {
1405
- promptFeedback: (_k = response.promptFeedback) != null ? _k : null,
1406
- groundingMetadata: (_l = candidate.groundingMetadata) != null ? _l : null,
1407
- urlContextMetadata: (_m = candidate.urlContextMetadata) != null ? _m : null,
1408
- safetyRatings: (_n = candidate.safetyRatings) != null ? _n : null,
1409
- serviceTier: (_o = response.serviceTier) != null ? _o : null,
1497
+ promptFeedback: (_m = response.promptFeedback) != null ? _m : null,
1498
+ groundingMetadata: (_n = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _n : null,
1499
+ urlContextMetadata: (_o = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _o : null,
1500
+ safetyRatings: (_p = candidate == null ? void 0 : candidate.safetyRatings) != null ? _p : null,
1501
+ serviceTier: (_q = response.serviceTier) != null ? _q : null,
1410
1502
  usageMetadata: usageMetadata != null ? usageMetadata : null
1411
1503
  }
1412
1504
  },
@@ -1460,7 +1552,7 @@ var GoogleGenerativeAILanguageModel = class {
1460
1552
  controller.enqueue({ type: "stream-start", warnings });
1461
1553
  },
1462
1554
  transform(chunk, controller) {
1463
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1555
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1464
1556
  if (options.includeRawChunks) {
1465
1557
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1466
1558
  }
@@ -1482,6 +1574,22 @@ var GoogleGenerativeAILanguageModel = class {
1482
1574
  }
1483
1575
  const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
1484
1576
  if (candidate == null) {
1577
+ const promptBlockReason2 = (_g = value.promptFeedback) == null ? void 0 : _g.blockReason;
1578
+ if (promptBlockReason2 != null) {
1579
+ finishReason = "content-filter";
1580
+ providerMetadata = {
1581
+ google: {
1582
+ promptFeedback: (_h = value.promptFeedback) != null ? _h : null,
1583
+ groundingMetadata: lastGroundingMetadata,
1584
+ urlContextMetadata: lastUrlContextMetadata,
1585
+ safetyRatings: null,
1586
+ serviceTier
1587
+ }
1588
+ };
1589
+ if (usageMetadata != null) {
1590
+ providerMetadata.google.usageMetadata = usageMetadata;
1591
+ }
1592
+ }
1485
1593
  return;
1486
1594
  }
1487
1595
  const content = candidate.content;
@@ -1504,9 +1612,9 @@ var GoogleGenerativeAILanguageModel = class {
1504
1612
  }
1505
1613
  }
1506
1614
  if (content != null) {
1507
- const parts = (_g = content.parts) != null ? _g : [];
1615
+ const parts = (_i = content.parts) != null ? _i : [];
1508
1616
  for (const part of parts) {
1509
- if ("executableCode" in part && ((_h = part.executableCode) == null ? void 0 : _h.code)) {
1617
+ if ("executableCode" in part && ((_j = part.executableCode) == null ? void 0 : _j.code)) {
1510
1618
  const toolCallId = generateId3();
1511
1619
  lastCodeExecutionToolCallId = toolCallId;
1512
1620
  controller.enqueue({
@@ -1633,17 +1741,20 @@ var GoogleGenerativeAILanguageModel = class {
1633
1741
  }
1634
1742
  }
1635
1743
  }
1636
- if (candidate.finishReason != null) {
1637
- finishReason = mapGoogleGenerativeAIFinishReason({
1638
- finishReason: candidate.finishReason,
1744
+ const promptBlockReason = (_k = value.promptFeedback) == null ? void 0 : _k.blockReason;
1745
+ const isPromptBlocked = candidate.finishReason == null && promptBlockReason != null;
1746
+ const rawFinishReason = (_m = (_l = candidate.finishReason) != null ? _l : promptBlockReason) != null ? _m : void 0;
1747
+ if (rawFinishReason != null) {
1748
+ finishReason = isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
1749
+ finishReason: rawFinishReason,
1639
1750
  hasToolCalls
1640
1751
  });
1641
1752
  providerMetadata = {
1642
1753
  google: {
1643
- promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1754
+ promptFeedback: (_n = value.promptFeedback) != null ? _n : null,
1644
1755
  groundingMetadata: lastGroundingMetadata,
1645
1756
  urlContextMetadata: lastUrlContextMetadata,
1646
- safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
1757
+ safetyRatings: (_o = candidate.safetyRatings) != null ? _o : null,
1647
1758
  serviceTier
1648
1759
  }
1649
1760
  };
@@ -1919,7 +2030,7 @@ var responseSchema = lazySchema5(
1919
2030
  groundingMetadata: getGroundingMetadataSchema().nullish(),
1920
2031
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
1921
2032
  })
1922
- ),
2033
+ ).nullish(),
1923
2034
  usageMetadata: usageSchema.nullish(),
1924
2035
  promptFeedback: z5.object({
1925
2036
  blockReason: z5.string().nullish(),