In the openai chat create api is says: "" responseformat object Optional An object…
- User
In the openai chat create api is says:
"" response_format object
Optional An object specifying the format that the model must output.
Setting to { "type": "json_schema", "json_schema": {...} } enables Structured Outputs which ensures the model will match your supplied JSON schema. Learn more in the Structured Outputs guide.
Setting to { "type": "json_object" } enables the older JSON mode, which ensures the message the model generates is valid JSON. Using json_schema is preferred for models that support it.
Hide possible types Text object Default response format. Used to generate text responses.
Hide properties type string
Required The type of response format being defined. Always text.
JSON schema object JSON Schema response format. Used to generate structured JSON responses. Learn more about Structured Outputs.
Show properties JSON object object JSON object response format. An older method of generating JSON responses. Using json_schema is recommended for models that support it. Note that the model will not generate JSON without a system or user message instructing it to do so.
Hide properties type string
Required The type of response format being defined. Always json_object. ""
What is the default response format? My code look like this:
response_format={"type": "json_object"} if res_format is not None else str # TODO : check - Assistant
Good catch — the default response_format in the OpenAI Chat Completions API is text, not JSON. Let’s unpack what’s happening in your code: pythonCopy coderesponse_format={"type": "json_object"} if res_format is not None else str
- What the API expects The API expects response_format to be an object, not a Python type. The valid options are:
Default (text) → omit response_format entirely, or set: pythonCopy coderesponse_format={"type": "text"}
JSON (structured output, legacy mode) → pythonCopy coderesponse_format={"type": "json_object"}
JSON schema (new structured output mode) → pythonCopy coderesponse_format={ "type": "json_schema", "json_schema": {...} }
- Problem with your current code When res_format is None, you’re passing str, which is not valid — the API expects a dictionary, not a type. ✅ Correct version Here’s how you should write it: pythonCopy coderesponse_format = {"type": "json_object"} if res_format is not None else {"type": "text"}
Or even better — since "text" is the default, you can just omit it when not needed: pythonCopy codeif res_format is not None: response_format = {"type": "json_object"} else: response_format = None # API will default to text
🔍 Summary Caseresponse_format valueOutput typeOmitted / None(default)Plain text{"type": "json_object"}Legacy JSON (valid JSON object string){"type": "json_schema", "json_schema": {...}}Structured JSON (schema-validated) So in your case: ➡️ Replace str with {"type": "text"} or None.