#2695·instructor

Mode.JSON_SCHEMA / TOOLS(strict=True) / OpenRouter emit OpenAI-noncompliant strict schemas

Author: michaelkillgtaCreated Sep 25, 2026Updated Sep 25, 2026

Description

Live OpenAI Structured Outputs paths emit schemas that violate OpenAI’s strict rules. Requests either fail OpenAI validation or run without real schema adherence.

OpenAI Structured Outputs requires strict: true, additionalProperties: false on every object (including $defs), and all properties listed in required (optionals via null union). Observed failures:

Path Failure
Mode.JSON_SCHEMA Omits strict entirely; raw model_json_schema() with no recursive additionalProperties: false / all-required
Mode.TOOLS + strict=True Sets function.strict=true but leaves parameters non-compliant
OpenRouter structured helper Sets strict:true and root additionalProperties:false only — nested $defs untouched

Contrast: instructor/batch/request.py already has recursive make_strict_schema, and Bedrock has _prepare_bedrock_strict_schema. The chat Completions path never got the same treatment.

Environment

  • instructor==1.17.0
  • Python 3.13

Minimal repro

python
# Offline prepare_request — no API key required
from typing import Optional
from pydantic import BaseModel
import instructor
from instructor import Mode

class Address(BaseModel):
    street: str
    city: Optional[str] = None

class User(BaseModel):
    name: str
    address: Address
    tags: Optional[list[str]] = None

client = instructor.from_openai(
    # any openai-compatible stub is fine for prepare_request inspection;
    # see full offline script in hunt repros if needed
)
# Inspect prepared request schemas for Mode.JSON_SCHEMA and Mode.TOOLS(strict=True):
# - JSON_SCHEMA: no top-level strict
# - TOOLS strict: function.strict true but parameters missing recursive AP:false + all-required
# - OpenRouter helper: nested $defs.Address missing additionalProperties:false

Full offline checker used for confirmation: problems include root/$defs.Address missing additionalProperties:false and optional props not listed in required.

Expected

Mode.JSON_SCHEMA, Mode.TOOLS with strict=True, and OpenRouter structured helpers should emit OpenAI-compliant strict schemas (same recursive normalization as batch/Bedrock).

Actual

Live Completions paths omit strict and/or leave nested objects non-compliant.

Root cause (pointer)

instructor/v2/providers/openai/handlers.py — OpenAIJSONSchemaHandler.prepare_request, OpenAIToolsHandler.prepare_request, handle_openrouter_structured_outputs do not reuse batch’s recursive strict normalizer.

Suggested fix direction

Reuse / share make_strict_schema (or equivalent) on the Completions JSON_SCHEMA / TOOLS(strict) / OpenRouter paths so nested $defs get additionalProperties: false and all properties are required.