> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Response Healing

> Repair malformed JSON in LLM output with the response_healing plugin in the AI Gateway, fixing code fences, trailing commas, and missing brackets.

The `response_healing` **plugin** repairs malformed JSON in model output before the response reaches the caller. Models asked for structured output sometimes wrap it in a markdown code fence, add a sentence of explanation around it, or emit a trailing comma. The plugin extracts and repairs the JSON so the response parses.

> [!NOTE]
>
> This feature is in Beta.

## Use cases

* Getting reliable structured output from models with weaker JSON adherence.
* Removing per-service JSON cleanup code from application clients.
* Recovering tool calls whose arguments came back slightly malformed.

## Quick start

Add a `response_healing` entry to the `plugins` array.

```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl -X POST https://router.toqan.ai/eu/v3/router/chat/completions \
  -H "Authorization: Bearer $TOQAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-5.4-mini",
    "messages": [{ "role": "user", "content": "List two colors as JSON." }],
    "response_format": { "type": "json_object" },
    "plugins": [{ "id": "response_healing" }]
  }'
```

```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.TOQAN_API_KEY,
  baseURL: 'https://router.toqan.ai/eu/v3/router',
});

const response = await client.chat.completions.create({
  model: 'openai/gpt-5.4-mini',
  messages: [{ role: 'user', content: 'List two colors as JSON.' }],
  response_format: { type: 'json_object' },
  // @ts-ignore - Toqan Router extension
  plugins: [{ id: 'response_healing' }],
});
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from openai import OpenAI
import os

client = OpenAI(
    api_key=os.environ.get("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router",
)

response = client.chat.completions.create(
    model="openai/gpt-5.4-mini",
    messages=[{"role": "user", "content": "List two colors as JSON."}],
    response_format={"type": "json_object"},
    extra_body={"plugins": [{"id": "response_healing"}]},
)
```

The plugin takes no configuration.

`response-healing`, the hyphenated spelling used by OpenRouter, is accepted as an alias for `response_healing`. Payloads migrated from OpenRouter work without edits.

## What gets repaired

| Problem | Model output | Response |
| - | - | - |
| Markdown code fence | JSON wrapped in a ` ```json ` block | `{"a": 1}` |
| JSON embedded in prose | `Here you go: {"a": 1} Hope that helps.` | `{"a": 1}` |
| Missing closing bracket | `{"a": 1` | `{"a": 1}` |
| Trailing comma | `{"a": 1,}` | `{"a": 1}` |
| Unquoted keys | `{a: 1}` | `{"a": 1}` |
| Single-quoted strings | `{'a': 'b'}` | `{"a": "b"}` |

## When it applies

The plugin repairs two things:

* **Message content**, only when the request asks for JSON. On `/v3/router/chat/completions` that means `response_format` is `json_object` or `json_schema`. On `/v3/router/responses` it means `text.format` declares `type: json_schema`, or carries a non-empty inline `schema` body without an explicit type. Requests that ask for plain text are never modified.
* **Tool call arguments**, whenever the plugin is enabled. Arguments are JSON by definition, so no `response_format` is required.

## Limits

* **Non-streaming requests only.** Streamed chunks reach the caller before the full body is known, so there is nothing left to repair. Setting the plugin on a streaming request has no effect.
* **Output truncated by `max_tokens` may be unrecoverable.** A response cut mid-string cannot be reconstructed.
* **Unrepairable output passes through unchanged.** The plugin never fails a request, and never replaces valid JSON.

## Enable for a workspace

Enable response healing for every request from **Settings** > **Plugins**, without passing a `plugins` array on each call. Once enabled, it applies automatically to every call that doesn't already specify a `response_healing` plugin.

## Apply per routing rule

Attach a `response_healing` entry to a [Routing Rule](/docs/ai-gateway/configuration/routing-rules.md) to run healing on the traffic that rule matches.

## Supported endpoints

* [`POST /v3/router/responses`](/docs/reference/responses/create-response)
* [`POST /v3/router/chat/completions`](/docs/reference/chat/create-chat-completion)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.