JSON Schema conditional fields for OpenAI structured responses

I need help with creating a JSON Schema for OpenAI structured outputs where one field depends on another.

Basically I want:

  • When field_a equals “yes”, then field_b should be required.
  • When field_a equals “no”, then field_b should not exist.

I tried using allOf with if-then conditions but OpenAI rejects my schema. The allOf section seems to be the problem.

What’s the right way to write this conditional schema that OpenAI will accept?

from openai import OpenAI
import json
from dotenv import load_dotenv

load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_KEY"))

# My schema with conditional logic
response_schema = {
    "name": "process_request",
    "description": "Process user request with dependent fields",
    "parameters": {
        "type": "object",
        "properties": {
            "field_a": {
                "type": "string",
                "enum": ["yes", "no"],
                "description": "Primary response"
            },
            "field_b": {
                "type": "string",
                "enum": ["approved", "denied"],
                "description": "Secondary response needed when field_a is yes"
            }
        },
        "required": ["field_a"],
        "allOf": [
            {
                "if": {
                    "properties": {"field_a": {"const": "yes"}}
                },
                "then": {
                    "required": ["field_b"]
                }
            },
            {
                "if": {
                    "properties": {"field_a": {"const": "no"}}
                },
                "then": {
                    "not": {"required": ["field_b"]}
                }
            }
        ]
    }
}

def test_schema(prompt_text):
    try:
        completion = client.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": prompt_text}],
            response_format={
                "type": "json_schema",
                "json_schema": {
                    "name": "process_request",
                    "schema": response_schema["parameters"]
                }
            }
        )
        
        result = completion.choices[0].message.content
        data = json.loads(result)
        print(f"Success: {data}")
        
    except Exception as error:
        print(f"Error occurred: {error}")

if __name__ == "__main__":
    test_schema("Answer yes and provide the second field")
    test_schema("Answer no without the second field")

I hit this exact problem building a complex approval workflow that needed different fields based on request type. Manual validation works but turns into a mess with multiple conditional paths.

I ended up automating the whole OpenAI structured response through Latenode. Instead of wrestling with schema limitations, I built a workflow that:

  1. Sends the initial request to OpenAI with a basic schema (just field_a required)
  2. Auto-parses the response
  3. If field_a is “yes”, triggers a second OpenAI call asking for field_b
  4. Combines both responses into the final output

You get real conditional behavior without the schema headaches. Latenode handles the API calls, response parsing, and conditional logic visually. You can add validation, error handling, and retry logic if OpenAI ignores your prompt.

10 minutes to set up vs hours debugging code. The visual workflow makes changes dead simple when requirements shift.

OpenAI’s structured outputs don’t support conditional schema stuff like allOf, if-then, or not. Hit this same wall last month building a dynamic survey system. Here’s what worked: make field_b optional in your schema and handle validation after. Use this schema: {“type”: “object”, “properties”: {“field_a”: {“type”: “string”, “enum”: [“yes”, “no”]}, “field_b”: {“type”: “string”, “enum”: [“approved”, “denied”]}}, “required”: [“field_a”]}. Then validate once you get the response back. OpenAI usually follows logical flow from your prompt, so just tell it directly “when answering yes, include field_b” and it’ll comply. The schema just keeps structure clean - it won’t handle your business logic.

yea, i think keeping field_b optional and just managing the conditions in your code is much easier. forcing the schema can be tricky. gl with it!