Documentation
¶
Overview ¶
Package schemautil provides utilities for generating and manipulating JSON schemas with OpenAI's constraints.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrAdditionalProperties = errors.New("additionalProperties not allowed for object types")
ErrAdditionalProperties is returned when additionalProperties is set for object types.
Functions ¶
func EnsureStrictness ¶
EnsureStrictness mutates the given JSON schema to ensure it conforms to the `strict` standard that the OpenAI API expects for function/tool calling.
This function enforces the following rules recursively throughout the schema:
Objects (`type: "object"`): - Ensures `additionalProperties: false` is set, disallowing extra fields. - Ensures all defined properties are listed in the `required` array. - Recursively applies strictness to all properties.
Arrays (`type: "array"`): - Recursively applies strictness to the `items` schema.
Unions (`anyOf`): - Converts `oneOf` to `anyOf` as OpenAI does not support `oneOf`, see https://community.openai.com/t/oneof-allof-usage-has-problems-with-strict-mode/966047 - Recursively applies strictness to each variant.
Intersections (`allOf`): - If only one variant, inlines it into the parent schema. - Otherwise, recursively applies strictness to each variant.
Definitions (`$defs` or `definitions`): - Recursively applies strictness to all subschemas in definitions.
`$ref` handling: - If a schema contains a `$ref` **and** other properties, the `$ref` is "unraveled": - The referenced schema is resolved and merged in, with local properties taking precedence. - The `$ref` key is removed. - The merged schema is recursively made strict. - If a schema is just `{"$ref": ...}` (no other keys), it is left as-is.
Defaults: - Removes `default: null` as it has no meaningful distinction for OpenAI.
Defensive fallback: - If the schema is empty, returns a minimal empty object schema.
This ensures the resulting schema is as strict and explicit as possible, matching OpenAI's requirements (see https://platform.openai.com/docs/guides/structured-outputs?type-restrictions=number-restrictions).
Returns the strictified schema or an error if the schema cannot be made strict.
func StrictObject ¶
StrictObject builds a strict JSON object schema from the given properties. All properties are marked required and additionalProperties is disallowed, matching the constraints OpenAI structured outputs / tool calling expects. Use this when the property set is known only at runtime; for static Go types prefer ToStrictJSONSchema.
Types ¶
This section is empty.