Skip to content

Structured Output

Generate typed objects using GenerationSchema and respondWithSchema().

ts
import { LanguageModelSession, GenerationSchema, GenerationGuide } from "tsfm-sdk";

interface Cat {
  name: string;
  age: number;
  breed: string;
}

async function main() {
  const schema = new GenerationSchema("Cat", "A rescue cat")
    .property("name", "string", { description: "The cat's name" })
    .property("age", "integer", {
      description: "Age in years",
      guides: [GenerationGuide.range(0, 20)],
    })
    .property("breed", "string", { description: "The cat's breed" });

  const session = new LanguageModelSession();
  const content = await session.respondWithSchema("Generate a rescue cat", schema);

  const cat: Cat = {
    name: content.value("name"),
    age: content.value("age"),
    breed: content.value("breed"),
  };
  console.log("Cat:", cat);

  session.dispose();
}

main().catch(console.error);

What This Shows

  1. Define a schema with GenerationSchema and typed properties
  2. Use GenerationGuide.range() to constrain numeric values
  3. Extract typed values with content.value<T>(key)
  4. Map results to a TypeScript interface

Released under the Apache 2.0 License.