orq.aiAdvent of RoutingDocs

Door 10 · still sealed

Hands off the wax, elf. This door opens on Dec 10.

-days--hours--min--sec

Rudolph is still polishing this one. Back to the calendar

Door 10 · Dec 10 Structured outputs & response healing

Gift tags must parse

Ask for JSON, switch on response healing, and the router strips stray prose, fences and trailing commas before your app parses the reply.

The gift-tag printer reads every tag straight from the model. Four jams this hour, and a reindeer is already chewing an unlabelled parcel.

  1. Sprocket

    Jammed again! The model writes Here you go! before every tag. Very festive. Very unparseable. And this one ends in a trailing comma.

  2. Mrs. Claus

    Before anyone fixes it: what happens to a tag nobody can read? Somewhere a child unwraps someone else's present.

  3. Sprocket

    I'll write a regex that scrapes the JSON out! One little regex. What could go wrong?

  4. Tinsel

    Then you'd have two problems. Ask for JSON so the model knows the format, and add response_healing so the router mends the syntax.

  5. Mrs. Claus

    It mends the wrapping, not the present, dear. A tag it cannot mend arrives as it was, so the printer keeps its own check.

requestYour appresponse_format + pluginmodelopenai/gpt-6-lunaHere you go! {"gift":"sled",}orq.aiResponse healingfences · commas · quotesresponseJSON that parsesresponseOriginal outputraw replyrepairedas-is
  1. requestYour appresponse_format + plugin
  2. modelopenai/gpt-6-lunaHere you go! {"gift":"sled",}
  3. orq.aiResponse healingfences · commas · quotes
  4. responseJSON that parses
    responseOriginal output
The model gift-wraps its tag in a cheerful sentence and a trailing comma; the router unwraps it into JSON that parses, or hands the reply on as-is when it cannot.

How it works

Tinsel’s fix is two settings on one request: one shapes the reply, the other mends it. They do different jobs:

  1. Schema. Structured outputs hand the model a JSON Schema. On Chat Completions you set response_format: json_object for any valid JSON, or json_schema for a fixed shape. On the Responses API the same thing goes in text.format. With strict: true the model follows the exact schema; only a subset of JSON Schema is supported then. Without it, the schema only guides the model.
  2. Healing. Add "plugins": [{"id": "response_healing"}]. The plugin takes no configuration. Before the response reaches you, it checks the message content (when the request asked for JSON) and tool-call arguments (always, since they are JSON by definition).
  3. Repair or pass. If the JSON is broken in a known way, the plugin extracts and repairs it. It never replaces valid JSON. Output it cannot repair passes through unchanged, and the plugin never fails a request.

Response healing is in Beta. It works on POST /v3/router/chat/completions and POST /v3/router/responses.

Try it with curl

curl -X POST https://my.orq.ai/v3/router/chat/completions \
  -H "Authorization: Bearer $ORQ_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "openai/gpt-6-luna",
    "messages": [{
      "role": "user",
      "content": "Gift tag: Tim, 7, red sled. JSON: name, age, gift."
    }],
    "response_format": {"type": "json_object"},
    "plugins": [{"id": "response_healing"}]
  }'

Or from the OpenAI SDK

plugins is not an OpenAI request field, so pass it in extra_body. The parse helper sends your Pydantic model as a schema with strict: true:

import os
from openai import OpenAI
from pydantic import BaseModel

client = OpenAI(
    api_key=os.environ["ORQ_API_KEY"],
    base_url="https://my.orq.ai/v3/router",
)

class GiftTag(BaseModel):
    name: str
    age: int
    gift: str

response = client.responses.parse(
    model="openai/gpt-6-luna",
    input="Gift tag: Tim, 7, gets a red sled.",
    text_format=GiftTag,
    extra_body={"plugins": [{"id": "response_healing"}]},
)

tag = response.output_parsed
print(tag.name, tag.age, tag.gift)

If the reply still does not parse, parse raises, so wrap the call in your existing error handling.

What gets repaired

From the docs’ repair table:

Problem Model output Response
Markdown code fence JSON inside a json code 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"}

Every row in the table is a syntax repair. Use the schema with strict: true to control which fields come back and their types.

When to use it

  • Models with weaker JSON adherence that still need to return structured output.
  • Removing per-service JSON cleanup code from your clients.
  • Recovering tool calls whose arguments came back slightly malformed.

Streaming and cut-off replies

Healing works on non-streaming requests only. Streamed chunks reach your app before the full body is known, so setting the plugin on a streaming request has no effect.

Output cut off by max_tokens may be unrecoverable: a response cut mid-string cannot be reconstructed. If replies hit the limit, raise it.

The plugin also accepts response-healing, the hyphenated spelling OpenRouter uses, as an alias.

Nice list

  • Set strict: true when your code reads the fields. It defaults to false, and then the schema only guides the model.
  • List every property in required and set additionalProperties: false, so the model cannot skip a field or invent a new one. The docs' quick-start schema does both.
  • Keep your own parse check. Output the plugin cannot repair passes through unchanged, and the plugin never fails the request.

Naughty list

  • Adding healing to a streaming request. Chunks reach you before the full body is known, so the plugin has no effect.
  • Setting a tight max_tokens and counting on healing. A reply cut off mid-string cannot be reconstructed.
  • Expecting healing to add a missing field or fix a wrong type. Every repair in the docs' table is syntax: fences, prose, brackets, commas and quotes.
  • Writing a regex in every service to strip code fences and chatter. The plugin does that cleanup once, on the router, for every client.

Elf's challenge

Three tags jammed the printer today: {name: "Tim"}, {'gift': 'sled'} and {"gift": "sle. Which can response healing repair back to what the model meant?

Stuck? Ask Tinsel for a hint

The first two. Unquoted keys and single-quoted strings are both in the repair table. The third was cut off mid-string, and the docs say a reply cut mid-string cannot be reconstructed.

Christmas crackerWhy was the gift tag with no closing bracket so calm?Pull it for the punchline

It knew the router would give it closure.