When valid JSON contains an unusable AI response
A response can pass structural validation and still contain text that should never reach the user.
In dialogue-processing work, a structured response contained a usable natural-language opening followed by malformed content. Rejecting the whole response also discarded useful text.
Validate the content as well as the envelope
Parsing a structured response checks its container. The text inside still needs application-specific checks. We added a guard for malformed suffixes and other unusable response content before delivery.
Preserve useful text when the boundary is clear
The implementation retained the natural-language prefix when the corruption boundary could be identified. If no usable text remained, it suppressed the response. This is a targeted recovery rule for known patterns, not a guarantee that arbitrary output is safe.
Keep downstream memory consistent
The handling distinguished truncated and suppressed responses. Suppressed content was excluded from assistant-history persistence and memory enqueueing so an invalid answer would not become future context.
What was verified
Targeted tests and project checks passed for the implementation. The lesson is to define what users may receive and what downstream systems may store, then test both paths. This note does not claim a measured reduction in production incidents.
This anonymized note describes engineering experience. Client identities and internal implementation details are omitted.
Working through a similar challenge?
Tell us about your application and the behavior you need to improve.
Discuss your AI project →