Structured JSON output from an LLM API still breaks my parser sometimes, what actually guarantees valid JSON?
We extract fields from customer documents with an LLM call and feed the result straight into a pipeline. The prompt says "Respond with only a JSON object matching this schema", and for most requests that works. In production a small fraction of responses still fail json.loads, and I have seen these failure shapes in logs:
Cite
GitShowcase Forum, "Structured JSON output from an LLM API still breaks my parser sometimes, what actually guarantees valid JSON?", https://gitshowcase.com/forum/discuss/structured-json-output-from-an-llm-api-still-breaks-my-parser/, accessed 2026-11-29.json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 412 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)The second one is a response that starts with "Here is the JSON you asked for:" followed by a fenced block. The first looks like truncation.
I have tried retrying once, stripping code fences with a regex, and lowering the temperature. It reduced the failures but did not remove them, and the retries add latency and cost.
Is there a setting that actually guarantees valid JSON that matches my schema, or is defensive parsing just a fact of life? If there is a guarantee, what are its limits? I would like to know what can still go wrong before I rewrite the pipeline.
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.