Prompt engineering: How the 'Structured Output' method forces AI to follow a template

Prompt engineering: How the 'Structured Output' method forces AI to follow a template

When you ask an AI to return a list of tasks or a product card, it might provide text in free form—leaving you to parse it with regex or hope the format doesn't break. Anthropic and OpenAI have solved this at the API level: Claude and GPT-4 models can now return responses strictly according to a JSON schema that you define in advance. This is called Structured Output, and it is not just a convenience, but a new standard for working with AI in production.

What is Structured Output and how does it differ from a regular prompt?

A regular prompt is a text instruction: "Return a list of three items in JSON format." The model tries to follow the request, but it might add commentary, forget a closing bracket, or rename a field. Structured Output works differently: you pass a JSON Schema to the API—a formal description of the response structure with field types, required keys, and constraints. The model is guaranteed to return valid JSON that conforms to the schema.

The mode is available in the Claude 3.5 Sonnet API (via the response_format parameter) and GPT-4 Turbo / GPT-4o (via the response_format parameter with the json_schema type). In both cases, you describe the structure once, and every subsequent response from the model will be valid JSON without the need for additional validation.

How to use Structured Output: four practical scenarios

Here are four use cases where structured output saves time and reduces errors:

  • Data extraction from text. You upload a candidate's resume, and the model returns an object with name, email, skills, and experience fields. No need to parse paragraphs manually.
  • Product card generation. You describe a product in text, and you get a JSON with title, description, price, category, and tags fields. It is ready for database entry immediately.
  • Task planning. You ask to break a project into stages, and the model returns an array of objects with task, deadline, assignee, and priority fields. You can import it directly into a task manager.
  • Classification and labeling. You provide a review text and receive an object with sentiment (positive/negative/neutral), topics (an array of tags), and urgency (low/medium/high) fields. It is ready for automated processing.

Example: how to set up Structured Output in the API

Suppose you want to extract the sender's name, the subject, and a list of action items from an email. Here is what the JSON Schema for this scenario looks like:

Schema:

  • An email_data object with three fields: sender (string), subject (string), and action_items (array of strings).
  • All fields are required.

You pass this schema in the response_format parameter along with the email text in the prompt. The model analyzes the email and returns valid JSON—no extra words, no explanations, just the data. If there are no clear action items in the email, the action_items field will be an empty array, but the structure will remain the same.

Three tips for creating a more effective schema

Here are three techniques that help achieve more accurate results:

  1. Add descriptions to fields. JSON Schema supports the description key—use it to explain to the model exactly what should go into a field. For example, for the priority field, you could write: "Assess the urgency of the task: low, medium, or high."
  2. Restrict possible values using enum. If a field can only accept a fixed set of values (e.g., order status: pending, shipped, delivered), list them in an enum. The model will not be able to return anything other than these options.
  3. Use nested objects for complex data. If you need to extract information about multiple entities (e.g., a list of meeting participants with each person's name, role, and email), describe an array of objects with name, role, and email fields. The model will return a structured list.

Where to try Structured Output right now

The structured output mode is available in the Claude and GPT APIs—but if you want to test it without writing code, try it in the GEMERA AI chat. Claude 3.5 Sonnet and GPT-4o models support JSON schemas: describe the desired response structure in your prompt, and the model will try to follow it. For production integration, use the API with the response_format parameter to ensure you get guaranteed valid JSON with every request.