Structured Response
This function allows you to request GPT to answer in a specific format defined with zod
const response = await useGPT('How high is Mt. Everest?')
.toStructured(
z.object({
meters: z.number()
})
)
// Output:
{ meters: 8849 }
.toStructured(zodType)
Sends the message but requests GPT to return the provided type. Use ZOD to define the return type.
Useful Notes
Here is some information I stumbled upon while developing this:
- OpenAI's API is very selective about zod's types. It really only supports basic structures but nothing like e.g.
z.array(...).length()
or.optional()
. - For nullable types there is a workaround with union:
z.union([z.<your-type>(), z.null()])
Table of Contents