Struktur

extract()

Main extraction function for the TypeScript SDK.

Usage

import { extract, simple } from "@struktur/sdk";
import { openai } from "@ai-sdk/openai";

const result = await extract({
  artifacts,
  schema,
  strategy: simple({ model: openai("gpt-4o-mini") }),
});

console.log(result.data);
console.log(result.usage.totalTokens);

Options

Prop

Type

Exactly one of schema or fields must be provided. Passing both, or neither, throws immediately.

Result

Prop

Type

Using fields instead of schema

For quick extractions where you don't need full JSON Schema control, pass a fields string:

const result = await extract({
  artifacts,
  fields: "title, author, year:integer, genre:enum{fiction|nonfiction|reference}",
  strategy: simple({ model: openai("gpt-4o-mini") }),
});

fields supports scalar types (string, number/float, boolean/bool, integer, int), enum sets (status:enum{draft|live}), and arrays (tags:array{string}). See the Fields Shorthand reference for the full syntax.

See also

On this page