Skip to main content
Some requirements depend on a field’s value, not just its presence. If delivery_method is "shipping", you need a full address. If it is "pickup", you need a pickup location instead. JSON Schema’s if / then / else keywords let you express that branching logic directly in the schema. This is different from dependentRequired. Use dependentRequired when fields travel together based only on presence. Use if / then / else when the required fields depend on a specific value. Pydantic and Zod do not generate JSON Schema with if / then / else. If you need conditional constraints, write the JSON Schema directly.

Use case

Checkout fulfillment where delivery_method="shipping" requires address, while delivery_method="pickup" requires pickup_location.

Schema pattern

JSON Schema
{
  "type": "object",
  "properties": {
    "delivery_method": { "type": "string", "enum": ["shipping", "pickup"] },
    "address": { "type": "string", "minLength": 10, "maxLength": 200 },
    "pickup_location": {
      "type": "string",
      "enum": ["warehouse-a", "warehouse-b", "storefront"]
    }
  },
  "required": ["delivery_method"],
  "if": {
    "properties": {
      "delivery_method": { "const": "shipping" }
    },
    "required": ["delivery_method"]
  },
  "then": {
    "required": ["address"]
  },
  "else": {
    "required": ["pickup_location"]
  },
  "additionalProperties": false
}
curl
curl https://api.dottxt.ai/v1/chat/completions \
  -H "Authorization: Bearer $DOTTXT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-oss-20b",
    "messages": [{ "role": "user", "content": "Customer wants to pick up the order at warehouse-a." }],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "fulfillment_details",
        "schema": {
          "type": "object",
          "properties": {
            "delivery_method": { "type": "string", "enum": ["shipping", "pickup"] },
            "address": { "type": "string", "minLength": 10, "maxLength": 200 },
            "pickup_location": {
              "type": "string",
              "enum": ["warehouse-a", "warehouse-b", "storefront"]
            }
          },
          "required": ["delivery_method"],
          "if": {
            "properties": {
              "delivery_method": { "const": "shipping" }
            },
            "required": ["delivery_method"]
          },
          "then": {
            "required": ["address"]
          },
          "else": {
            "required": ["pickup_location"]
          },
          "additionalProperties": false
        }
      }
    }
  }'

Example outputs

Shipping:
{
  "delivery_method": "shipping",
  "address": "10 Main Street, Austin, TX 78701"
}
Pickup:
{
  "delivery_method": "pickup",
  "pickup_location": "warehouse-a"
}

Why this works

Without conditional requirements, the model might produce a shipping request with no address, or a pickup request with no pickup location. Your application would then need to infer which fields are missing and retry or repair the payload. if / then / else prevents that at generation time. The model sees that the required field set changes with delivery_method, and it produces the matching branch. Use this pattern when the decision depends on a value. For presence-only dependencies like “if vat_id exists, require billing_country”, use Field Dependencies.