Session 5· 04· 10 min

Strict Required Fields

What you'll learn
  • Mark fields as required in the JSON schema
  • Set additionalProperties: false to block extra keys
  • See strict mode in action with a richer schema

This is a deeper drill on the same concept as lesson 03. The schema now has three required fields — concept, example, confidence — and you watch the model fill them without inventing extras.

04_schema_strict_required_fields.py
strict_schema = {
    "type": "object",
    "properties": {
        "concept":    {"type": "string"},
        "example":    {"type": "string"},
        "confidence": {"type": "integer"},                     ①
    },
    "required": ["concept", "example", "confidence"],           ②
    "additionalProperties": False,                              ③
}

response = client.chat.completions.create(
    model=model,
    messages=[
        {"role": "system", "content": "Return concise JSON only."},
        {"role": "user",   "content": "Explain function calling and include confidence from 1 to 10."},
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {"name": "strict_note", "schema": strict_schema, "strict": True},
    },
)
integer is a separate type from number — use integer when you expect no decimals.
All three fields are required — the model cannot omit any.
No extra fields allowed. If the model tries "details": "...", the API blocks it.
$ python 04_schema_strict_required_fields.py
Code Check
If you remove "confidence" from the required array, what changes?
Recap — what you just learned
  • required[] = these fields MUST appear in every response
  • additionalProperties: false = no extras allowed
  • Together they give you a lockstep-defined output shape
Next up: 05 — Parse & Validate