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.6 KB · 19 lines typescript
Raw Blame History
1// STATUS: LIVE_VERIFIED — executed 2026-09-18 (exit 0 in 1.2s)2// Structured output: responseMimeType application/json + responseJsonSchema (JSON Schema) and text/x.enum mode.3// Run: node --env-file=.env --experimental-strip-types examples/gemini/structured-output/json_schema.ts   (@google/genai 2.23 reads GEMINI_API_KEY)4import { GoogleGenAI } from "@google/genai";5import { appendFileSync } from "node:fs";67const ai = new GoogleGenAI({});8const MODEL = "gemini-3.5-flash-lite";9const log = (method: string, path: string, status: number, cost: number, note: string) =>10  appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString().replace(/\.\d+Z$/, "Z"), provider: "gemini", method, path, status, est_cost_usd: cost, note }) + "\n");11const r = await ai.models.generateContent({ model: MODEL, contents: "Reply with OK.", config: { maxOutputTokens: 64, responseMimeType: "application/json",12  responseJsonSchema: { type: "object", properties: { ok: { type: "boolean" }, word: { type: "string" } }, required: ["ok", "word"] } } });13log("POST", "/v1beta/models/{model}:generateContent", 200, 0.00003, "example structured-output/json_schema.ts");14console.log(r.text);15if (JSON.parse(r.text ?? "{}").ok !== true) throw new Error("schema not honoured");16const e = await ai.models.generateContent({ model: MODEL, contents: "Reply with OK.", config: { maxOutputTokens: 8, responseMimeType: "text/x.enum", responseSchema: { type: "STRING", enum: ["OK", "NOT_OK"] } } });17log("POST", "/v1beta/models/{model}:generateContent", 200, 0.00001, "example structured-output/json_schema.ts (enum)");18console.log("enum:", e.text);19