Request JSON That Fits a Schema

In the last lesson, your booking prompt asked for each record as a JSON object, and most replies parsed. But the reply to Pepper and Salt’s note sometimes came back with a run of stray words before the object or was cut off at the output limit, and your guard had to hold it back. The prompt can only describe the record in words.

Both providers can do more than that. You can send the record’s shape with the request, and the provider constrains the reply to fit it. In this lesson, you’ll write the appointment record as a schema that both providers accept and send it with one request you build by hand, so you see exactly what goes out and what comes back. Start by writing the record’s shape down as data.

Describe the Record as a JSON Schema

Your prompt lists the record’s keys and their values in plain text, and the model can ignore that text. A provider can enforce the record’s shape only when you write it as data. JSON Schema is a standard for that: a JSON object that describes other JSON, saying which keys an object has, what type each value is, and which values are allowed.

The providers’ structured output features take a JSON Schema with the request and constrain what the model generates to match it. Create record-schema.js in your project, next to booking-prompt.js:

Language: JavaScript Filename: record-schema.js
const textOrNull = { type: ["string", "null"] };

export const RECORD_SCHEMA = {
  type: "object",
  properties: {
    kind: { type: "string", enum: ["booking", "not_a_booking"] },
    species: textOrNull,
    petName: textOrNull,
    problem: textOrNull,
    urgency: {
      type: ["string", "null"],
      enum: ["routine", "soon", "urgent", null],
    },
    preferredDate: textOrNull,
    preferredTime: { type: "string" },
  },
  required: [
    "kind", "species", "petName", "problem", "urgency",
    "preferredDate",
  ],
  additionalProperties: false,
};

The record is an object, and properties lists its seven keys. kind is a string, and its enum allows only "booking" or "not_a_booking", so the reply’s kind should be one of those two.

Most other keys use textOrNull, whose type list allows a string or null, because your prompt’s rules write null for anything the note doesn’t give. urgency takes the clinic’s three levels or null, which is what a note that isn’t a booking gets. additionalProperties: false rules out any key not listed.

That leaves required, the keys a record must have. Plenty of owners give a day but no time, and in JSON Schema, a key left out of required may be missing altogether. So preferredTime is out of required and has a plain string type, which means a record either has a time or has no preferredTime key at all.

Send the Schema With One Request

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.