In the previous lesson, you learned how to use placeholders and explanations in your prompt examples to help LLMs generate outputs in the exact format you need. This lesson builds on that foundation by focusing on how to prompt LLMs to produce outputs in specific, parsable formats such as JSON
, YAML
, and HTML
.
Why is this important? In many real-world scenarios, you need the output from an LLM to be directly usable by other systems or tools. For example, you might want to generate a JSON
object to send to an API, a YAML
file for configuration, or an HTML
snippet for a web page. If the output is not in the correct format, it can cause errors or require extra work.
By the end of this lesson, you will know how to write prompts that consistently produce well-structured, parsable outputs in the format you need.
Before diving into prompting, let’s quickly review what some popular formats look like and how they differ.
-
JSON
is a widely used data format, especially in web APIs. It uses curly braces{}
for objects and square brackets[]
for arrays. Keys and string values are always in double-quotes.Example:
-
YAML
is often used for configuration files. It is more human-readable thanJSON
and relies on indentation instead of brackets or braces.Example:
-
HTML
is used to structure content on the web. It uses tags like<div>
,<p>
, and<span>
.Example:
The key takeaway is that each format has its own rules for structure and syntax. When prompting an LLM, you must be clear about which format you want and what the output should look like.
To get a parsable format, you need to be explicit. For example, if you want YAML
, you can say:
To ensure the output is consistent and meets your needs, add constraints. For example, you might want five users, each with a name, age, and email, and you want the YAML
to be properly formatted.
Here’s how you can add those constraints:
Providing an example helps the LLM understand exactly what you want. For YAML
, you might add:
This shows the model, including the structure, indentation, and spacing you expect. This approach works for other formats, too. For instance, if you need JSON
, you would adjust the example and constraints to match JSON
syntax.
Providing an example is always a good idea—it helps the LLM understand precisely what you want, including the structure, indentation, and any subtle formatting details. However, if you don’t know the details of the format or don’t have time to craft an example, you can often rely on the LLM to fill in the gaps.
LLMs are pretty good at generating correct syntax and structure just from clear instructions for popular formats like JSON,
YAML,
HTML,
XML,
etc. In many cases, simply specifying the desired format in your prompt is enough to get a well-formed output. If you notice minor issues or want more control, you can always add an example or more constraints later.
In this lesson, you learned how to prompt LLMs to generate outputs in specific, parsable formats like JSON
, YAML
, and HTML
. You saw how to:
- Clearly specify the desired format in your prompt.
- Add constraints to control the structure and content.
- Provide examples to guide the model’s output.
Next, you will get hands-on practice writing prompts and checking outputs for different formats. This will help you build confidence and skill in getting exactly the output you need from an LLM.
