Skip to main content

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:
Use the complete Model ID from the model plaza. Keep the BetterToken API Key in an environment variable, not in source code.

Validation workflow

  1. Define the smallest schema that the application needs.
  2. Ask for JSON without adding fields that are not in the schema.
  3. Preserve the raw response in a protected location if your data policy allows it.
  4. Parse with json.loads.
  5. Validate with model_validate.
  6. Return an explicit failure or make one bounded repair attempt.
Do not silently replace missing values, invent defaults, or retry forever. A repaired object must pass the same schema as the first response.

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.

Capability boundary

The published BetterToken Chat Completions contract documents the OpenAI-compatible request and text response. Provider-specific structured-output fields can vary by model and route. Do not send an undocumented field unless the selected model and current API reference explicitly support it.