How do I get structured output in the typescript SDK? Can anyone add a code snippet?
1 Like
Seems this works for now…
import { InferenceClient } from "@huggingface/inference";
//const hf = new InferenceClient(process.env.HF_TOKEN);
const hf = new InferenceClient("hf_**my_read_token**");
// 1) Define your JSON Schema
const CalendarEventSchema = {
type: "object",
additionalProperties: false,
properties: {
name: { type: "string" },
date: { type: "string" }, // e.g. "2025-08-12"
participants: { type: "array", items: { type: "string" } },
},
required: ["name", "date", "participants"],
} as const;
// 2) Call chatCompletion with response_format
const completion = await hf.chatCompletion({
model: "HuggingFaceTB/SmolLM3-3B", // pick any chat model your provider supports
provider: "auto",
messages: [
{ role: "user", content: "Alice and Bob are going to a science fair on 2025-08-12." }
],
response_format: {
type: "json_schema",
json_schema: {
name: "calendar_event",
schema: CalendarEventSchema,
strict: true, // reject anything outside the schema
},
},
max_tokens: 150,
});
// 3) Parse JSON content
const jsonText = completion.choices[0].message.content;
const event = JSON.parse(jsonText) as {
name: string; date: string; participants: string[];
};
console.log(event);