Choose an output mode
A compatible endpoint does not imply
json_schema support for every model. Test the target model with a minimal Schema. Resolve unsupported-parameter errors before changing modes; avoid silently accepting invalid business data after a fallback.
Runnable example: extract a product
Complete the TypeScript SDK setup. Save asstructured-output.mts and run node structured-output.mts. The example requests a product name and quantity using strict Schema, then independently checks a nonempty name and a nonnegative safe integer quantity.
Handle failures
finish_reason=length: the output may be truncated; do not repair the JSON and treat it as complete.message.refusalor absent content: use a refusal/failure branch before parsing.- Unsupported Schema or failed validation: retain a failed state, inspect the request and model capabilities, and avoid unbounded retries.
- Streaming: JSON fragments generally cannot be parsed separately. Confirm completion before parsing the full text; discard interrupted structured results.
additionalProperties: false on objects and properties listed in required. Nullable fields need an appropriate Schema and support from the selected model.
Responses uses a different field
Chat Completions usesresponse_format: {type: "json_schema", json_schema: {...}}. Responses uses text: {format: {type: "json_schema", name, schema, strict}}. Do not copy response_format unchanged into a Responses request.
API parameters · Errors and retries
TypeScript: structured extraction with Responses
Save asresponses-structured.mts, configure the TypeScript SDK, and run node responses-structured.mts. The channel must support strict Schema through Responses text.format; Chat Completions support alone does not establish this capability.
output[].content[] even when the overall status is completed.
Reference: OpenAI structured outputs.
简体中文