1# Structured model outputs1# Structured model outputs
2 2
3export const snippetRefusalsChatCompletionsApi = {3import {
4 python: `4 snippetRefusalsChatCompletionsApi,
5class Step(BaseModel):5 snippetRefusalsResponsesApi,
6 explanation: str6} from "./inline-examples";
7 output: str
8
9class MathReasoning(BaseModel):
10steps: list[Step]
11final_answer: str
12
13completion = client.chat.completions.parse(
14model="gpt-4o-2024-08-06",
15messages=[
16{"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."},
17{"role": "user", "content": "how can I solve 8x + 7 = -23"},
18],
19response_format=MathReasoning,
20)
21
22math_reasoning = completion.choices[0].message
23
24# If the model refuses to respond, you will get a refusal message
25
26if math_reasoning.refusal:
27print(math_reasoning.refusal)
28else:
29print(math_reasoning.parsed)
30`.trim(),
31 "javascript": `
32const Step = z.object({
33explanation: z.string(),
34output: z.string(),
35});
36
37const MathReasoning = z.object({
38steps: z.array(Step),
39final_answer: z.string(),
40});
41
42const completion = await openai.chat.completions.parse({
43model: "gpt-4o-2024-08-06",
44messages: [
45{ role: "system", content: "You are a helpful math tutor. Guide the user through the solution step by step." },
46{ role: "user", content: "how can I solve 8x + 7 = -23" },
47],
48response_format: zodResponseFormat(MathReasoning, "math_reasoning"),
49});
50
51const math_reasoning = completion.choices[0].message
52
53// If the model refuses to respond, you will get a refusal message
54if (math_reasoning.refusal) {
55console.log(math_reasoning.refusal);
56} else {
57console.log(math_reasoning.parsed);
58}
59`.trim(),
60};
61export const snippetRefusalsResponsesApi = {
62 python: `
63class Step(BaseModel):
64explanation: str
65output: str
66
67class MathReasoning(BaseModel):
68steps: list[Step]
69final_answer: str
70
71response = client.responses.parse(
72model="gpt-4o-2024-08-06",
73input=[
74{"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."},
75{"role": "user", "content": "how can I solve 8x + 7 = -23"},
76],
77text_format=MathReasoning,
78)
79
80for output in response.output:
81if output.type != "message":
82raise Exception("Unexpected non message")
83
84 for item in output.content:
85 if item.type == "refusal":
86 # If the model refuses to respond, you will get a refusal message
87 print(item.refusal)
88 continue
89
90 if not item.parsed:
91 raise Exception("Could not parse response")
92
93 print(item.parsed)
94
95`.trim(),
96 "javascript": `
97const Step = z.object({
98explanation: z.string(),
99output: z.string(),
100});
101
102const MathReasoning = z.object({
103steps: z.array(Step),
104final_answer: z.string(),
105});
106
107const response = await openai.responses.parse({
108model: "gpt-4o-2024-08-06",
109input: [
110{ role: "system", content: "You are a helpful math tutor. Guide the user through the solution step by step." },
111{ role: "user", content: "how can I solve 8x + 7 = -23" }
112],
113text: {
114format: zodTextFormat(MathReasoning, "math_response"),
115},
116});
117
118for (const output of response.output) {
119if (output.type != "message") {
120throw new Error("Unexpected non message");
121}
122
123 for (const item of output.content) {
124 if (item.type == "refusal") {
125 // If the model refuses to respond, you will get a refusal message
126 console.log(item.refusal);
127 continue;
128 }
129
130 if (!item.parsed) {
131 throw new Error("Could not parse response");
132 }
133
134 console.log(item.parsed);
135 }
136
137}
138`.trim(),
139};
140 7
141export const snippetRefusalApiResponseChatCompletionsApi = {8export const snippetRefusalApiResponseChatCompletionsApi = {
142 json: `9 json: `
326 193
327When the `refusal` property appears in your output object, you might present the refusal in your UI, or include conditional logic in code that consumes the response to handle the case of a refused request.194When the `refusal` property appears in your output object, you might present the refusal in your UI, or include conditional logic in code that consumes the response to handle the case of a refused request.
328 195
329The API response from a refusal will look something like this:196
197
198
199 The API response from a refusal will look something like this:
330 200
331 201
332 202