Structured outputs & JSON mode
Force a model to return valid JSON. MeshTok supports two modes from the OpenAI spec: json_object (loose JSON) and json_schema (strict schema enforcement on supporting models). All 86 JSON-capable models support at least json_object.
Mode 1: json_object (works everywhere)
Sets response_format to JSON. You must include the word "JSON" in the prompt or the request will be rejected.
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: {name, color}."}], response_format={"type": "json_object"}, ) import json data = json.loads(resp.choices[0].message.content)
Mode 2: json_schema (strict)
Constrains the model to a specific JSON schema — every field, every type, every enum enforced. Supported by GPT-5.x, Claude 5, GLM 5.2, Gemini 2.5, and Qwen 3.
resp = client.chat.completions.create(
model="bigmodel/glm-5.2",
messages=[{"role": "user", "content": "Extract the company from: Apple announced..."}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "company",
"strict": True,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"ticker": {"type": "string"},
"founded": {"type": "integer"}
},
"required": ["name", "ticker", "founded"],
"additionalProperties": False,
}
}
},
) Tips for reliable JSON
- Use
json_schemaoverjson_objectwhenever the model supports it — it eliminates malformed responses. - Lower temperature (0.0–0.3) for extraction tasks; the schema does the rest.
- For streaming, parse each
deltaand reconstruct — schema-constrained streams emit field-by-field. - If a model rejects
strict: true, fall back tojson_objectplus Pydantic validation.