Introduction

Let's delve into "Structured Data Mastery," in our course on Format Control in Prompt Engineering. In the world of data exchange and system integration, it's crucial to understand how to instruct Large Language Models (LLMs) to generate structured data formats. This lesson guides you through the nuances of prompting LLMs to return data in well-defined, machine-readable formats. Whether you are working with web APIs, configuring software, or merely organizing data, mastering these skills will significantly enhance your data manipulation and automation capabilities.

Core Principles of Getting the Right Format

Before we dive into specifics, let's grasp the core principles that underpin the generation of structured formats:

  • Precision in Instructions: Clearly articulate the specific format you expect as an output, be it JSON, YAML, or any other structured format.
  • Contextual Clarity: Provide enough contextual information to align with the expected structured data format.

Let's explore these principles through the outcomes of some examples.

Getting JSON Format

Suppose you want to retrieve user data in a JSON format. Here’s how you might craft your prompt:

__ASK__
Generate sample user information in JSON format.

__CONSTRAINTS__
- The information should include name, age, and email of the user.

__EXAMPLE__
{
  "name": "Jane Doe",
  "age": 28,
  "email": "jane.doe@example.com"
}

This prompt instructs the LLM clearly to format the output in JSON. Let's observe its output:

{
  "name": "Jane Doe",
  "age": 28,
  "email": "jane.doe@example.com"
}

Note that it is the same as an example. A simple workaround would be just to include "The information should be different from an example below" in the constraints.

Getting YAML Format

Now, if you need the same information in YAML format, your prompt could resemble this:

__ASK__
Generate sample user information in YAML.

__CONSTRAINTS__
- The information should include name, age, and email of the user.
- Include examples for 5 users different from the examples provided by me.
- Pay careful attention to spacing consistency shown in the __EXAMPLE__ to ensure proper YAML syntax.

__EXAMPLE__
- name: Jane Doe
  age: 28
  email: jane.doe@example.com

- name: John Smith
  age: 32
  email: john.smith@example.com

Let's unpack what we've done here. First of all, we've included an example with the specific spacing guidelines we want. While we could try to explain these requirements in the constraints section, this is a case where one example is worth a million words. We've also included how many samples we are looking for and instructed the LLM to pay attention to the spacing in the __EXAMPLE__ section since YAML tends to be very sensitive to this.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal