> For the complete documentation index, see [llms.txt](https://infronai.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://infronai.gitbook.io/docs/features/plugins/response-healing.md).

# Response Healing

Automatically fix malformed JSON responses

The Response Healing plugin automatically validates and repairs malformed JSON responses from AI models. When models return imperfect formatting

* Missing brackets
* Trailing commas
* Markdown wrappers
* Mixed text

This plugin attempts to repair the response so you receive valid, parseable JSON.

### Overview

Response Healing provides:

* **Automatic JSON repair**: Fixes missing brackets, commas, quotes, and other syntax errors
* **Markdown extraction**: Extracts JSON from markdown code blocks

### How It Works

The plugin activates for non-streaming requests when you use `response_format` with either `type: "json_schema"` or `type: "json_object"`, and include the response-healing plugin in your `plugins` array. See the Complete Example below for a full implementation.

### What Gets Fixed

The Response Healing plugin handles common issues in LLM responses:

#### JSON Syntax Errors

**Input:** Missing closing bracket

```json
{"name": "Alice", "age": 30
```

**Output:** Fixed

```json
{"name": "Alice", "age": 30}
```

#### Markdown Code Blocks

**Input:** Wrapped in markdown

````json
```json
{"name": "Bob"}
```
````

**Output:** Extracted

```json
{"name": "Bob"}
```

#### Mixed Text and JSON

**Input:** Text before JSON

```json
Here's the data you requested:
{"name": "Charlie", "age": 25}
```

**Output:** Extracted

```json
{"name": "Charlie", "age": 25}
```

#### Trailing Commas

**Input:** Invalid trailing comma

```json
{"name": "David", "age": 35,}
```

**Output:** Fixed

```json
{"name": "David", "age": 35}
```

#### Unquoted Keys

**Input:** JavaScript-style

```json
{name: "Eve", age: 40}
```

**Output:** Fixed

```json
{"name": "Eve", "age": 40}
```

### Complete Example

{% tabs %}
{% tab title="cURL" %}

```bash
curl https://llm.onerouter.pro/v1/chat/completions \
  -H "Authorization: Bearer $INFRON_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  --data-raw '{
  "model": "qwen/qwen3-235b-a22b-instruct-2507",
  "messages": [
    {
      "role": "user",
      "content": "Return JSON only. Categorize the ticket and return the category and priority. Ticket: I was double-charged on my bill; this needs to be handled as soon as possible."
    }
  ],
  "stream": false,
  "response_format": {
    "type": "json_object"
  },
  "plugins": [
    {
      "id": "response-healing"
    }
  ]
}'
```

{% endtab %}
{% endtabs %}

### Using with JSON Schema&#x20;

If the selected model and provider support `json_schema`, you can replace the `response_format` in the example above with the following configuration, while retaining the plugin and non-streaming parameters:

```json
{
  "type": "json_schema",
  "json_schema": {
    "name": "ticket_classification",
    "strict": true,
    "schema": {
      "type": "object",
      "properties": {
        "category": {
          "type": "string",
          "enum": ["billing", "account", "other"]
        },
        "priority": {
          "type": "string",
          "enum": ["low", "medium", "high"]
        }
      },
      "required": ["category", "priority"],
      "additionalProperties": false
    }
  }
}
```

This configuration requires `category` and `priority`, and rejects undeclared fields. Listing a field in `properties` does not make it mandatory; use `required` for that. See the [JSON Schema object reference](https://json-schema.org/understanding-json-schema/reference/object).

> **Note**
>
> Response Healing repairs JSON formatting; it does not validate the schema. Even with `strict: true`, the plugin does not check required fields, types or enum values. Structured generation capabilities still depend on the model and provider, and applications should retain result validation.

### Limitations

{% hint style="info" %}
**Non-Streaming Requests Only**

Response Healing only applies to non-streaming requests.
{% endhint %}

{% hint style="info" %}
**Will not repair all JSON**

Some malformed JSON responses may still be unrepairable. In particular, if the response is truncated by `max_tokens`, the plugin will not be able to repair it.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://infronai.gitbook.io/docs/features/plugins/response-healing.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
