AIResearched. Verified against official documentation (2 sources), not personally tested.

Get consistent structured output from an LLM

Stop fighting the chatbot, tell it exactly what shape to return, and switch on JSON mode.

1 min read

An LLM is the AI behind a chatbot. Ask it for JSON and it often replies with a friendly greeting, wraps the answer in markdown, or leaves out a field you need. You spend more time cleaning the text than using it. Tell the model exactly what shape to return, and switch off the extra chat.

Use a system message to stop the chatter

A system message is an instruction at the start of a conversation that shapes how the model behaves. It works like a form with labelled boxes: you tell the person to fill only those boxes and nothing else.

Steps:

  1. Start the chat with this system message: Return only valid JSON. No markdown. No introduction. No text before or after the JSON.
  2. In your user message, name the fields you need. Example: give me the customer name, order number, and total as JSON.
  3. Keep unrelated questions out of this turn.

Reference: Chat completions API reference

Turn on JSON mode

JSON is plain text that looks like labelled boxes and lists. Programs can read it easily. JSON mode is a provider setting that forces the model to emit valid JSON instead of free-flowing text. It does not choose your field names; you still ask for those in the prompt.

Steps:

  1. Add this to your API request — the message your program sends to the AI service: “response_format”: { “type”: “json_object” }
  2. Keep the system message from the section above.
  3. Say which fields are required and which can be empty.

Reference: Structured outputs

Expected result

The model replies with only JSON, starting with { and ending with }. Your code can read it directly without stripping markdown or fixing missing fields. If it still adds chatter, check that you set the system message before your user message in the same request.

Sources

  1. https://developers.openai.com/api/reference/resources/chat
  2. https://developers.openai.com/api/docs/guides/structured-outputs

Stay Ahead. Work Smarter.

Get practical AI tips, automation ideas and useful technology tricks — one useful thing at a time. Follow the feed in any reader.