Перейти к навигации

Why does requested JSON still contain code fences or fail parsing?

Requested JSON parses reliably only when the request enables native structured output with response_format and the model finishes the reply without truncation. Prompt wording alone still allows Markdown code fences, extra commentary, or incomplete objects when output hits a token cap.

Request structured output natively

Confirm that the target model supports native response_format on POST /v1/chat/completions, then declare the required schema in the request payload rather than relying solely on prompt text. Keep max_tokens large enough for the full JSON object while staying at or below the model’s max_output_tokens.

{
"model": "gpt-6-luna",
"messages": [
{
"role": "user",
"content": "Classify the ticket and summarize the issue in one sentence."
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "ticket_triage",
"schema": {
"type": "object",
"properties": {
"category": { "type": "string" },
"summary": { "type": "string" }
},
"required": ["category", "summary"],
"additionalProperties": false
}
}
}
}

When calling a model through Capabilities, inspect the model’s exposed Capabilities input fields before passing any format option.

Validate completion state and schema

  1. Inspect choices[0].finish_reason before parsing. Only stop indicates a complete answer; log truncation and refusals as separate states instead of running a JSON parser on partial text.
  2. Parse choices[0].message.content and validate every required field (such as category and summary) against an application schema. An HTTP 200 confirms transport delivery, not schema compliance.
  3. Route parse errors and schema validation failures to manual review so malformed records never enter downstream automation.

Before sending a paid request, inspect the model contracts for free: model:gpt-6-luna, model:gpt-image-2.5-flare-std.

Back to AI Models questions.