# Structured output in Typescript SDK

**URL:** <https://discuss.huggingface.co/t/structured-output-in-typescript-sdk/166399>\
**Category:** Beginners\
**Created:** [August 11, 2025, 11:10am UTC](https://discuss.huggingface.co/t/structured-output-in-typescript-sdk/166399 "2025-08-11T11:10:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bilaaaaaaaaaaaal](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@bilaaaaaaaaaaaal](https://discuss.huggingface.co/u/bilaaaaaaaaaaaal)\
**Post date:** [August 11, 2025, 11:10am UTC](https://discuss.huggingface.co/t/structured-output-in-typescript-sdk/166399/1 "2025-08-11T11:10:55Z")

</div>

How do I get structured output in the typescript SDK? Can anyone add a code snippet?

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [August 11, 2025, 1:22pm UTC](https://discuss.huggingface.co/t/structured-output-in-typescript-sdk/166399/2 "2025-08-11T13:22:33Z")

</div>

Seems this works for now…

```typescript
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);

```

- [Structured Outputs with Inference Providers](https://huggingface.co/docs/inference-providers/en/guides/structured-output)
- [🤗 Hugging Face Inference](https://huggingface.co/docs/huggingface.js/en/inference/README)
- [Pr 2982 ci branch](https://github.com/huggingface/text-generation-inference/pull/3046)
