Guides
JSON output
Make deepseek-flash return a valid JSON object with response_format.
On this page
Set response_format to {"type": "json_object"} and the model returns its answer as a single valid JSON object in message.content.
Example
import json
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.larasrouter.com/v1",
api_key=os.environ["LARAS_API_KEY"],
)
response = client.chat.completions.create(
model="deepseek-flash",
response_format={"type": "json_object"},
messages=[
{
"role": "system",
"content": (
"Extract the product and quantity from the message. "
'Reply in JSON like {"product": "string", "quantity": 0}.'
),
},
{"role": "user", "content": "Saya mau pesan tiga kopi susu gula aren."},
],
)
data = json.loads(response.choices[0].message.content)
print(data) # {'product': 'kopi susu gula aren', 'quantity': 3}
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.larasrouter.com/v1",
apiKey: process.env.LARAS_API_KEY,
});
const response = await client.chat.completions.create({
model: "deepseek-flash",
response_format: { type: "json_object" },
messages: [
{
role: "system",
content:
'Extract the product and quantity from the message. Reply in JSON like {"product": "string", "quantity": 0}.',
},
{ role: "user", content: "Saya mau pesan tiga kopi susu gula aren." },
],
});
const data = JSON.parse(response.choices[0].message.content ?? "{}");
console.log(data); // { product: 'kopi susu gula aren', quantity: 3 }
Get reliable JSON
- Say "JSON" in the prompt and show the shape. JSON mode guarantees valid syntax, not your schema. Describe the keys and types you expect, ideally with an example object, in the system or user message.
- Leave room in
max_tokens. If the output hits the cap, the JSON is cut off and will not parse. Check thatfinish_reasonis"stop"before parsing. - Validate the result. Parse with
json.loads/JSON.parseand validate against your schema (for example with Pydantic or Zod) before you use it. - Use tools for strict schemas. If you need the model to fill a fixed set of arguments, a function in tool calling with a JSON Schema is often a better fit.