Short answer
Treat model output as untrusted text. Ask for JSON, keep the raw response, parse it locally, and validate the parsed object with Pydantic. If validation fails, return an explicit error or make one bounded repair attempt. This approach does not require a provider-specific structured-output parameter. It also does not guarantee that the first model response will be valid JSON.Python example
Install the OpenAI SDK and Pydantic, then use the OpenAI-compatible Chat Completions API:Validation workflow
- Define the smallest schema that the application needs.
- Ask for JSON without adding fields that are not in the schema.
- Preserve the raw response in a protected location if your data policy allows it.
- Parse with
json.loads. - Validate with
model_validate. - Return an explicit failure or make one bounded repair attempt.
What to check when validation fails
Pydantic validates the client-side object. It does not prove that factual values inside the object are correct. Add separate business checks for IDs, permissions, totals, and other domain rules.

