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

# Response Healing | Plugins | AI Studio

> Repair malformed JSON in model output using the response_healing plugin in the AI Gateway.

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.</Note>

## 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.

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST https://api.orq.ai/v3/router/chat/completions \
    -H "Authorization: Bearer $ORQ_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-4o-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.ORQ_API_KEY,
    baseURL: 'https://api.orq.ai/v3/router',
  });

  const response = await client.chat.completions.create({
    model: 'openai/gpt-4o-mini',
    messages: [{ role: 'user', content: 'List two colors as JSON.' }],
    response_format: { type: 'json_object' },
    // @ts-ignore - orq.ai 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("ORQ_API_KEY"),
      base_url="https://api.orq.ai/v3/router",
  )

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

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** > **Organization** > **Plugins**, without passing a `plugins` array on each call. Once enabled, it applies automatically to all **AI Gateway** calls that don't already specify a `response_healing` plugin.

## Supported endpoints

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