Skip to content

Add opt-in strict JSON Schema output for OpenAI models #1661

Description

@cacoze

Problem

The OpenAI integrations currently send JSON schemas without enabling strict Structured Outputs:

  • Chat Completions omits json_schema.strict.
  • Responses API explicitly sets strict: false.

Consequently, supplying a schema does not guarantee schema-conforming output. We have observed successful responses containing:

  • Arrays violating minItems
  • Strings where objects were required
  • Unexpected properties despite additionalProperties: false
  • Incorrectly named fields

These responses are valid JSON, but fail downstream Pydantic validation.

Proposed solution

Add an opt-in OpenAI model option such as:

llm -m gpt-4o -o strict_schema true --schema schema.json "..."

And its Python equivalent:

model.prompt(
    prompt,
    schema=OutputModel,
    strict_schema=True,
)

When enabled, the OpenAI adapter would send:

{
  "type": "json_schema",
  "json_schema": {
    "name": "output",
    "schema": {},
    "strict": true
  }
}

The Responses API equivalent should also use strict: true.

The option could default to false for backwards compatibility.

Schema compatibility

OpenAI strict mode requires a restricted JSON Schema shape, including:

  • additionalProperties: false on objects
  • Every property listed as required
  • Only supported JSON Schema keywords

Possible behavior:

  1. Convert Pydantic models using the same strict-schema normalization used by the OpenAI Python SDK, or
  2. Validate the supplied schema and return a clear error when it is incompatible.

Silently weakening the schema would be undesirable.

Response handling

It would also be useful to preserve or expose:

  • Safety refusals
  • finish_reason
  • Incomplete/truncated response status

These are documented cases where strict Structured Outputs may not return a schema-conforming result.

Reproduction

A schema containing object restrictions and a non-empty array constraint can occasionally return output that violates those constraints:

{
  "type": "object",
  "properties": {
    "items": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "properties": {
          "name": {"type": "string"}
        },
        "required": ["name"],
        "additionalProperties": false
      }
    }
  },
  "required": ["items"],
  "additionalProperties": false
}

Without strict: true, responses such as {"items": []} or
{"items": ["example"]} remain possible.

Environment

  • llm 0.29
  • OpenAI gpt-4o
  • Python API with a Pydantic model passed as schema

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions