If you're building anything that uses an LLM to generate structured output and stores it permanently, read this.
The Model Went Off Script - I have a system where Claude generates structured output that gets stored permanently.
The prompt is explicit about format. It ignored it anyway. This is not a bug, it is a fundamental property of LLMs. They are probabilistic, not deterministic. Even a perfect prompt will occasionally produce unexpected output.
You must validate and sanitize before anything goes permanent.
The Fix Was Too Aggressive - I added a regex strip to remove the unwanted output. What I failed to check was that the same pattern was used legitimately elsewhere in the output. The strip removed things it shouldn't have. Permanent. Gone.
What To Do Before - any LLM output touches permanent storage, validate structure and strip surgically.
Know exactly what you are removing and what you are keeping. Test the validator in isolation before deploying. The Broader Point Any project using LLMs to generate structured output -- SVG, JSON, code, HTML -- has this exposure.
The model will follow instructions almost always. Almost is not good enough when the output is permanent.
Build the safety net before you need it, not after.