GitHub

openai_structured_output

API reference for openai_structured_output.

Schemas as OpenAI structured output (Effect's effect/ai/OpenAiStructuredOutput).

to_codec_openai(schema) gives the JSON Schema to send to OpenAI's strict mode and the codec decoding the model's output back into the schema's values. Shapes strict mode cannot express are sent in the provider-safe form anthropic_structured_output describes (tuples as objects, records as [key, value] pairs, optional keys as required nullable ones, one_of as anyOf). OpenAI takes no allOf: patterns of its members are merged into one, bounds into the tightest, and the other keywords kept only when that keeps their meaning. Keywords OpenAI does not support, or that do not apply to the schema's type, are left out (an unsupported format becomes part of the description) and still checked by the codec. The root must be an object without anyOf. Compatibility targets standard models; fine-tuned ones take less.

Classes

Functions

to_codec_openai

to_codec_openai[T, E, RD, RE](schema) -> StructuredOutputCodec[T, RD, RE]

schema's OpenAI JSON Schema and the codec of its output (see the module); raises ValueError when the root is not an object.

ParameterTypeDescription
schemaCodec[T, E, RD, RE]

Returns StructuredOutputCodec[T, RD, RE]

def to_codec_openai[T, E, RD, RE](
    schema: Codec[T, E, RD, RE], /
) -> StructuredOutputCodec[T, RD, RE]:
    """``schema``'s OpenAI JSON Schema and the codec of its output (see the
    module); raises ``ValueError`` when the root is not an object."""
    codec = to_codec(schema)
    document = J.document(codec)
    root = _rewrite(J.resolve_root(document))
    if root.get("type") != "object" or "anyOf" in root:
        raise ValueError(
            'OpenAiStructuredOutput: Root JSON Schema must have type "object" '
            'and must not use "anyOf"'
        )
    return StructuredOutputCodec(codec, J.with_definitions(root, document.definitions, _rewrite))