@ai-sdk/google 4.0.72 → 4.0.73

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/google",
3
- "version": "4.0.72",
3
+ "version": "4.0.73",
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.16",
39
+ "@ai-sdk/provider-utils": "5.0.42"
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
+ }