ai 4.1.42 → 4.1.43

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
@@ -918,6 +918,10 @@ var DefaultGeneratedImage = class {
918
918
  };
919
919
 
920
920
  // core/generate-object/generate-object.ts
921
+ import {
922
+ JSONParseError,
923
+ TypeValidationError as TypeValidationError2
924
+ } from "@ai-sdk/provider";
921
925
  import { createIdGenerator, safeParseJSON } from "@ai-sdk/provider-utils";
922
926
 
923
927
  // errors/no-object-generated-error.ts
@@ -2370,6 +2374,7 @@ async function generateObject({
2370
2374
  maxRetries: maxRetriesArg,
2371
2375
  abortSignal,
2372
2376
  headers,
2377
+ experimental_repairText: repairText,
2373
2378
  experimental_telemetry: telemetry,
2374
2379
  experimental_providerMetadata,
2375
2380
  providerOptions = experimental_providerMetadata,
@@ -2670,32 +2675,52 @@ async function generateObject({
2670
2675
  throw new Error(`Unsupported mode: ${_exhaustiveCheck}`);
2671
2676
  }
2672
2677
  }
2673
- const parseResult = safeParseJSON({ text: result });
2674
- if (!parseResult.success) {
2675
- throw new NoObjectGeneratedError({
2676
- message: "No object generated: could not parse the response.",
2677
- cause: parseResult.error,
2678
- text: result,
2679
- response,
2680
- usage: calculateLanguageModelUsage(usage)
2681
- });
2678
+ function processResult(result2) {
2679
+ const parseResult = safeParseJSON({ text: result2 });
2680
+ if (!parseResult.success) {
2681
+ throw new NoObjectGeneratedError({
2682
+ message: "No object generated: could not parse the response.",
2683
+ cause: parseResult.error,
2684
+ text: result2,
2685
+ response,
2686
+ usage: calculateLanguageModelUsage(usage)
2687
+ });
2688
+ }
2689
+ const validationResult = outputStrategy.validateFinalResult(
2690
+ parseResult.value,
2691
+ {
2692
+ text: result2,
2693
+ response,
2694
+ usage: calculateLanguageModelUsage(usage)
2695
+ }
2696
+ );
2697
+ if (!validationResult.success) {
2698
+ throw new NoObjectGeneratedError({
2699
+ message: "No object generated: response did not match schema.",
2700
+ cause: validationResult.error,
2701
+ text: result2,
2702
+ response,
2703
+ usage: calculateLanguageModelUsage(usage)
2704
+ });
2705
+ }
2706
+ return validationResult.value;
2682
2707
  }
2683
- const validationResult = outputStrategy.validateFinalResult(
2684
- parseResult.value,
2685
- {
2686
- text: result,
2687
- response,
2688
- usage: calculateLanguageModelUsage(usage)
2708
+ let object2;
2709
+ try {
2710
+ object2 = processResult(result);
2711
+ } catch (error) {
2712
+ if (repairText != null && NoObjectGeneratedError.isInstance(error) && (JSONParseError.isInstance(error.cause) || TypeValidationError2.isInstance(error.cause))) {
2713
+ const repairedText = await repairText({
2714
+ text: result,
2715
+ error: error.cause
2716
+ });
2717
+ if (repairedText === null) {
2718
+ throw error;
2719
+ }
2720
+ object2 = processResult(repairedText);
2721
+ } else {
2722
+ throw error;
2689
2723
  }
2690
- );
2691
- if (!validationResult.success) {
2692
- throw new NoObjectGeneratedError({
2693
- message: "No object generated: response did not match schema.",
2694
- cause: validationResult.error,
2695
- text: result,
2696
- response,
2697
- usage: calculateLanguageModelUsage(usage)
2698
- });
2699
2724
  }
2700
2725
  span.setAttributes(
2701
2726
  selectTelemetryAttributes({
@@ -2703,7 +2728,7 @@ async function generateObject({
2703
2728
  attributes: {
2704
2729
  "ai.response.finishReason": finishReason,
2705
2730
  "ai.response.object": {
2706
- output: () => JSON.stringify(validationResult.value)
2731
+ output: () => JSON.stringify(object2)
2707
2732
  },
2708
2733
  "ai.usage.promptTokens": usage.promptTokens,
2709
2734
  "ai.usage.completionTokens": usage.completionTokens
@@ -2711,7 +2736,7 @@ async function generateObject({
2711
2736
  })
2712
2737
  );
2713
2738
  return new DefaultGenerateObjectResult({
2714
- object: validationResult.value,
2739
+ object: object2,
2715
2740
  finishReason,
2716
2741
  usage: calculateLanguageModelUsage(usage),
2717
2742
  warnings,
@@ -4216,11 +4241,11 @@ import {
4216
4241
  EmptyResponseBodyError,
4217
4242
  InvalidPromptError as InvalidPromptError2,
4218
4243
  InvalidResponseDataError,
4219
- JSONParseError,
4244
+ JSONParseError as JSONParseError2,
4220
4245
  LoadAPIKeyError,
4221
4246
  NoContentGeneratedError,
4222
4247
  NoSuchModelError,
4223
- TypeValidationError as TypeValidationError2,
4248
+ TypeValidationError as TypeValidationError3,
4224
4249
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
4225
4250
  } from "@ai-sdk/provider";
4226
4251
 
@@ -6576,7 +6601,7 @@ export {
6576
6601
  InvalidPromptError2 as InvalidPromptError,
6577
6602
  InvalidResponseDataError,
6578
6603
  InvalidToolArgumentsError,
6579
- JSONParseError,
6604
+ JSONParseError2 as JSONParseError,
6580
6605
  langchain_adapter_exports as LangChainAdapter,
6581
6606
  llamaindex_adapter_exports as LlamaIndexAdapter,
6582
6607
  LoadAPIKeyError,
@@ -6593,7 +6618,7 @@ export {
6593
6618
  StreamData,
6594
6619
  ToolCallRepairError,
6595
6620
  ToolExecutionError,
6596
- TypeValidationError2 as TypeValidationError,
6621
+ TypeValidationError3 as TypeValidationError,
6597
6622
  UnsupportedFunctionalityError2 as UnsupportedFunctionalityError,
6598
6623
  appendClientMessage,
6599
6624
  appendResponseMessages,