api.models

schema

api.models.create({ schema })

Give the session a JSON Schema and it answers objects instead of prose. This is what turns a model into something the rest of your code can call.

set at creationchanges the default outputJSON Schema
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

Without a schema you get text, and whoever consumes it has to read English. With one, you get a value — and the difference is not convenience, it is whether the next function can be written at all.

Setting one

const extractor = await api.models.create({
  model: "gemini-3.5-flash",
  settings: { temperature: 0 },
  schema: {
    type: "object",
    properties: {
      supplier: { type: "string" },
      total:    { type: "number", description: "Amount including tax" },
      currency: { type: "string", description: "ISO 4217, e.g. EUR" },
      dueDate:  { type: "string", description: "ISO 8601 date" },
      lines: {
        type: "array",
        items: {
          type: "object",
          properties: { label: { type: "string" }, amount: { type: "number" } },
          required: ["label", "amount"],
        },
      },
    },
    required: ["supplier", "total", "currency"],
  },
});

const { outputs } = await extractor.generate({
  inputs: [{ type: "text", content: "Extract this invoice." },
           { type: "file", url: "https://example.com/invoice.pdf" }],
});

outputs[0].content;   // an object, not a string
extractor = api.models.create(
    model="gemini-3.5-flash",
    settings={"temperature": 0},
    schema={
        "type": "object",
        "properties": {
            "supplier": {"type": "string"},
            "total": {"type": "number", "description": "Amount including tax"},
            "currency": {"type": "string", "description": "ISO 4217, e.g. EUR"},
            "dueDate": {"type": "string", "description": "ISO 8601 date"},
            "lines": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {"label": {"type": "string"}, "amount": {"type": "number"}},
                    "required": ["label", "amount"],
                },
            },
        },
        "required": ["supplier", "total", "currency"],
    },
)

outputs = extractor.generate(inputs=[
    {"type": "text", "content": "Extract this invoice."},
    {"type": "file", "url": "https://example.com/invoice.pdf"},
])["outputs"]

outputs[0]["content"]   # an object, not a string

It changes what the session answers by default

A session with a schema answers JSON unless a call asks for something else; a session without one answers text. You can override per call.

await extractor.generate({ inputs });                          // JSON — the schema
await extractor.generate({ inputs, outputs: [{ type: "text" }] }); // prose, this once
extractor.generate(inputs=inputs)                              # JSON — the schema
extractor.generate(inputs=inputs, outputs=[{"type": "text"}])  # prose, this once

The descriptions are not documentation

They are read by the model, and they are where ambiguity is settled. total alone invites a guess about tax; total with « Amount including tax » does not.

FieldWithout a descriptionWith one
totalBefore or after tax? Each call may differ.« Amount including tax » — settled.
dateAny format the model likes.« ISO 8601 » — parseable.
statusFree text, unbounded.Use enum: the set is the contract.
{
  status: { type: "string", enum: ["paid", "pending", "overdue"] },
  confidence: { type: "number", description: "0 to 1. Below 0.5 means the document was unclear." }
}
{
    "status": {"type": "string", "enum": ["paid", "pending", "overdue"]},
    "confidence": {"type": "number", "description": "0 to 1. Below 0.5 means the document was unclear."},
}
Ask for the uncertainty, do not infer it

A model fills every field it is given. A confidence number, or a nullable field with « null when the document does not say », is how you tell a reading from a guess — and it costs a few tokens.

Validate it anyway

The shape is asked for, strongly, and you should still check it before acting on it — the same way you would validate a response from any service you do not control.

const invoice = outputs[0].content;

if (typeof invoice?.total !== "number" || !invoice.currency) {
  throw new Error("the extraction did not come back in the expected shape");
}
invoice = outputs[0]["content"]

if not isinstance(invoice, dict) or not isinstance(invoice.get("total"), (int, float)) or not invoice.get("currency"):
    raise ValueError("the extraction did not come back in the expected shape")

A shape for one call only

You do not have to open a session to get a shape. Ask for it on the call — useful when each question wants a different one.

await assistant.generate({
  inputs: [{ type: "text", content: "Three risks of shipping on a Friday." }],
  outputs: [{ type: "json", schema: {
    type: "object",
    properties: { risks: { type: "array", items: { type: "string" } } },
    required: ["risks"],
  } }],
});
assistant.generate(
    inputs=[{"type": "text", "content": "Three risks of shipping on a Friday."}],
    outputs=[{"type": "json", "schema": {
        "type": "object",
        "properties": {"risks": {"type": "array", "items": {"type": "string"}}},
        "required": ["risks"],
    }}],
)

Keep it the size of the answer

The schema is sent with every call, so a large one is a standing cost per answer. Ask for the fields you will use; a schema with thirty optional fields you never read is paid for thirty times over.