To set up custom output formats with the OpenAI API, you need to design your prompts and parameters to guide the model’s response structure. The API doesn’t natively enforce strict formatting, but you can achieve consistent results by combining explicit instructions, response format parameters, and post-processing. For example, if you want JSON output, specify the structure in your prompt and use the response_format
parameter (available in newer models like gpt-4-1106-preview
or gpt-3.5-turbo-1106
) to signal that JSON is required. This approach reduces errors and ensures the model prioritizes structured outputs.
Start by crafting a system or user message that clearly defines the desired format. For instance, a system prompt like, “Always respond in JSON with keys ‘summary’ and 'keywords’,” sets expectations. Pair this with the API’s response_format={ "type": "json_object" }
parameter to enforce JSON syntax. Note that the model may still require examples to align with complex schemas. If you need XML, CSV, or other formats, provide a template in the prompt (e.g., “Use
After receiving the response, validate and parse it. For JSON, use a parser like json.loads()
in Python to catch syntax errors. If the output doesn’t match your schema, consider adding retry logic or refining your prompt. For example, if the model occasionally omits a required field, include a phrase like, “Include all fields even if empty.” Tools like JSON Schema can automate validation. For non-JSON formats, regular expressions or custom parsers can extract structured data. Testing with varied inputs helps identify edge cases. While not foolproof, this combination of clear instructions, API parameters, and validation ensures reliable custom formats for most use cases.
Zilliz Cloud is a managed vector database built on Milvus perfect for building GenAI applications.
Try FreeLike the article? Spread the word