# openai.BadRequestError: Invalid schema for response_format: 'properties' must be an object with string keys.

- **ID:** `llm/structured-output-schema-mismatch`
- **Domain:** llm
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

The JSON schema provided in the response_format parameter is malformed, e.g., using integer keys or missing required fields like 'type'.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| openai>=1.0.0 | active | — | — |
| openai>=1.12.0 | active | — | — |

## Workarounds

1. **Use the Pydantic library to generate the schema automatically and validate it:
from pydantic import BaseModel
from openai import OpenAI

class ResponseModel(BaseModel):
    name: str
    age: int

client = OpenAI()
schema = ResponseModel.model_json_schema()
print(schema)  # Check that 'properties' has string keys
response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'user', 'content': 'Extract info'}],
    response_format={
        'type': 'json_schema',
        'json_schema': {'name': 'response', 'schema': schema}
    }
)** (95% success)
   ```
   Use the Pydantic library to generate the schema automatically and validate it:
from pydantic import BaseModel
from openai import OpenAI

class ResponseModel(BaseModel):
    name: str
    age: int

client = OpenAI()
schema = ResponseModel.model_json_schema()
print(schema)  # Check that 'properties' has string keys
response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'user', 'content': 'Extract info'}],
    response_format={
        'type': 'json_schema',
        'json_schema': {'name': 'response', 'schema': schema}
    }
)
   ```
2. **Manually ensure the schema has the correct structure:
{
    'type': 'object',
    'properties': {
        'name': {'type': 'string'},
        'age': {'type': 'integer'}
    },
    'required': ['name', 'age']
}** (90% success)
   ```
   Manually ensure the schema has the correct structure:
{
    'type': 'object',
    'properties': {
        'name': {'type': 'string'},
        'age': {'type': 'integer'}
    },
    'required': ['name', 'age']
}
   ```

## Dead Ends

- **** — OpenAI's schema validation is stricter than standard JSON; it requires specific keys like 'type': 'object' and 'properties' must be an object. (50% fail)
- **** — Older models may not support the same schema features; the schema must comply with OpenAI's JSON Schema subset. (40% fail)
