LangSchema.Converter.OpenAI (LangSchema v0.7.1)

View Source

Converts a LangSchema into a JSON schema compatible with OpenAI's Structured Outputs.

Supported JSON Schema spec

Reference: https://platform.openai.com/docs/guides/structured-outputs/supported-schemas

Supported types

string, number, integer, boolean, object, array, enum, anyOf

Root objects cannot be anyOf type.

Supported keywords

  • type, description, enum, const
  • properties, required, additionalProperties (must be false)
  • items, anyOf
  • $ref, $defs (recursive schemas supported)

Unsupported keywords

TypeUnsupported keywords
StringminLength, maxLength, pattern, format
Numberminimum, maximum, multipleOf
ObjectpatternProperties, unevaluatedProperties, propertyNames, minProperties, maxProperties
ArrayunevaluatedItems, contains, minContains, maxContains, minItems, maxItems, uniqueItems

Constraints

  • All fields must be required.
  • additionalProperties must be false on every object.
  • Max 100 object properties total, up to 5 levels of nesting.

LangSchema implementation status

KeywordStatus
type, description, enumSupported
properties, required, additionalPropertiesSupported (auto-enforced)
itemsSupported
anyOfSupported
nullableSupported (via ["type", "null"])
constNot yet implemented
$ref, $defsNot yet implemented

Summary

Functions

Wraps the given JSON schema into the structured output envelope expected by OpenAI's Chat API (response_format: "json_schema").

Functions

wrap(json_schema, opts)

Wraps the given JSON schema into the structured output envelope expected by OpenAI's Chat API (response_format: "json_schema").

This implementation follows the specification described under: OpenAI API Reference - Create Chat Completion (Request body → response_format → JSON schema → json_schema).

It sets the name, attaches the provided JSON schema under the schema field, and enforces strict: true. If a description is provided in the options, it will be added as well.