Skip to content

Auto-repair and retry malformed JSON model outputs

Protect client apps from rare upstream formatting glitches with built-in schema validation, automatic repair prompts, and clean retries.

Copy the prompt and paste it into the AI assistant you already use.

Configuring resilient JSON validation & retries

  1. 01

    Set sensible max token ceilings

    Ensure max_tokens leaves generous headroom above typical payload size to prevent mid-JSON cutoffs.

  2. 02

    Cap repair attempts to two tries

    Limit automatic retries to one or two quick repair loops so tail latency stays bounded.

  3. 03

    Escalate stubborn failures

    Route failed repair attempts to a stronger fallback model instead of failing immediately.

  4. 04

    Log raw failed outputs

    Inspect captured malformed payloads in traces to tighten confusing schema field descriptions.

Common questions

Why do AI APIs still break JSON without a gateway layer?

High traffic, max-token truncation, or fallback switches to weaker models can occasionally emit unclosed braces or invalid types.

Do callers have to implement retry loops themselves?

No. APIkit intercepts schema violations on the server, runs a targeted validation-aware retry or fallback, and returns clean JSON.

How can I see how often retries are triggered?

Observability traces record validation pass rates and retry triggers per endpoint version so you can simplify fragile schemas.