Recipe

JSON Mode Output Design

Structured JSON output is the backbone of every reliable LLM integration. Meridian guarantees parseable JSON when you opt in viaresponse_formatand pair it with a tight system prompt. This recipe walks through schema design, prompt shape, and validation.

1. Lock the response_format

Pass { type: "json_object" } on every request. Meridian rejects responses that fail JSON.parseserver-side and retries once before surfacing an error to your client, so you never have to ship prose-stripping regex.

2. Anchor the schema in the system prompt

The most common JSON-mode failure is a model hallucinating keys. Spell every required field in the system prompt and give one short example. Keep the schema flat where possible — nested objects invite drift and bloat tokens.

3. Validate before you trust

Always wrap the parse in a validator (zod, valibot, ajv). JSON mode guarantees syntactic JSON, not semantic shape. Reject early, log the raw string, and you will catch prompt regressions before customers do.

Full example

// Request JSON mode output from Meridian
const response = await fetch("https://api.meridian.ai/v1/chat/completions", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.MERIDIAN_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "meridian-pro",
    response_format: { type: "json_object" },
    messages: [
      {
        role: "system",
        content: "You output strict JSON. No prose. No markdown fences.",
      },
      {
        role: "user",
        content: "Extract: name, email, intent from: 'Hi I am Ada (ada@x.io), need a refund.'",
      },
    ],
  }),
});

const data = await response.json();
const parsed = JSON.parse(data.choices[0].message.content);
// parsed => { name: "Ada", email: "ada@x.io", intent: "refund" }