How to intermediate · 3 min read

How to validate structured output against business rules

Quick answer
Use structured output from AI models and validate it in Python by applying explicit business rules as conditional checks or schema validations. Combine JSON schema validation libraries with custom logic to enforce rules on AI-generated data.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0 jsonschema

Setup

Install the required Python packages and set your environment variable for the OpenAI API key.

bash
pip install openai jsonschema

Step by step

This example shows how to request structured JSON output from the gpt-4o model and validate it against business rules using the jsonschema library and custom Python logic.

python
import os
from openai import OpenAI
import json
from jsonschema import validate, ValidationError

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Define JSON schema for expected structured output
schema = {
    "type": "object",
    "properties": {
        "customer_id": {"type": "string", "pattern": "^CUST-[0-9]{4}$"},
        "order_amount": {"type": "number", "minimum": 0},
        "priority": {"type": "string", "enum": ["low", "medium", "high"]}
    },
    "required": ["customer_id", "order_amount", "priority"]
}

# Define business rule: order_amount must be at least 100 if priority is high

def business_rules(data):
    if data["priority"] == "high" and data["order_amount"] < 100:
        raise ValueError("order_amount must be at least 100 for high priority orders")

# Prompt requesting structured JSON output
prompt = (
    "Generate a JSON object with customer_id, order_amount, and priority. "
    "customer_id format: CUST- followed by 4 digits. "
    "Priority must be one of low, medium, or high."
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}],
    temperature=0
)

# Parse the model's response
text = response.choices[0].message.content

try:
    data = json.loads(text)
except json.JSONDecodeError as e:
    raise ValueError(f"Invalid JSON output: {e}")

# Validate JSON schema
try:
    validate(instance=data, schema=schema)
except ValidationError as e:
    raise ValueError(f"Schema validation error: {e.message}")

# Validate business rules
business_rules(data)

print("Validated structured output:", data)
output
Validated structured output: {'customer_id': 'CUST-1234', 'order_amount': 150, 'priority': 'high'}

Common variations

You can adapt validation for asynchronous calls, use other models like claude-3-5-sonnet-20241022, or integrate streaming output validation. For Anthropic, use the system= parameter and client.messages.create(). Schema validation libraries like pydantic or cerberus offer alternatives to jsonschema.

python
import anthropic
import os

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

system_prompt = "You are a helpful assistant that outputs JSON with customer_id, order_amount, and priority."
user_prompt = "Generate a valid JSON object with the specified fields."

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=512,
    system=system_prompt,
    messages=[{"role": "user", "content": user_prompt}]
)

text = response.content[0].text

# Then parse and validate as shown in the previous example

Troubleshooting

  • If JSON parsing fails, verify the model prompt explicitly requests JSON output and consider using temperature=0 for deterministic results.
  • Schema validation errors indicate the output structure or types do not match expectations; update your schema or prompt accordingly.
  • Business rule violations require refining your validation logic or adjusting the prompt to enforce stricter output constraints.

Key Takeaways

  • Always define a strict JSON schema to validate AI structured output format.
  • Implement custom business rules as Python functions to enforce domain-specific constraints.
  • Use deterministic model parameters like temperature=0 to improve output consistency.
  • Combine schema validation with business logic checks for robust data validation.
  • Adjust prompts to guide models toward producing valid, rule-compliant structured data.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗