This older article and its examples have not yet been reviewed against current APIs. Check the latest documentation before using the code or treating any example as a measured result.
A call summary needs fields an application can use: an intent, a customer name, and an urgency level. A paragraph may be easy for a person to read, but the application still has to work out where those values are.
Define and validate the schema
Define a schema for the response and use it to keep the JSON schema, runtime validator, and TypeScript types aligned. That gives the model and the rest of the application the same contract.
Validate the result before using it. A bounded retry may help if validation fails, but measure both its success rate and its cost for your workload.
Avoid sending sensitive validation details back to the model unnecessarily.
Constrain choices and review errors
Use enums where the application needs a fixed set of choices. If an unexpected value reaches a downstream branch, the application needs to reject it or handle it explicitly.
Record schema errors so you can see why responses were rejected. Repeated failures can reveal problems worth investigating in the prompt or schema.
import { z } from "zod";
export const CallSummary = z.object({
intent: z.enum(["booking", "reschedule", "pricing", "support", "other"]),
urgency: z.enum(["low", "medium", "high"]),
customer: z.object({
name: z.string().min(1),
phone: z.string().optional(),
}),
requestedSlots: z.array(z.string().datetime()).max(5),
needsHuman: z.boolean(),
});
export type CallSummary = z.infer<typeof CallSummary>;
export async function summariseCall(transcript: string) {
for (let attempt = 0; attempt < 3; attempt++) {
const raw = await model.json(transcript, CallSummary);
const parsed = CallSummary.safeParse(raw);
if (parsed.success) return parsed.data;
transcript += `\n[validation error: ${parsed.error.issues[0].message}]`;
}
throw new Error("LLM output failed validation after 3 attempts");
}Record schema errors so you can see why responses were rejected. Repeated failures can reveal problems worth investigating in the prompt or schema.