Skip to main content

Represented fields

The DTO represents these fields and current Chat ↔ Responses text-format converters map json_object. Actual model/channel support is still required; not every compatible model supports JSON. Function-declaration strict limitations do not automatically apply to final-text formatting. JSON mode suits integrations needing JSON without server-side Schema constraints. When json_schema is supported and appropriate, see structured output. Do not silently downgrade failed Schema requests while business code assumes stronger validation.

Three validation layers

  1. Protocol completion: Check Chat stop, role, refusal, and tools, or Responses completed status and output items. A truncated result that happens to parse still fails completion.
  2. JSON syntax: Explicitly instruct the model to return JSON and parse complete text. Markdown fences, whitespace, adjacent objects, and malformed JSON fail; this example neither repairs nor retries them.
  3. Business shape: Valid JSON may still be an array, null, missing fields, or mistyped values. The example requires sku=NOTEBOOK, quantity as a safe integer from 0–100, and a present notes field containing a string or null. Extra fields and coercion are rejected.
notes: null differs from missing notes, and string "3" differs from integer 3. Structural validity does not establish extraction accuracy or authorize inventory changes, payments, or other actions. Use an existing Schema validation library for complex business objects.

Example boundaries

Set API_JSON_PROTOCOL=chat|responses to select one protocol; each run makes one request. Responses accepts output_text from exactly one assistant message, allowing multiple text blocks. It does not combine independent messages into one business object. Reasoning items are ignored without logging; refusals and function calls fail. JSON mode does not establish that OpenRouter’s response-healing plugin is active on this gateway. Parsing and business validation happen in the client; the gateway does not guarantee application field semantics. Parse streamed JSON only after successful completion, not each delta individually. See streaming and troubleshooting. Source: relaykit/dto/openai_request.go and Chat / Responses request converters. See the upstream JSON mode guide.

Runnable example

Run from the documentation directory; keep http-client.mjs when copying. See the REST runtime.
阅读中文