🎓 A1Learner
🔍
0 of 50 lessons completed

Prompt Engineering: Ask for JSON, Get JSON

Structured output is an answer in a fixed shape. JSON is the shape, and a prompt that asks for JSON gets an answer a program can read.

The same question, plain words

The prompt asks for the order status in plain words.

Where is order 1042? Answer in plain words.

The same question, as JSON

The prompt names the JSON shape it wants.

Where is order 1042? Reply with a JSON object with the fields "order_number", "customer", "product" and "status".

JSON for a second order

The same shape works for a different order.

What is the status of order 1044? Reply with a JSON object with the fields "order_number", "customer", "product" and "status".

Example

What is the status of order 1043? Reply with a JSON object with the fields "order_number", "customer", "product" and "status".

Key points

  • JSON is a text shape with named fields and values in curly brackets.
  • A prompt that names the shape gets the same shape back every time.
  • A plain answer is hard for a program to read. A JSON answer is easy for a program to read.
  • The prompt must name every field it wants. The model fills each field from the facts.

This example runs the JSON prompt for order 1043 against the local model and shows the reply it gave.

Note

Ask for JSON when a program will read the answer. Ask for plain words when a person will read it.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Why ask the model for JSON?