SingleAPI supports JSON response formats on models configured for them. The catalogue response includes each model's JSON capability flags; the dashboard does not currently display them. An unsupported model and format combination returns an error.
With client and model_id from the quickstart, request a JSON object:
response = client.chat.completions.create(
model=model_id,
messages=[{"role": "user", "content": "Return a JSON object with a greeting field."}],
response_format={"type": "json_object"},
)
print(response.choices[0].message.content)JSON object mode asks the model for JSON but does not enforce a particular shape. To request a shape, use json_schema with an object-root schema:
response = client.chat.completions.create(
model=model_id,
messages=[{"role": "user", "content": "Write a short greeting."}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "greeting",
"schema": {
"type": "object",
"properties": {"greeting": {"type": "string"}},
"required": ["greeting"],
"additionalProperties": False,
},
"strict": True,
},
},
)
print(response.choices[0].message.content)Provider support varies, so validate the response in your application. The API reference lists the accepted formats, schema rules and errors.