Schemas as Anthropic structured output (Effect's
effect/ai/AnthropicStructuredOutput).
to_codec_anthropic(schema) gives the JSON Schema to send to Anthropic
and the codec decoding the model's output back into the schema's values.
Shapes strict mode cannot express are sent in a provider-safe form the
codec decodes: a tuple as an object with keys "0", "1", ... (its
rest under "__rest__"), an object with index signatures as an array of
[key, value] pairs, an optional key as a required nullable one, a
one_of union as anyOf. Keywords Anthropic does not support are left
out of the JSON Schema (an unsupported format becomes part of the
description) and still checked by the codec. A recursive schema raises.
Re-exports#
Functions#
to_codec_anthropic#
to_codec_anthropic[T, E, RD, RE](schema) -> StructuredOutputCodec[T, RD, RE]schema's Anthropic JSON Schema and the codec of its output (see
the module); raises ValueError on a recursive schema.
| Parameter | Type | Description |
|---|---|---|
schema | Codec[T, E, RD, RE] |
Returns StructuredOutputCodec[T, RD, RE]
def to_codec_anthropic[T, E, RD, RE](
schema: Codec[T, E, RD, RE], /
) -> StructuredOutputCodec[T, RD, RE]:
"""``schema``'s Anthropic JSON Schema and the codec of its output (see
the module); raises ``ValueError`` on a recursive schema."""
codec = to_codec(schema)
document = J.document(codec)
if _has_reference_cycle(document.schema, document.definitions):
raise ValueError("AnthropicStructuredOutput: Recursive schemas are not supported")
root = _rewrite(J.resolve_root(document))
return StructuredOutputCodec(codec, J.with_definitions(root, document.definitions, _rewrite))