← BlogAI Automation

Why Your AI Agent Keeps Returning Broken JSON (And How to Actually Fix It)

By Aditya JhaJuly 30, 20268 min read

Why Your AI Agent Keeps Returning Broken JSON (And How to Actually Fix It)

A team builds an n8n workflow where an AI agent reads an incoming email or document, extracts the relevant fields, and hands back JSON that a downstream node uses to update a CRM record or an expense line. It works cleanly through testing and the first few weeks in production. Then, without any code change, one response comes back with a trailing sentence before the opening brace, or a number formatted as a string, or a field silently missing, and the downstream node either throws an error or, worse, writes a malformed record that nobody notices until someone questions the data weeks later. The AI model didn't malfunction. It was never actually guaranteed to produce valid JSON in the first place, it was only asked to.

Why does an LLM sometimes return broken or invalid JSON?

Because a large language model is a text predictor, not a JSON serializer. When a prompt simply instructs it to "respond in JSON," the model is generating tokens one at a time that happen to look like JSON, with no built-in guarantee of syntactic validity or schema conformance behind that instruction.

Small deviations slip through more often as the requested structure gets more nested, as the response gets long enough to run into a token limit mid-object, or as the model has to choose between two plausible field names or types it wasn't given a hard constraint against.

What's the difference between older 'JSON mode' and real structured outputs?

Earlier JSON mode implementations only guaranteed the output would parse as syntactically valid JSON, they said nothing about whether it matched the shape your code actually expects. OpenAI's Structured Outputs guide is explicit about this distinction: Structured Outputs is the evolution of JSON mode, and while both ensure valid JSON is produced, only Structured Outputs ensures the response also adheres to a schema you define.

The mechanism behind that guarantee is constrained decoding: the API takes your JSON schema and restricts which tokens the model is even allowed to generate at each step, so the output can't drift outside a path that's valid for that schema, rather than hoping a plain-language instruction was followed.

What causes schema-valid JSON that's still wrong?

  • Missing required fields the schema didn't explicitly mark as required, so the model treats them as optional.
  • Wrong types, a string where a number was expected, common when a field could plausibly be represented either way.
  • Extra, unexpected fields slipping in when the schema doesn't lock down additionalProperties, which Claude's strict tool use documentation calls out directly: strict schema enforcement requires setting additionalProperties to false on every object and listing every field in the required array.
  • Truncation, where the response is cut off mid-object at the token limit and can't be repaired after the fact, since there's no way to recover a half-written JSON structure at the application layer.

How do you actually make agent-to-automation JSON reliable?

  • Use the provider's native structured output or strict tool-calling mode instead of a plain "respond in JSON" instruction in the prompt, so the schema is enforced at the token level, not just requested in English.
  • Validate every response against the schema in code before it touches a downstream system, treating the boundary between the model and your automation as a strict API contract, not a best-effort parse.
  • Set the max token limit with real headroom above your expected schema size, so truncation can't silently produce a half-written object your parser then has to guess at.
  • On a validation failure, retry once with the specific validation error fed back to the model so it can self-correct, and only after that dead-letter the record for human review instead of letting a bad write reach your CRM or accounting system unchecked.

How AIBOOTSTRAPPER solved this for a client

Expensorr, the AI-powered expense management product AIBOOTSTRAPPER built end to end, ingests structured expense line items pulled from receipts and documents by an AI extraction step, and a malformed record here doesn't fail loudly, it can quietly corrupt a user's spend history in a way nobody notices until the numbers stop reconciling. That's exactly why schema-first validation on every AI-to-database write, not just a best-effort JSON parse, was core to how the ingestion layer was designed.

It's the same trigger-layer discipline covered in securing an n8n webhook with HMAC verification: don't just check that a request or a response arrived, verify it actually is what your system expects before anything downstream acts on it.

How AIBOOTSTRAPPER helps

An AI agent that returns valid JSON 98% of the time will still break a production automation eventually, and the failure usually surfaces as bad data, not a loud error. AIBOOTSTRAPPER's AI automation team builds structured output enforcement, schema validation and retry logic into every agent-to-workflow handoff by default.

If you have an n8n or agent workflow parsing AI output today and you're not sure what happens the next time it comes back malformed, book a call and we'll audit the handoff before it costs you a bad record.

Want this done for you?

Book a free strategy call and we'll show you how to build and market your business with AI.

FAQ

Questions, answered

Everything you might want to know before we hop on a call.

Because an LLM is a text predictor, not a JSON serializer. A plain prompt instruction to 'respond in JSON' has no built-in guarantee of validity, so deviations like extra commentary, wrong types, missing fields, or mid-object truncation can slip through, especially as the schema gets more complex.

JSON mode only guarantees the output is syntactically valid JSON. Structured outputs go further and guarantee the JSON also conforms to a schema you define, enforced by constraining which tokens the model can generate at each step, not just by instruction.

It's the strongest single layer, but best practice pairs it with in-code schema validation before the data reaches a downstream system, a retry loop that feeds validation errors back to the model, and a dead-letter path for records that still fail after a retry.

Yes. If the response hits the max token limit mid-object, the output is cut off and cannot be repaired after the fact. Setting a token limit with real headroom above your expected schema size is the fix.

Keep reading

Let's talk

Ready to build and sell with AI?

Book a free 30 minute strategy call. We'll map the highest ROI AI move for your business, no pitch, just value.