@ai-sdk/google 4.0.72 → 4.0.74

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.
@@ -2304,3 +2304,54 @@ const result = await generateSpeech({
2304
2304
  | `gemini-2.5-flash-preview-tts` | <Check /> | <Check /> |
2305
2305
  | `gemini-2.5-pro-preview-tts` | <Check /> | <Check /> |
2306
2306
  | `gemini-3.1-flash-tts-preview` | <Check /> | <Check /> |
2307
+
2308
+ ## Evaluation Models
2309
+
2310
+ Create an experimental evaluation model with `google.evaluationModel(modelId)`.
2311
+ It uses Gemini structured output for Choice, Score, and Boolean questions. Choose a model
2312
+ that supports structured output, such as `gemini-3.5-flash-lite`.
2313
+
2314
+ ```ts
2315
+ import { google } from '@ai-sdk/google';
2316
+ import { experimental_evaluate } from 'ai';
2317
+
2318
+ const { answers } = await experimental_evaluate({
2319
+ model: google.evaluationModel('gemini-3.5-flash-lite'),
2320
+ state: 'I was charged twice.',
2321
+ questions: {
2322
+ requestsRefund: {
2323
+ type: 'boolean',
2324
+ instructions: 'Is the customer requesting money back?',
2325
+ },
2326
+ department: {
2327
+ type: 'choice',
2328
+ instructions: 'Which team should handle this?',
2329
+ criteria: { billing: 'Charges and refunds', support: 'Other requests' },
2330
+ },
2331
+ },
2332
+ providerOptions: {
2333
+ google: { thinkingConfig: { thinkingLevel: 'minimal' } },
2334
+ },
2335
+ });
2336
+ ```
2337
+
2338
+ The factory respects `createGoogle` settings and forwards `providerOptions.google`,
2339
+ including thinking configuration. Thinking controls depend on the selected model;
2340
+ use options supported by that model. The existing Gemini implementation sends the
2341
+ answer schema as `generationConfig.responseJsonSchema`.
2342
+
2343
+ Choice labels are returned exactly, and Scores are finite fractional positions
2344
+ within the ordered rubric. The adapter validates complete answers after parsing
2345
+ and rejects safety-blocked, truncated, or invalid output. It preserves usage
2346
+ (including reasoning tokens), warnings, response data, and provider metadata.
2347
+
2348
+ Choice and Score answers do not include probability distributions. Boolean
2349
+ answers contain prompted estimates of P(true), validated to be finite and in
2350
+ `[0, 1]`. These estimates are not guaranteed to be calibrated. Apply thresholds
2351
+ in application code, for example `answers.requestsRefund.probability >= 0.5`. See [Evaluation](/docs/ai-sdk-core/evaluation).
2352
+
2353
+ Evaluation models can also be accessed through `customProvider` aliases or
2354
+ `createProviderRegistry().evaluationModel('provider:model')`. Direct string IDs
2355
+ require an explicitly configured default provider with an `evaluationModel`
2356
+ method; they do not automatically use Gateway. See
2357
+ [model aliases and registries](/docs/ai-sdk-core/evaluation#model-aliases-and-registries).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/google",
3
- "version": "4.0.72",
3
+ "version": "4.0.74",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -35,8 +35,8 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@ai-sdk/provider": "4.0.15",
39
- "@ai-sdk/provider-utils": "5.0.41"
38
+ "@ai-sdk/provider": "4.0.17",
39
+ "@ai-sdk/provider-utils": "5.0.43"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@ai-sdk/test-server": "2.0.1",
@@ -726,9 +726,12 @@ export class GoogleLanguageModel implements LanguageModelV4 {
726
726
  raw: undefined,
727
727
  };
728
728
  let usage: GoogleUsageMetadata | undefined = undefined;
729
- let providerMetadata: SharedV4ProviderMetadata | undefined = undefined;
729
+ let promptFeedback: PromptFeedbackSchema | null = null;
730
730
  let lastGroundingMetadata: GroundingMetadataSchema | null = null;
731
731
  let lastUrlContextMetadata: UrlContextMetadataSchema | null = null;
732
+ let lastSafetyRatings: SafetyRatingSchema[] | null = null;
733
+ let lastFinishMessage: string | null = null;
734
+ let confirmedPromptBlockReason: string | undefined;
732
735
 
733
736
  const generateId = this.config.generateId;
734
737
  let hasToolCalls = false;
@@ -825,38 +828,48 @@ export class GoogleLanguageModel implements LanguageModelV4 {
825
828
  usage = usageMetadata;
826
829
  }
827
830
 
828
- const candidate = value.candidates?.[0];
831
+ if (
832
+ value.promptFeedback != null &&
833
+ confirmedPromptBlockReason == null
834
+ ) {
835
+ promptFeedback = value.promptFeedback;
829
836
 
830
- // sometimes the API returns an empty candidates array
831
- if (candidate == null) {
832
- const promptBlockReason = value.promptFeedback?.blockReason;
833
- if (promptBlockReason != null) {
837
+ if (
838
+ isConfirmedPromptBlockReason(value.promptFeedback.blockReason)
839
+ ) {
840
+ confirmedPromptBlockReason = value.promptFeedback.blockReason;
834
841
  finishReason = {
835
842
  unified: 'content-filter',
836
- raw: promptBlockReason,
843
+ raw: confirmedPromptBlockReason,
837
844
  };
838
- providerMetadata = wrapProviderMetadata({
839
- promptFeedback: value.promptFeedback ?? null,
840
- groundingMetadata: lastGroundingMetadata,
841
- urlContextMetadata: lastUrlContextMetadata,
842
- safetyRatings: null,
843
- usageMetadata: usageMetadata ?? null,
844
- finishMessage: null,
845
- serviceTier: usage?.serviceTier ?? null,
846
- } satisfies GoogleProviderMetadata);
847
845
  }
848
- return;
849
846
  }
850
847
 
851
- const content = candidate.content;
848
+ const candidate = value.candidates?.[0];
852
849
 
853
- if (candidate.groundingMetadata != null) {
854
- lastGroundingMetadata = candidate.groundingMetadata;
850
+ if (candidate != null) {
851
+ if (candidate.groundingMetadata != null) {
852
+ lastGroundingMetadata = candidate.groundingMetadata;
853
+ }
854
+ if (candidate.urlContextMetadata != null) {
855
+ lastUrlContextMetadata = candidate.urlContextMetadata;
856
+ }
857
+ if (candidate.safetyRatings != null) {
858
+ lastSafetyRatings = candidate.safetyRatings;
859
+ }
860
+ if (candidate.finishMessage != null) {
861
+ lastFinishMessage = candidate.finishMessage;
862
+ }
855
863
  }
856
- if (candidate.urlContextMetadata != null) {
857
- lastUrlContextMetadata = candidate.urlContextMetadata;
864
+
865
+ // A confirmed prompt block is terminal for generated content, but
866
+ // later chunks can still contribute usage and provider metadata.
867
+ if (confirmedPromptBlockReason != null || candidate == null) {
868
+ return;
858
869
  }
859
870
 
871
+ const content = candidate.content;
872
+
860
873
  const sources = extractSources({
861
874
  groundingMetadata: candidate.groundingMetadata,
862
875
  generateId,
@@ -1222,32 +1235,14 @@ export class GoogleLanguageModel implements LanguageModelV4 {
1222
1235
  }
1223
1236
  }
1224
1237
 
1225
- const promptBlockReason = value.promptFeedback?.blockReason;
1226
- const isPromptBlocked =
1227
- candidate.finishReason == null && promptBlockReason != null;
1228
- const rawFinishReason =
1229
- candidate.finishReason ?? promptBlockReason ?? undefined;
1230
-
1231
- if (rawFinishReason != null) {
1238
+ if (candidate.finishReason != null) {
1232
1239
  finishReason = {
1233
- unified: isPromptBlocked
1234
- ? 'content-filter'
1235
- : mapGoogleFinishReason({
1236
- finishReason: rawFinishReason,
1237
- hasToolCalls,
1238
- }),
1239
- raw: rawFinishReason,
1240
+ unified: mapGoogleFinishReason({
1241
+ finishReason: candidate.finishReason,
1242
+ hasToolCalls,
1243
+ }),
1244
+ raw: candidate.finishReason,
1240
1245
  };
1241
-
1242
- providerMetadata = wrapProviderMetadata({
1243
- promptFeedback: value.promptFeedback ?? null,
1244
- groundingMetadata: lastGroundingMetadata,
1245
- urlContextMetadata: lastUrlContextMetadata,
1246
- safetyRatings: candidate.safetyRatings ?? null,
1247
- usageMetadata: usageMetadata ?? null,
1248
- finishMessage: candidate.finishMessage ?? null,
1249
- serviceTier: usage?.serviceTier ?? null,
1250
- } satisfies GoogleProviderMetadata);
1251
1246
  }
1252
1247
  },
1253
1248
 
@@ -1269,7 +1264,15 @@ export class GoogleLanguageModel implements LanguageModelV4 {
1269
1264
  type: 'finish',
1270
1265
  finishReason,
1271
1266
  usage: convertGoogleUsage(usage),
1272
- providerMetadata,
1267
+ providerMetadata: wrapProviderMetadata({
1268
+ promptFeedback,
1269
+ groundingMetadata: lastGroundingMetadata,
1270
+ urlContextMetadata: lastUrlContextMetadata,
1271
+ safetyRatings: lastSafetyRatings,
1272
+ usageMetadata: usage ?? null,
1273
+ finishMessage: lastFinishMessage,
1274
+ serviceTier: usage?.serviceTier ?? null,
1275
+ } satisfies GoogleProviderMetadata),
1273
1276
  });
1274
1277
  },
1275
1278
  }),
@@ -1794,3 +1797,14 @@ const chunkSchema = lazySchema(() =>
1794
1797
  );
1795
1798
 
1796
1799
  type ChunkSchema = InferSchema<typeof chunkSchema>;
1800
+
1801
+ function isConfirmedPromptBlockReason(
1802
+ blockReason: string | null | undefined,
1803
+ ): blockReason is string {
1804
+ return (
1805
+ blockReason != null &&
1806
+ blockReason !== '' &&
1807
+ blockReason !== 'BLOCK_REASON_UNSPECIFIED' &&
1808
+ blockReason !== 'BLOCKED_REASON_UNSPECIFIED'
1809
+ );
1810
+ }
@@ -1,6 +1,7 @@
1
1
  import type {
2
2
  EmbeddingModelV4,
3
3
  Experimental_BatchV4 as BatchV4,
4
+ Experimental_EvaluationModelV4 as EvaluationModelV4,
4
5
  Experimental_VideoModelV4,
5
6
  FilesV4,
6
7
  ImageModelV4,
@@ -20,6 +21,7 @@ import {
20
21
  type FetchFunction,
21
22
  type WebSocketConstructor,
22
23
  } from '@ai-sdk/provider-utils';
24
+ import { Experimental_EvaluationLanguageModel as EvaluationLanguageModel } from '@ai-sdk/provider-utils/experimental-evaluation';
23
25
  import { VERSION } from './version';
24
26
  import { GoogleEmbeddingModel } from './google-embedding-model';
25
27
  import type { GoogleEmbeddingModelId } from './google-embedding-model-options';
@@ -61,6 +63,9 @@ export interface GoogleProvider extends ProviderV4 {
61
63
 
62
64
  chat(modelId: GoogleModelId): LanguageModelV4;
63
65
 
66
+ /** Creates an experimental Choice/Score/Boolean evaluation model using Gemini. */
67
+ evaluationModel(modelId: GoogleModelId): EvaluationModelV4;
68
+
64
69
  experimental_batch(): BatchV4<{
65
70
  text: GoogleModelId;
66
71
  image: GoogleImageModelId;
@@ -430,6 +435,11 @@ export function createGoogle(
430
435
  provider.languageModel = createChatModel;
431
436
  provider.chat = createChatModel;
432
437
  provider.generativeAI = createChatModel;
438
+ provider.evaluationModel = (modelId: GoogleModelId) =>
439
+ new EvaluationLanguageModel({
440
+ model: createChatModel(modelId),
441
+ provider: `${providerName.replace(/\.generative-ai$/, '')}.evaluation`,
442
+ });
433
443
  provider.experimental_batch = createBatch;
434
444
  provider.embedding = createEmbeddingModel;
435
445
  provider.embeddingModel = createEmbeddingModel;