AI Interview Prep

AI Interview Prep

LLM Inference Interview Questions #10 - The JSON Serialization Trap

Why stuffing multi-line code into JSON strings silently pushes your model off-manifold, and how separating the metadata envelope from the raw payload instantly stops escape-character hallucinations.

Hao Hoang's avatar
Hao Hoang
Aug 08, 2026
∙ Paid

You’re in a Senior AI Engineer interview at Anthropic and the interviewer asks:

“Your coding agent keeps failing on multi-line Python, escaped quotes, mangled indentation. A teammate says just use a stronger model. What’s the real fix?”

Most candidates say: “Add a JSON repair step and retry the call.”

Wrong instinct. You just built a band-aid around a design flaw.

Here’s what’s actually happening.

This isn’t a reasoning failure. It’s a serialization failure. The model knows the code. It fails at the transport layer.

JSON demands every " becomes \". Every newline becomes \n. Every backslash doubles. Your model isn’t writing Python anymore, it’s writing an encoded version of Python, one escape token at a time.

You’re asking a chef to pass you a soufflé through a letterbox.

Three things break:

a) Out-of-distribution encoding. Pretraining saw billions of lines of raw Python. It saw far less \"\"\"Docstring.\"\"\"\n return x. You moved the payload off-manifold.

b) Silent corruption. A dropped backslash doesn’t throw. The JSON parses. The patch applies. The indentation is wrong, and CI finds out 40 minutes later.

c) Escape tax. Escaped code inflates token count, and every escape is another chance to break.

The fix, in priority order:

AI Interview Prep is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.

User's avatar

Continue reading this post for free, courtesy of Hao Hoang.

Or purchase a paid subscription.
© 2026 Hao Hoang · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture