structured-output
Force LLMs to emit valid, schema-constrained structured output: JSON modes, function calling, JSON Schema validation, and error recovery. Use for reliable data extraction.
Structured Output
Getting LLMs to reliably emit valid, schema-constrained output.
Quick Start
pip install openai pydantic jsonschema
# combine response_format with a JSON Schema for guaranteed shapeWhen to Use
- Data extraction into typed records
- API responses that must match a contract
- Tool/function arguments generation
- Reducing malformed-output handling
Best Practices
Schema Design
- Define a strict JSON Schema per output shape
- Use `additionalProperties: false` to reject surprises
- Keep schemas flat and primitive where possible
- Version schemas alongside your code
Mode Selection
- Prefer the provider's `response_format` (json_object / json_schema)
- Use function/tool calling when the output feeds a tool
- Fall back to few-shot + validation for models without structured mode
- Never trust unvalidated text
Validation & Recovery
- Validate every response against the schema
- Re-prompt with the validation error on failure
- Cap retries; fall back to a default record
- Log malformed responses for prompt tuning
Dependencies
pip install openai pydantic jsonschemaExamples
import json
import openai
from jsonschema import validate, ValidationError
client = openai.OpenAI()
SCHEMA = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0},
"emails": {"type": "array", "items": {"type": "string"}},
},
"required": ["name", "age"],
"additionalProperties": False,
}
def extract(text: str) -> dict:
resp = client.chat.completions.create(
model="gpt-6",
messages=[{"role": "user", "content": f"Extract from: {text}"}],
response_format={"type": "json_schema", "json_schema": {"name": "person", "schema": SCHEMA}},
)
return json.loads(resp.choices[0].message.content)# Validate and recover on failure
def safe_extract(text: str, retries: int = 2) -> dict:
for attempt in range(retries):
raw = extract(text)
try:
validate(instance=raw, schema=SCHEMA)
return raw
except ValidationError as e:
print(f"retry {attempt}: {e.message}")
return {"name": "unknown", "age": 0}# Pydantic-based extraction
from pydantic import BaseModel, Field
class Person(BaseModel):
name: str
age: int = Field(ge=0)
emails: list[str] = []
resp = client.beta.chat.completions.parse(
model="gpt-6",
messages=[{"role": "user", "content": "Alice is 30, email a@b.com"}],
response_format=Person,
)
print(resp.choices[0].message.parsed.model_dump())# JSON mode fallback for providers without json_schema
def extract_json_mode(text: str) -> dict:
resp = client.chat.completions.create(
model="gpt-6",
messages=[
{"role": "system", "content": "Respond only with valid JSON matching the schema."},
{"role": "user", "content": text},
],
response_format={"type": "json_object"},
)
return json.loads(resp.choices[0].message.content)Step-by-Step
1. Define the exact output shape as a JSON Schema.
2. Pick the strictest mode the provider supports (schema > json_object > few-shot).
3. Implement extraction with the schema attached.
4. Validate every response; re-prompt with errors on mismatch.
5. Add retry limits and safe defaults.
6. Log failures to improve the prompt or schema.
7. Add tests with realistic inputs including edge cases.
8. Version schemas and monitor drift.
Validation
1. All outputs parse as valid JSON
2. Every output satisfies the schema (validated programmatically)
3. Invalid inputs still yield schema-conforming records
4. Retry recovery works without infinite loops
5. Contract changes are versioned
Troubleshooting
- Output fails schema: tighten the prompt, add examples, or narrow the schema.
- `additionalProperties` errors: expected extra fields — adjust the schema.
- Model ignores format: switch to function calling or guided generation.
Structured Output (Структурированный вывод)
Получение от LLM валидного, ограниченного схемой вывода.
Быстрый старт
pip install openai pydantic jsonschema
# объединяйте response_format с JSON Schema для гарантированной формыКогда использовать
- Извлечение данных в типизированные записи
- Ответы API, которые должны соответствовать контракту
- Генерация аргументов tool/function
- Снижение нагрузки от битого вывода
Лучшие практики
Дизайн схемы
- Строгая JSON Schema под форму вывода
- `additionalProperties: false` — отбрасывать лишнее
- Держите схемы плоскими и примитивными, где можно
- Версионируйте схемы вместе с кодом
Выбор режима
- Предпочитайте `response_format` провайдера (json_object / json_schema)
- Если вывод идёт в tool — function calling
- Без structured mode — few-shot + валидация
- Никогда не доверяйте невалидированному тексту
Валидация и восстановление
- Валидируйте каждый ответ по схеме
- При ошибке — повторный промпт с сообщением об ошибке
- Ограничьте retries; фолбэк на запись по умолчанию
- Логируйте битые ответы для настройки промпта
Зависимости
pip install openai pydantic jsonschemaПримеры
import json
import openai
from jsonschema import validate, ValidationError
client = openai.OpenAI()
SCHEMA = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0},
"emails": {"type": "array", "items": {"type": "string"}},
},
"required": ["name", "age"],
"additionalProperties": False,
}
def extract(text: str) -> dict:
resp = client.chat.completions.create(
model="gpt-6",
messages=[{"role": "user", "content": f"Extract from: {text}"}],
response_format={"type": "json_schema", "json_schema": {"name": "person", "schema": SCHEMA}},
)
return json.loads(resp.choices[0].message.content)# Валидация и восстановление при фейле
def safe_extract(text: str, retries: int = 2) -> dict:
for attempt in range(retries):
raw = extract(text)
try:
validate(instance=raw, schema=SCHEMA)
return raw
except ValidationError as e:
print(f"retry {attempt}: {e.message}")
return {"name": "unknown", "age": 0}# Извлечение через Pydantic
from pydantic import BaseModel, Field
class Person(BaseModel):
name: str
age: int = Field(ge=0)
emails: list[str] = []
resp = client.beta.chat.completions.parse(
model="gpt-6",
messages=[{"role": "user", "content": "Alice is 30, email a@b.com"}],
response_format=Person,
)
print(resp.choices[0].message.parsed.model_dump())# JSON mode фолбэк для провайдеров без json_schema
def extract_json_mode(text: str) -> dict:
resp = client.chat.completions.create(
model="gpt-6",
messages=[
{"role": "system", "content": "Respond only with valid JSON matching the schema."},
{"role": "user", "content": text},
],
response_format={"type": "json_object"},
)
return json.loads(resp.choices[0].message.content)Пошаговое руководство
1. Определите точную форму вывода как JSON Schema.
2. Выберите строжайший режим провайдера (schema > json_object > few-shot).
3. Реализуйте извлечение со схемой.
4. Валидируйте каждый ответ; при несовпадении — повторный промпт с ошибкой.
5. Добавьте лимиты retries и безопасные дефолты.
6. Логируйте фейлы для улучшения промпта или схемы.
7. Добавьте тесты на реалистичные вводы, включая edge cases.
8. Версионируйте схемы и мониторьте дрейф.
Валидация
1. Весь вывод парсится как валидный JSON
2. Каждый вывод удовлетворяет схеме (программная проверка)
3. Невалидные вводы всё равно дают schema-conforming записи
4. Восстановление через retry работает без бесконечных циклов
5. Изменения контракта версионируются
Устранение неполадок
- Вывод не проходит схему: ужесточите промпт, добавьте примеры или сузьте схему.
- Ошибки `additionalProperties`: есть лишние поля — поправьте схему.
- Модель игнорирует формат: переключитесь на function calling или guided generation.