api.models

Schema

api.models.create({ schema })

Geben Sie der Sitzung einen JSON Schema und es beantwortet Objekte statt Prosa. Dies ist, was ein Modell in etwas verwandelt, was der Rest Ihres Codes nennen kann.

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.

Ohne Schema erhält man Text, und wer es verbraucht, muss Englisch lesen. Mit einem erhält man einen Wert – und der Unterschied ist nicht einfach, es ist, ob die nächste Funktion überhaupt geschrieben werden kann.

Einstellen

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

Es ändert, was die Sitzung standardmäßig beantwortet

Eine Sitzung mit einem Schema antwortet JSON es sei denn, ein Anruf verlangt etwas anderes; eine Sitzung ohne einen Antworttext. Sie können pro Anruf überschreiben.

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

Die Beschreibungen sind keine Dokumentation

Sie werden vom Modell gelesen, und sie sind, wo Mehrdeutigkeit geregelt wird. total allein lädt eine Vermutung über Steuern; total mit « Betrag einschließlich Steuern » nicht.

FeldOhne BeschreibungMit einem
InsgesamtVor oder nach Steuern? Jeder Anruf kann abweichen.„Einschl. Steuern" — abgegolten.
DatumJedes Format, das das Modell mag.« ISO 8601 » — leserlich.
StatusFreier Text, unbeschränkt.Enum verwenden: Der Satz ist der Vertrag.
{
  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."},
}
Bitten Sie um die Unsicherheit, nicht folgern Sie es

Ein Modell füllt jedes Feld, das es gegeben wird. Eine Vertrauenszahl oder ein Nullfeld mit „ null, wenn das Dokument nicht sagt", ist, wie man eine Lesung von einer Vermutung erzählt – und es kostet ein paar Zeichen.

Es trotzdem bestätigen

Die Form wird dringend verlangt, und Sie sollten sie noch überprüfen, bevor Sie darauf reagieren — genauso wie Sie eine Antwort von einem Dienst, den Sie nicht kontrollieren, bestätigen würden.

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")

Eine Form für einen Anruf

Sie müssen keine Sitzung öffnen, um eine Form zu erhalten. Fragen Sie nach ihr auf dem Anruf — nützlich, wenn jede Frage eine andere will.

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"],
    }}],
)

Behalten Sie es die Größe der Antwort

Das Schema wird mit jedem Anruf gesendet, so dass ein großes ein Standing-Kosten pro Antwort ist. Fragen Sie nach den Feldern, die Sie verwenden werden; ein Schema mit dreißig optionalen Feldern, die Sie nie lesen, wird für dreißig Mal über bezahlt.