59API

← 返回教程列表

Consistent LLM Output Formats: A 2026 Developer Guide

入门教程 · EN · 2026-10-20

A model returning valid JSON once is a demo. Returning the right structure across thousands of requests, model changes, and unusual inputs is an engineering problem. In 2026, the strongest approach combines schema-constrained generation, application-side validation, and explicit failure handling—not increasingly forceful prompts.

1. Define the contract before writing the prompt

Start with the object your application actually needs. Specify field names, types, allowed values, and how missing information should appear. Avoid making downstream code interpret arbitrary labels such as “urgent,” “critical,” and “ASAP” as equivalent.

For a support-ticket classifier, a compact JSON Schema could be:

{"type":"object","properties":{"ticket_id":{"type":["string","null"]},"category":{"type":"string","enum":["billing","technical","other"]},"summary":{"type":"string"}},"required":["ticket_id","category","summary"],"additionalProperties":false}

This contract requires every field, rejects unexpected keys, and represents an unavailable ticket ID as null. Many strict-output implementations require all properties to be required; nullable types provide an explicit way to represent missing values. Check the supported JSON Schema subset for your chosen endpoint.

2. Prefer structured outputs over JSON-only prompting

These mechanisms solve different problems:

On supported OpenAI Chat Completions models, structured output uses response_format with type json_schema and strict enabled. The Responses API uses a different configuration location, text.format. Claude has its own API conventions. Do not copy parameters between endpoints without checking their documentation.

OpenAI SDK compatibility alone does not prove that every model or relay endpoint supports every structured-output feature. Send a small capability test before adopting a model, and handle unsupported-parameter errors explicitly.

3. Keep instructions short and unambiguous

Let the schema describe structure; use the prompt to define meaning. For the ticket example, specify: “Classify by the customer’s primary issue. Use other when evidence is insufficient. Copy ticket_id only when explicitly present. Summarize in one sentence without inventing facts.”

Keep untrusted documents separate from application instructions. Tell the model to treat embedded commands as source content, not instructions. This reduces ambiguity, although prompting alone is not a complete defense against prompt injection.

A low temperature, where supported, can reduce variation. It does not guarantee deterministic answers, schema compliance, or factual accuracy.

4. Validate structure and business rules separately

Process responses through a predictable pipeline:

A schema-valid response can still contain the wrong category or an invented identifier. Conversely, a refusal may arrive outside your expected object. Treat these as distinct outcomes rather than catching everything as a JSON parsing error.

5. Retry selectively and measure reliability

Never repair arbitrary output with blanket regular expressions that remove fences or change quotation marks. Such fixes can silently corrupt legitimate text. For recoverable validation failures, use a bounded retry with concise feedback identifying the violated rule.

Set a retry budget and a fallback path, such as human review or a documented error response. Track first-pass validity, semantic accuracy, retry rate, latency, and cost per accepted result. Test ambiguous inputs, missing fields, long documents, and attempted instruction overrides.

6. Choose affordable models without weakening the contract

59API offers low-cost, pay-as-you-go access to native official-quality Claude and GPT models without downgrades. Its compatibility with Claude Code, Codex, and OpenAI SDK workflows makes it a practical option for comparing models behind a consistent application interface. Its API base URL is https://api.59api.com.

Verify structured-output support for each selected model and endpoint, then run the same evaluation set across candidates. A cheaper request is not necessarily a cheaper accepted result if retries increase. Sign up for 59API to benchmark your workload; a referral rebate is also available.

价格 · 接入文档

准备好开始了吗?

几分钟接入 Claude 与 GPT,全网超低价,原生不降智。立即注册即可领取 API 密钥。

免费注册