schemautil

package
v1.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package schemautil provides utilities for generating and manipulating JSON schemas with OpenAI's constraints.

Index

Constants

This section is empty.

Variables

View Source
var ErrAdditionalProperties = errors.New("additionalProperties not allowed for object types")

ErrAdditionalProperties is returned when additionalProperties is set for object types.

Functions

func EnsureStrictness

func EnsureStrictness(schema map[string]any) (map[string]any, error)

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:

  1. 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.

  2. Arrays (`type: "array"`): - Recursively applies strictness to the `items` schema.

  3. 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.

  4. Intersections (`allOf`): - If only one variant, inlines it into the parent schema. - Otherwise, recursively applies strictness to each variant.

  5. Definitions (`$defs` or `definitions`): - Recursively applies strictness to all subschemas in definitions.

  6. `$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.

  7. Defaults: - Removes `default: null` as it has no meaningful distinction for OpenAI.

  8. 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

func StrictObject(properties map[string]any) map[string]any

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.

func ToStrictJSONSchema

func ToStrictJSONSchema[T any]() (map[string]any, error)

ToStrictJSONSchema generates a strict JSON schema for the given type T.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL