Structured outputs and JSON

Get JSON back from the model using response_format with json_object or json_schema on the OpenAI chat shape.

Structured outputs make the model return machine-readable JSON instead of free-form prose, so you can parse the result directly. FluxRouter accepts the standard OpenAI response_format field against https://api.fluxrouter.ai/v1/chat/completions, so you do this exactly as you would calling OpenAI. The only Flux-specific part is the base URL and the flux-auto model.

Two modes

The response_format field has two structured modes:

  • JSON mode ({"type": "json_object"}): the model returns syntactically valid JSON. You still describe the shape you want in the prompt.
  • JSON schema ({"type": "json_schema", ...}): the model returns JSON that conforms to a schema you provide, so the keys and types are constrained, not just "valid JSON."

JSON mode

Ask for JSON and set response_format to json_object. Always state in the prompt what fields you expect.

python
import json
from openai import OpenAI

client = OpenAI(
    api_key="sk-...",                          # your Flux key
    base_url="https://api.fluxrouter.ai/v1",   # the one line you change
)

response = client.chat.completions.create(
    model="flux-auto",
    response_format={"type": "json_object"},
    messages=[
        {
            "role": "user",
            "content": "Extract the name and age as JSON with keys 'name' and 'age': Ada is 36.",
        }
    ],
)

data = json.loads(response.choices[0].message.content)
print(data)  # {'name': 'Ada', 'age': 36}

JSON schema

For a guaranteed shape, pass a json_schema. The model is constrained to the schema's properties and types.

python
from openai import OpenAI

client = OpenAI(
    api_key="sk-...",
    base_url="https://api.fluxrouter.ai/v1",
)

response = client.chat.completions.create(
    model="flux-auto",
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "person",
            "schema": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "age": {"type": "integer"},
                },
                "required": ["name", "age"],
                "additionalProperties": False,
            },
        },
    },
    messages=[{"role": "user", "content": "Ada is 36 years old."}],
)

print(response.choices[0].message.content)  # {"name":"Ada","age":36}

curl (JSON mode)

bash
curl https://api.fluxrouter.ai/v1/chat/completions \
  -H "Authorization: Bearer $FLUX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "flux-auto",
    "response_format": { "type": "json_object" },
    "messages": [
      { "role": "user", "content": "Return JSON with keys name and age: Ada is 36." }
    ]
  }'

Make sure your model supports it

Structured output support depends on the model that serves the request. JSON mode is widely supported; strict json_schema enforcement is supported by fewer models. flux-auto routes to a sensible model, but whether a given mode is honored is a property of the underlying model, not of FluxRouter.

If your application depends on a specific mode, pin a model you have verified supports it. Pass a flux-pinned-* id (for example flux-pinned-gpt-5 or flux-pinned-claude-sonnet) instead of flux-auto. See Models for the full list.

Whatever model serves the request, always validate the JSON you get back (parse it, check required keys) rather than trusting the shape blindly.

Anthropic shape

The Anthropic-compatible base at https://api.fluxrouter.ai/anthropic does not use response_format. To get structured output there, use Anthropic's standard patterns (a tool whose input schema is your target shape, or prompting for JSON), exactly as you would calling Anthropic directly. See Tools and function calling.