Skip to main content
Structured output asks a compatible provider/model to return JSON matching a schema. Parse and validate the result before using it as application data; constrained output is not a substitute for business validation.

Build a schema from Go

With model and ctx configured:
NewOutputSchema returns a schema map. NewOutputFormatJSONSchema(value, strict) wraps it with the format type, name, and strict setting. You can supply a hand-written TextFormat.Format map for a provider-compatible schema. The schema helper removes schema metadata and inlines definitions. Providers support different JSON Schema subsets; test nested objects, nullable/optional fields, enums, and strictness with your selected model.

Agent output

Set AgentConfig.Output to hastekit.NewOutputSchema(Classification{}) to configure the agent’s final output schema. This convenience path uses non-strict structured output. If you need explicit strictness, configure AgentConfig.Parameters.Text directly and leave Output unset, because Output sets that text format.

Parse only complete output

Extract assistant text from a completed response or use an agent result’s Text(), then decode JSON into your application type. Check errors and interruption status before parsing. Partial streaming JSON is not a completed structured result; accumulate and parse after successful completion. Handle refusals, output-limit exhaustion, invalid JSON, and valid JSON with unacceptable field values as separate application outcomes.