Structured output

Ask a model for JSON that matches a schema. The answer is a JSON string on message.content.

Send response_format on a chat completion. The model answers with a JSON string on choices[0].message.content. Parse that string. Keep the root of the schema an object. A non-object root is wrapped under a result key.

A model supports this when its page lists Structured output under Details. That row is present when the catalog hop advertises response_format or structured_outputs. Models without that row are not asked to follow a schema.

The shape follows the OpenAI chat structured outputs guide. AnyRouter forwards the object. It does not rewrite the schema.

{
  "type": "json_schema",
  "json_schema": {
    "name": "founder",
    "strict": false,
    "schema": {
      "type": "object",
      "properties": {
        "name": { "type": "string" }
      },
      "required": ["name"],
      "additionalProperties": false
    }
  }
}

strict is false. That is the request Interfaze documents. A strict: true body on that hop came back as ordinary prose.

OpenAI SDK

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://anyrouter.dev/api/v1",
    api_key=os.environ["ANYROUTER_API_KEY"],
)

completion = client.chat.completions.create(
    model="interfaze/interfaze-1.1-lite",
    messages=[{"role": "user", "content": "Who is the founder of Interfaze?"}],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "founder",
            "strict": False,
            "schema": {
                "type": "object",
                "properties": {"name": {"type": "string"}},
                "required": ["name"],
                "additionalProperties": False,
            },
        },
    },
)
print(completion.choices[0].message.content)

Swap model for any listing whose Details card links here. The Try dialog on that page has a visual editor and a code editor for the same schema, and it prints the answer as a JSON code block.

AnyRouter routes requests to third-party models. Models and outputs are provided as is, with no warranty; you use them at your own risk. Disclaimer