Skip to main content
Gatelit normalizes structured output across all providers. You configure it once with the outputSchema option, and the gateway handles the provider-specific translation — whether you are using OpenAI, Anthropic, Google, or Mistral.

The outputSchema option

Add outputSchema to any client.chat() or client.stream() call:
There are two modes:

json_object mode

The model is instructed to return valid JSON of any shape. Use this when you want JSON output but do not need to enforce a specific schema:

json_schema mode

The model is constrained to match a specific JSON Schema. Provide the schema inline alongside mode: "json_schema":

Automatic parsing with chatJson<T>()

client.chatJson<T>() is a convenience wrapper around chat() that parses the response content as JSON and returns it typed — no JSON.parse required:

Complete example

Here is a full end-to-end example that asks a model to evaluate a piece of text and return a structured assessment:

Cross-provider compatibility

Structured output normalization is applied by the gateway, not the SDK. The same outputSchema option works identically whether you target OpenAI, Anthropic, Google, or Mistral — the gateway translates it to each provider’s native format before forwarding the request.

Error handling

If the model returns content that is not valid JSON, chatJson() throws a GatelitError with code: "invalid_json". This can happen when a model ignores the structured output instruction despite being asked for JSON. Always wrap chatJson() calls in a try/catch when building production features: