Assign null when information not available in structured outputs

Hi,

I’m currently experimenting with structured outputs using NodeJS SDK. Is there any way to assign a null value or to catch whenever information is not available? (One of the information not the whole thing)

For example, I have this Zod schema:

const InstructionFormat = z.object({
    time: z.string(),
    task: z.string(),
    assignee: z.string(),
});

Let’s say the time is not available, I want something like
{ time: null, task: ‘finish math homework’, assignee: ‘Alice’ }

Adding .optional () didn’t work. My current workaround is by adjusting the prompt:
{ role: "system", content: "Extract the todo information from the prompt. Assign '-' if something not specified" },

So I can get something like this
{ time: '-', task: 'finish math homework', assignee: 'Alice' }

I guess it’s more reliable to have a null value than a certain string.

3 Likes

Not that I’m aware of at the moment.

Clever.

1 Like

@tukemon according to the docs it’s possible to emulate “optional” values.

So using your schema above, in JSON format it could be:

"time": {
    "type": ["string", "null"],
}

In your prompt you have to specify that if the timestamp is not available, return an empty string for the 'time' field. Since all the fields are actually required and will be returned, time will also be returned, but if there is no data it will be an empty string, i.e. '', so you need to just add some validation for that.

1 Like