| 770 | * @since 4.0.0 |
| 771 | */ |
| 772 | export class StructuredOutputError extends Schema.Error<StructuredOutputError>( |
| 773 | "effect/ai/AiError/StructuredOutputError" |
| 774 | )({ |
| 775 | _tag: Schema.tag("StructuredOutputError"), |
| 776 | description: Schema.String, |
| 777 | responseText: Schema.String, |
| 778 | metadata: providerMetadataWithDefaults<StructuredOutputErrorMetadata>(), |
| 779 | usage: Schema.optional(UsageInfo) |
| 780 | }) { |
| 781 | /** |
| 782 | * Marks `StructuredOutputError` as a semantic AI error reason for runtime guards. |
| 783 | * |
| 784 | * @since 4.0.0 |
| 785 | */ |
| 786 | readonly [ReasonTypeId] = ReasonTypeId |
| 787 | |
| 788 | /** |
| 789 | * Structured output errors are retryable since LLM outputs are non-deterministic. |
| 790 | * |
| 791 | * @since 4.0.0 |
| 792 | */ |
| 793 | get isRetryable(): boolean { |
| 794 | return true |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * Creates a StructuredOutputError from a Schema error. |
| 799 | * |
| 800 | * **Example** (Creating a structured output error from a schema error) |
| 801 | * |
| 802 | * ```ts import.meta.vitest |
| 803 | * import { Effect, Schema } from "effect" |
| 804 | * import { AiError } from "effect/unstable/ai" |
| 805 | * |
| 806 | * const schemaError = await Effect.runPromise( |
| 807 | * Schema.decodeUnknownEffect(Schema.Struct({ name: Schema.String }))({}).pipe(Effect.flip) |
| 808 | * ) |
| 809 | * const parseError = AiError.StructuredOutputError.fromSchemaError(schemaError, "{}") |
| 810 | * parseError.responseText // => "{}" |
| 811 | * ``` |
| 812 | * |
| 813 | * @since 4.0.0 |
| 814 | */ |
| 815 | static fromSchemaError(error: Schema.SchemaError, responseText: string): StructuredOutputError { |
| 816 | return new StructuredOutputError({ |
| 817 | description: error.message, |
| 818 | responseText |
| 819 | }) |
| 820 | } |
| 821 | |
| 822 | override get message(): string { |
| 823 | return `Structured output validation failed: ${this.description}` |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * Error indicating a codec transformer rejected a schema because it contains |
nothing calls this directly
no test coverage detected