SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
1.1 KB · 20 lines typescript
Raw Blame History
1// STATUS: LIVE_VERIFIED — executed 2026-09-18 (exit 0 in 0.5s)2// Structured Outputs on Chat Completions with the Node SDK (response_format json_schema, strict).3import OpenAI from "openai";4import fs from "node:fs";56const client = new OpenAI();7const completion = await client.chat.completions.create({8  model: "gpt-4.1-nano",9  messages: [{ role: "user", content: "Reply with OK." }],10  max_completion_tokens: 32,11  response_format: {12    type: "json_schema",13    json_schema: { name: "ok_reply", strict: true, schema: { type: "object", properties: { answer: { type: "string" } }, required: ["answer"], additionalProperties: false } },14  },15});16fs.appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString().replace(/\.\d+Z$/, "Z"), provider: "openai", method: "POST", path: "/v1/chat/completions", status: 200, est_cost_usd: 0.000006, note: "example chat/structured_output.ts" }) + "\n");17const parsed = JSON.parse(completion.choices[0].message.content ?? "{}");18console.log(parsed);19if (typeof parsed.answer !== "string") throw new Error("schema not honoured");20