Output Terstruktur

Paksa JSON yang valid. Mode json_object dan json_schema ketat.

Mode 1: json_object

from openai import OpenAI
client = OpenAI(base_url="https://meshtok.com/v1", api_key="sk-...")
resp = client.chat.completions.create(
    model="deepseek/deepseek-v4-flash",
    messages=[{"role": "user", "content": "List 3 fruits as JSON."}],
    response_format={"type": "json_object"},
)
import json
data = json.loads(resp.choices[0].message.content)

Mode 2: json_schema (strict)

resp = client.chat.completions.create(
    model="bigmodel/glm-5.2",
    messages=[{"role": "user", "content": "Extract the company."}],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "company", "strict": True,
            "schema": {"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"], "additionalProperties": False}
        }
    },
)
→ Pemanggilan Alat & Fungsi → Input Visi & Gambar Obrolan