Structured Output

The built-in generate_object tool asks the configured LLM for a JSON value, validates the response against a JSON Schema you supply, and returns the validated value only on a zero-exit result. Use it whenever you need machine-readable results: extraction, classification, config generation, or feeding another program.

You can call it directly through session.tool('generate_object', ...). The tool result carries the validated object as JSON on result.output — parse it and read the object field. The same tool also supports agent-driven invocation, where the model decides to call it during a send.

Direct tool call

The simplest path: call generate_object directly, check the tool exit code, and parse the validated object out of the result.

Rust
Node.js
Python
Go

The validated value lives on the object key of the parsed output. When result.exitCode (Node) / result.exit_code (Python) is zero, fields declared in required have passed runtime validation. If the model cannot satisfy the schema after repair attempts, the tool reports a non-zero exit code.

Enum classification

Constrain a field to a fixed set with enum. This turns a free-form model classification into a schema-gated result.

Rust
Node.js
Python
Go

Nested schemas and arrays

Schemas can nest objects and arrays within the runtime's bounded schema-depth limit, and the runtime validates the whole structure. This models real config files, manifests, or API payloads in one call.

Rust
Node.js
Python
Go

Agent-driven invocation

You can also let the agent decide when to use structured output. Ask it to call generate_object during a send; it gathers context first, then emits the object.

Rust
Node.js
Python
Go
Rust
let result = session
.send(
"Use generate_object to extract the movie title, year, and genre from: \
The movie \"Inception\" was released in 2010 and is a sci-fi thriller.",
None,
)
.await?;
println!(
"tool calls: {}, tokens: {}",
result.tool_calls_count,
result.usage.total_tokens
);

Schema validation coverage

The built-in validator supports:

  • type (including nullable arrays like ["string", "null"])
  • required, properties, additionalProperties
  • enum, const
  • $ref with local $defs or definitions
  • allOf, anyOf, oneOf
  • minLength, maxLength, pattern
  • minimum, maximum, exclusiveMinimum, exclusiveMaximum
  • minItems, maxItems, items
  • Nested object and array validation, including root arrays and scalar values

Notes

  • The validated value is on the object key of the parsed result.output. auto selects forced-tool mode when the provider declares that capability and otherwise uses the prompt+schema fallback. Explicit strict and json modes use provider-native response formats only when supported, then safely fall back to forced-tool or prompt mode.
  • List every field you depend on in required — the runtime enforces it, so missing or mistyped fields fail validation instead of silently returning partial data.
  • generate_object is a built-in tool registered independently of built-in skills.
  • Direct session.tool(...) calls are host control-plane calls. Use permissionPolicy when you let the model choose tools inside send / run / stream; use host authorization before direct SDK calls.

A runnable version ships at sdk/node/examples/basic/test_generate_object.ts and sdk/python/examples/test_generate_object.py.