Role-based Prompting
Use message arrays to create rich, context-aware interactions with multiple roles. This method provides more control over the conversation flow compared to single prompts.
Basic Role-based Generation
The example demonstrates role-based messaging with Zod schema validation. Messages define the user query and system instructions, while the Zod schema enforces response structure (company details as strings and numbers). The result is a type-safe JSON object.
import { z } from "zod";
const {
data: { payload },
} = await unbody.generate
.json(
[
{
role: "user",
content: "What is the structure of a business entity?",
},
{
role: "system",
content: "Explain in detail with examples."
},
],
{
schema: z.object({
companyName: z.string(),
employees: z.number(),
headquarters: z.string(),
}),
}
);
Learn more about advanced role-based prompting in our JSON Generation API Guide.