Three stages of an AI JSON formatting pipeline
Treat AI JSON formatting as a pipeline, not a one-click action. The repair stage is permissive and tolerates broken input. The format stage assumes valid JSON and enforces consistent indentation. The validate stage rejects anything that would crash JSON.parse in your service. Splitting the work this way gives you clearer error messages when something goes wrong, because each stage only owns one type of failure.
Keeping the stages separate also makes it easy to swap any single piece. If you switch from Doubao to a different model, only the repair stage needs to learn new quirks. The formatter and validator stay the same.
When to use AI fallback vs local repair
Local repair handles the long tail of LLM output mistakes deterministically. It is fast, free and never sends your data anywhere. Reach for the AI fallback when the structure is so broken that no rule-based pass can recover the original intent — for example, when the model returned a paragraph of natural language that needs to be reshaped into a JSON object based on its meaning.
- Use local repair for: code fences, single quotes, trailing commas, comments, smart quotes, leading prose.
- Use AI fallback for: deeply nested broken arrays, model output that mixes prose and JSON, messy text-to-JSON conversions.
- Skip the AI fallback entirely if the input is already valid JSON or only needs whitespace cleanup.
Integrating the workflow into a service
If you build on top of structured outputs from Doubao or OpenAI, run repair as a defensive middleware before JSON.parse. Surface the repair logs so you can spot drifting patterns in model output, then feed those patterns back into your prompt or schema. This site mirrors the same pipeline so you can sanity-check responses by hand without writing throwaway code.