Views
No views yet
.parse() schemanpm install @krishthesmart/contract-kit zod1import { z } from "zod";
2import { generateContract, openAIResponsesAdapter } from "@krishthesmart/contract-kit";
3import OpenAI from "openai";
4
5const client = new OpenAI();
6
7const UserProfile = z.object({
8 name: z.string(),
9 role: z.string(),
10 risk: z.enum(["low", "medium", "high"])
11});
12
13const result = await generateContract({
14 model: openAIResponsesAdapter(client, {
15 model: "gpt-4.1-mini",
16 temperature: 0
17 }),
18 schema: UserProfile,
19 prompt: "Extract the user's profile from this support ticket: ...",
20 retries: 2,
21 onEvent(event) {
22 console.log(event.type);
23 }
24});
25
26console.log(result.data);result.data is guaranteed to be the schema output type if the call succeeds.
If every attempt fails, Contract Kit throws a typed error containing replay, which can be saved as a test fixture.1const model = {
2 async generate(prompt: string) {
3 return callYourModel(prompt);
4 }
5};