JSON Mode

The response_format parameter is under development. To get JSON responses, include instructions in the system prompt: "Respond only in valid JSON format."

JSON mode guarantees that the model will return a response as a valid JSON object. This is ideal for application integration, data parsing, and automation.

Enabling JSON Mode

Add the response_format parameter to your request:

cURL
curl https://api.vmira.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-mira-YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mira",
    "response_format": { "type": "json_object" },
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant that always responds in JSON format."
      },
      {
        "role": "user",
        "content": "Name 3 European capital cities with their population"
      }
    ]
  }'
When using JSON mode, you must instruct the model in the system message to respond in JSON. Otherwise, the model may produce an endless stream of tokens.

Example Response

JSON
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "{\"capitals\": [{\"city\": \"Paris\", \"country\": \"France\", \"population\": 2161000}, {\"city\": \"Berlin\", \"country\": \"Germany\", \"population\": 3645000}, {\"city\": \"Madrid\", \"country\": \"Spain\", \"population\": 3223000}]}"
      },
      "finish_reason": "stop"
    }
  ]
}

JSON Schema (Structured Output)

For precise control over response structure, use JSON Schema in the response_format parameter. The model is guaranteed to return an object matching the specified schema.

Python
import requests

response = requests.post(
    "https://api.vmira.ai/v1/chat/completions",
    headers={"Authorization": "Bearer sk-mira-YOUR_KEY"},
    json={
        "model": "mira",
        "response_format": {
            "type": "json_schema",
            "json_schema": {
                "name": "city_info",
                "strict": True,
                "schema": {
                    "type": "object",
                    "properties": {
                        "city": {"type": "string"},
                        "country": {"type": "string"},
                        "population": {"type": "integer"},
                        "landmarks": {
                            "type": "array",
                            "items": {"type": "string"}
                        }
                    },
                    "required": ["city", "country", "population", "landmarks"],
                    "additionalProperties": False
                }
            }
        },
        "messages": [
            {"role": "system", "content": "Return city information in the specified JSON format."},
            {"role": "user", "content": "Tell me about Tokyo"}
        ]
    }
)

import json
data = json.loads(response.json()["choices"][0]["message"]["content"])
print(data)
# {"city": "Tokyo", "country": "Japan", "population": 13960000, "landmarks": ["Tokyo Tower", "Senso-ji", "Shibuya Crossing"]}

Common Patterns

Data Extraction

Extract structured data from unstructured text:

Prompt
Extract product info from this text and return JSON with fields name, price, category:

"For sale: iPhone 15 Pro, 128GB, price $999. Category: electronics."
Response
{
  "name": "iPhone 15 Pro 128GB",
  "price": 999,
  "category": "electronics"
}

Classification

Classify text with model confidence:

JSON Schema
{
  "type": "object",
  "properties": {
    "sentiment": { "type": "string", "enum": ["positive", "negative", "neutral"] },
    "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
    "topics": { "type": "array", "items": { "type": "string" } }
  },
  "required": ["sentiment", "confidence", "topics"]
}

Structured Data Generation

Generate test data, configurations, or API responses in a given format.

Best Practices

  • System messageAlways state in the system prompt that the response should be JSON. Describe the expected structure.
  • Use strict: trueWhen using json_schema, set strict: true to guarantee exact schema compliance.
  • Handle parse errorsEven in JSON mode, wrap JSON.parse() in try/catch for unexpected situations.
  • Examples in promptFor complex schemas, include an example of the expected JSON in the prompt to improve accuracy.

Next Steps