# openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid 'response_format': 'type' must be one of ['text', 'json_object', 'json_schema'].", 'type': 'invalid_request_error'}}

- **ID:** `llm/openai-structured-output-enum-violation`
- **Domain:** llm
- **Category:** config_error
- **Error Code:** `OAI-ERR-0400`
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

The response_format.type parameter is set to an invalid or unsupported value (e.g., 'json' instead of 'json_object' or 'json_schema'), or the model version does not support the specified format type.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| openai==1.30.0 | active | — | — |
| openai==1.35.0 | active | — | — |
| gpt-4o-mini-2024-07-18 | active | — | — |

## Workarounds

1. **Correct the response_format to use a valid type: `response_format={"type": "json_object"}` for unstructured JSON, or `response_format={"type": "json_schema", "json_schema": {"name": "my_schema", "schema": {...}}}` for structured JSON. Example: `client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello"}], response_format={"type": "json_object"})`.** (98% success)
   ```
   Correct the response_format to use a valid type: `response_format={"type": "json_object"}` for unstructured JSON, or `response_format={"type": "json_schema", "json_schema": {"name": "my_schema", "schema": {...}}}` for structured JSON. Example: `client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello"}], response_format={"type": "json_object"})`.
   ```
2. **If using 'json_schema', ensure the model supports it (gpt-4o-mini and later, or gpt-4-turbo with specific versions). Check the model's capabilities via the API: `model = client.models.retrieve("gpt-4o-mini"); print(model.supported_features)`.** (85% success)
   ```
   If using 'json_schema', ensure the model supports it (gpt-4o-mini and later, or gpt-4-turbo with specific versions). Check the model's capabilities via the API: `model = client.models.retrieve("gpt-4o-mini"); print(model.supported_features)`.
   ```
3. **For legacy models that don't support 'json_schema', fall back to 'json_object' and parse the output manually with a JSON schema validator like `jsonschema.validate()`.** (80% success)
   ```
   For legacy models that don't support 'json_schema', fall back to 'json_object' and parse the output manually with a JSON schema validator like `jsonschema.validate()`.
   ```

## Dead Ends

- **** — The API strictly requires the 'type' key; an empty dict causes a different validation error: 'response_format must be a valid object with a type field'. (90% fail)
- **** — The API only accepts 'text', 'json_object', or 'json_schema' (for newer models). 'json' is not recognized and triggers the exact error. (95% fail)
- **** — Older versions don't support 'json_schema' at all, and may have different validation rules. This breaks other features and is not a sustainable fix. (85% fail)
