59API

← 返回教程列表

Structured Output and JSON Mode for LLMs

入门教程 · EN · 2026-08-04

Why structured output matters

If you are building anything that needs to pass LLM results into code, you eventually hit the same problem: natural language is flexible, but your app needs predictable fields. That is where structured output and JSON mode help. Instead of hoping the model returns something parseable, you tell it exactly what shape to follow.

This is especially useful for extraction pipelines, support triage, lead enrichment, content tagging, and workflow automation. One malformed comma should not break your production job.

In practice, JSON mode is the simpler option when you just want valid JSON. Structured output is the stronger option when you also want specific keys, types, and nested objects enforced more tightly. The best part is that you can use both patterns with the same developer workflow through 59API, which gives you low-cost access to official-quality Claude and GPT models through https://api.59api.com.

A practical example: turn messy text into JSON

Let’s say you want to extract order details from a customer email. You want fields like customer_name, order_id, issue_type, and urgency. Here is the prompt strategy:

Instruction: Return only JSON. Do not add commentary. Follow this schema: customer_name string, order_id string, issue_type string, urgency one of low/medium/high, summary string.

That instruction is good, but for reliable production code, it is better to pair it with a schema in your application layer and validate the response after the model returns it.

Example request using an OpenAI-compatible SDK with 59API:

Set the base URL to https://api.59api.com, use your 59API key, and call the model as you normally would. Because 59API is compatible with any OpenAI SDK, you do not need to rewrite your app.

Example schema object: {"type":"object","properties":{"customer_name":{"type":"string"},"order_id":{"type":"string"},"issue_type":{"type":"string"},"urgency":{"type":"string","enum":["low","medium","high"]},"summary":{"type":"string"}},"required":["customer_name","order_id","issue_type","urgency","summary"],"additionalProperties":false}

Then send your prompt with JSON mode or schema-based output. With an OpenAI-style client, the flow usually looks like: create the client, pass the system instruction, pass the customer email as the user message, and enable a JSON-focused response format when supported by the model you choose.

Example response you want: {"customer_name":"Maya Chen","order_id":"A-18342","issue_type":"late_delivery","urgency":"high","summary":"Customer says package has not arrived after 12 days and wants a replacement or refund."}

JSON mode vs structured output

Think of JSON mode as a guardrail that says, “output valid JSON only.” Structured output goes further and says, “output valid JSON that matches this exact schema.”

If you are building with Claude Code, Codex, or an OpenAI SDK, 59API is a practical relay because it stays compatible with familiar tooling while keeping costs low. That means you can test schema-driven workflows across Claude Opus, Sonnet, Haiku, Fable, and GPT models without paying premium direct pricing for every experiment.

A simple implementation pattern

Use this pattern in production:

For example, if the model returns a string where your schema expects an enum, do not silently accept it. Retry with a shorter prompt or a stronger constraint. This is where cheaper API access really helps, because a few retries on 59API still cost far less than wasting time debugging brittle prompts or overpaying for experimentation.

Why 59API is a smart choice for this workflow

Structured output is easiest to adopt when experimentation is affordable. 59API gives developers pay-as-you-go access to official-quality models at some of the lowest relay prices, with no forced downgrade in model quality. You get the same familiar SDK integration, the same base URL style, and a referral rebate that can reduce ongoing spend even further.

If you are building a parser, agent, or internal automation that depends on dependable JSON, start with 59API and test your schema prompts on real traffic. It is a low-friction way to ship reliable LLM features without inflating your inference bill. If you want to try it, sign up, plug in your key, and run your first structured output request today.

价格 · 接入文档

准备好开始了吗?

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

免费注册