Sandcastle’s structured output feature lets you extract a typed payload from an agent’s output. Instead of parsing the agent’s stdout yourself, you declare what you expect — an XML tag and an optional schema — and Sandcastle handles extraction, validation, and type inference. The result is available onDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/mattpocock/sandcastle/llms.txt
Use this file to discover all available pages before exploring further.
result.output with full TypeScript types.
Output.object
UseOutput.object({ tag, schema }) to extract a JSON object from the agent’s output. The agent emits its answer inside an XML tag you specify, and Sandcastle JSON-parses the contents and validates them against your schema.
schema option accepts any Standard Schema-compatible validator, including Zod and Valibot.
Output.string
UseOutput.string({ tag }) to extract the contents of a tag as a plain string. The contents are whitespace-trimmed and returned as-is — no JSON parsing, no schema validation.
Constraints
BothOutput.object and Output.string require maxIterations to be 1 (which is the default). Multi-iteration runs cannot use structured output.
The resolved prompt must contain the configured opening tag literal. If you pass output: Output.object({ tag: "result", ... }) but the prompt does not contain the text <result>, run() throws before the sandbox starts.
Sandcastle does not inject the prompt instruction telling the agent to emit the tag. You own that part. The prompt in the example above explicitly tells the agent to output JSON inside
<result> tags — Sandcastle only handles the extraction and validation.The output field on RunResult
When you pass anoutput option to run(), the result object gains an output field typed as the schema’s inferred type:
output option, result.output is undefined.
StructuredOutputError
If extraction or validation fails,run() throws a StructuredOutputError. This can happen when:
- The configured XML tag was not found in the agent’s stdout (
rawMatchedisundefined) - The tag contents could not be JSON-parsed (
causecarries the parse error) - The parsed JSON failed schema validation (
causecarries the validation issues)
commits, branch, and optionally preservedWorktreePath so you can inspect the agent’s work even when output extraction fails.
Design note
Sandcastle does not inspect or modify the prompt to inject output instructions. Theoutput option is purely a post-processing declaration: you tell the agent what to emit in your prompt, and you tell Sandcastle what to extract via output. This separation means you have full control over how the instruction is worded, where it appears in the prompt, and what schema the agent is asked to conform to.