DevUp Docs
Back to Dashboard

Chat Completions

Structured Outputs

Get model responses in JSON format using response_format.

In addition to text, the DEVUP AI API can return responses in JSON format. This is supported in both our inference API and our OpenAI-compatible API, across many of our models.

ModeHow to useWhen to use
json_object{"type": "json_object"}Any valid JSON object, schema-free
json_schema{"type": "json_schema", "json_schema": {...}}Enforces a strict output schema

json_object mode

The simplest way to get JSON output. The model returns a valid JSON object but you don't control the exact shape.

import openai
import json

client = openai.OpenAI(
    base_url="https://api.devupai.com/v1",
    api_key="$DEVUP_API_KEY",
)

messages = [
    {
        "role": "user",
        "content": "Provide a JSON list of 3 famous scientific breakthroughs in the past century, all of the countries which contributed, and in what year."
    }
]

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3",
    messages=messages,
    response_format={"type": "json_object"},
)

print(response.choices[0].message.content)

json_schema mode

Enforces a strict output schema using JSON Schema. The model is constrained to produce only values that match your schema — useful when downstream code depends on a fixed structure.

import openai
import json

client = openai.OpenAI(
    base_url="https://api.devupai.com/v1",
    api_key="$DEVUP_API_KEY",
)

response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3",
    messages=[
        {
            "role": "user",
            "content": "Extract the name, country, and year from: 'Alexander Fleming discovered Penicillin in the UK in 1928.'"
        }
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "breakthrough",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "country": {"type": "string"},
                    "year": {"type": "integer"}
                },
                "required": ["name", "country", "year"],
                "additionalProperties": False
            }
        }
    }
)

print(json.loads(response.choices[0].message.content))

Output:

json
{"name": "Penicillin", "country": "UK", "year": 1928}

Tips

  • Always prompt the model to produce JSON: Ensure your system or user prompt explicitly instructs the model to output in JSON format.
  • Prefer json_schema for production: Use json_schema when you need strict validation and a guaranteed response shape for downstream systems.
  • Watch for truncation: Make sure your max_tokens limit is large enough to accommodate the complete JSON structure, otherwise it may be cut off mid-generation.

Caveats

If the model refuses to answer your prompt, it may still attempt to fulfill the request by hallucinating JSON keys that match your schema. For instance, if you ask "What's the weather in San Francisco?" with a schema that forces a strict {"temperature": "integer"} output, the model might invent a temperature to conform to the strict JSON rules.

Best practices:

  • Use JSON mode for structured data extraction, not for open-ended queries or knowledge retrieval.
  • Keep prompts specific and provide a clear expected structure if using json_object.
  • Validate model output with your own schema checker before using it in your application.
  • Use lower temperatures (e.g. 0.1 - 0.3) to increase adherence to deterministic JSON structures.