aillmjsonragagents

JSON for AI & LLM Engineering: Every Response Shape You'll Parse

·12 min read·AI & JSON

Almost every AI feature you build is, underneath, a JSON parsing problem. You send JSON to a model and you get JSON back — chat completions, tool calls, embeddings, retrieval results, agent traces. Knowing the exact shape of each payload is the difference between code that handles real responses and code that breaks the first time finish_reason is "length" or content is null.

This is a field guide to the JSON formats you'll meet across the LLM stack. Every section links to a copy-ready example with documented fields, real-world variants, and a one-click "open in tool" button so you can format, validate, or generate TypeScript types from it instantly.

> Browse the whole set on the JSON Examples Library — the AI & LLM category sits right at the top.

Chat completions: the core request/response

The workhorse of any LLM app is the chat completion. You send an array of role-tagged messages and get back a message plus token usage.

The two gotchas that bite everyone: content can be null when the model returns a tool call instead of text, and a finish_reason (or stop_reason) of length/max_tokens means the answer was truncated — always handle it.

Tool calling, function calling & MCP

When a model needs to *do* something — look up an order, query a database — it emits a tool call: a function name plus JSON arguments. You run the function and feed the result back.

Structured output: making the model return your shape

Free-text parsing is fragile. Structured output constrains the decoder to a JSON Schema so the response matches your shape exactly — no regex, no defensive parsing.

Embeddings, vector search & RAG

Retrieval-augmented generation is mostly moving vectors and chunks around. The shapes:

A reliable RAG system abstains when retrieval finds nothing relevant instead of hallucinating — that grounded-refusal payload is what separates trustworthy RAG from confident nonsense. For a stage-by-stage walkthrough of these shapes, see RAG JSON Formats Explained.

Agents: tracing what happened

Agents loop: think, call a tool, observe, repeat. Capturing each step as JSON makes runs debuggable and reproducible.

Training, evaluation & safety

Multimodal: audio & images

Tips for working with AI JSON

  1. Never trust the shape blindly. Even with structured output, validate against a schema. Models can omit fields or, in failure modes, return prose. Run model output through the JSON Validator or JSON Schema Validator.
  2. Parse stringified arguments. Tool-call arguments come as a JSON string — JSON.parse() them, then validate.
  3. Handle truncation. A length/max_tokens stop reason means incomplete output; raise max_tokens or continue the response.
  4. Generate types from real payloads. Paste any example into JSON to TypeScript to get interfaces you can actually rely on.
  5. Explore deeply nested responses. LLM responses nest fast — drop one into the JSON Tree Explorer to navigate choices[0].message.tool_calls[0] without losing your place.

Frequently asked questions

A chat completion returns an object with id, model, a choices array (each with a message and finish_reason), and a usage object of token counts. See the OpenAI Chat Completion example for every field documented.

Claude's Messages API returns content as an array of blocks (text, tool_use, etc.) rather than a single string, uses stop_reason instead of finish_reason, and splits usage into input_tokens/output_tokens. Compare the Claude Messages example side by side.

You register tools with a name, description, and JSON Schema parameters; the model responds with a tool call containing the function name and a JSON-encoded arguments string. The Tool / Function Call example shows the definition, the call, and the result you send back.

The JSON Examples Library has a dedicated AI & LLM category with 19+ examples covering OpenAI, Claude, MCP, embeddings, RAG, agents, fine-tuning, evals, and more — each with documented fields and a button to open it in a tool.

Try AI & LLM JSON Examples

Copy-ready JSON for OpenAI, Claude, RAG, agents, MCP and more — every field documented.