Response Healing

Auto-repair malformed JSON before your parser runs

Response healing is a built-in skill managed by TheRouter, designed for strict machine-consumed outputs. It attempts to recover valid JSON from responses with trailing commas, unquoted keys, markdown wrappers, or minor truncation artifacts. Execution happens entirely inside TheRouter infrastructure.

response-healing.json
{
  "model": "google/gemini-2.5-flash",
  "messages": [{ "role": "user", "content": "Return a product card as JSON." }],
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "product_card",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {
          "name": { "type": "string" },
          "price_usd": { "type": "number" }
        },
        "required": ["name", "price_usd"],
        "additionalProperties": false
      }
    }
  },
  "skills": [{ "id": "response-healing" }]
}
Safe parse with retry
async function createStructuredResponse(payload: unknown) {
  for (let i = 0; i < 2; i += 1) {
    const res = await client.chat.completions.create(payload as never);
    const content = res.choices[0]?.message?.content ?? "{}";
    try {
      return JSON.parse(content);
    } catch {
      if (i === 1) {
        throw new Error("Response remained invalid after healing retry.");
      }
    }
  }
}
Non-streaming only
Healing is applied on completed non-streaming responses. For streaming JSON assembly, continue validating chunks client-side and fail closed on invalid payloads.