AnthropicStructuredOutput.ts
AnthropicStructuredOutput.ts overview
Section titled “AnthropicStructuredOutput.ts overview”Adapts Effect Schema codecs to the JSON Schema subset accepted by Anthropic structured output.
The main entry point returns the JSON Schema to send to Anthropic and a codec
for decoding the model response back into the original application type. When
Anthropic cannot express the original schema shape directly, the conversion
rewrites supported cases such as tuples, records, optional properties, and
oneOf unions. Schema kinds that cannot be represented throw during
conversion instead of producing a lossy schema.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”Codec Transformation
Section titled “Codec Transformation”toCodecAnthropic
Section titled “toCodecAnthropic”Converts a Schema.Codec to Anthropic structured-output JSON Schema and a
matching codec for model output.
When to use
Use when you send Effect Schema-backed structured output requests to Anthropic and need provider-compatible JSON Schema without losing the decoded application type.
Details
Returns the JSON Schema to include in the request and the codec to use when decoding the model response. If the input schema already fits Anthropic’s supported JSON Schema subset, the original codec is returned unchanged.
Gotchas
- Some schemas use a provider-safe encoded shape: tuples become objects with
numeric string keys, records become arrays of
[key, value]pairs, and optional properties become required nullable properties. oneOfunions are emitted asanyOfunions.- Unsupported schema kinds throw during conversion instead of producing a lossy schema.
See
LanguageModel.CodecTransformerfor the structured-output transformer contractOpenAiStructuredOutput.toCodecOpenAIfor the OpenAI-specific transformer
Signature
declare const toCodecAnthropic: <T, E, RD, RE>( schema: Schema.ConstraintCodec<T, E, RD, RE>) => { readonly codec: Schema.ConstraintCodec<T, unknown, RD, RE>; readonly jsonSchema: JsonSchema.JsonSchema }Since v4.0.0