Skip to content

Instantly share code, notes, and snippets.

@aviadr1
aviadr1 / json_schema.py
Last active November 17, 2025 07:02
**OpenAI JSON Schema Sanitizer for Pydantic Models** - A production-ready function that transforms any Pydantic model into an OpenAI Structured Outputs-compatible JSON schema, handling optionals, unions, recursion detection, numeric constraints, and additionalProperties issues that cause API failures. Includes comprehensive test suite covering a…
"""
File: gv/ai/common/llm/json_schema.py
Author: Aviad Rozenhek
OpenAI Structured Outputs (`response_format={"type":"json_schema"}`) supports only a subset of JSON Schema.
Many perfectly valid Pydantic constructs won't fly as-is. Use these patterns:
-------------------------------------------------------------------------
1) Optional / nullable / Default fields
-------------------------------------------------------------------------
Patient History Report
Client: Batiz, Andrea (23583) Patient: HADES (23583-1)
Phone: (254) 251-8121 Species: FELINE Breed: DOMESTIC
SHORTHAIR
Address: 4306 Fremont Drive Age: 5 Mos. 3 Wks. 4
Days
Sex: Neutered Male