Structured Output
Gojent generates strict JSON Schemas from Go struct types and decodes model responses into strongly typed values via agent.GenerateStructured[T] or agent.WithOutputSchema[T]. Providers that cannot honor strict JSON Schema fail closed with llm.ErrUnsupported instead of silently relaxing constraints.
Decode directly with GenerateStructured
agent.GenerateStructured[T] attaches a strict schema derived from T, runs Generate, and unmarshals the final assistant text into T:
package main
import (
"context"
"fmt"
"log"
"github.com/ijaidev/gojent/core/agent"
"github.com/ijaidev/gojent/providers/mockllm"
)
type WeatherReport struct {
City string `json:"city" description:"City name"`
Temperature float64 `json:"temperature_c" description:"Current temperature in Celsius"`
Conditions []string `json:"conditions" description:"Short condition labels"`
}
func main() {
ctx := context.Background()
model := mockllm.New(`{"city":"Tokyo","temperature_c":21.5,"conditions":["clear"]}`)
assistant, err := agent.New(agent.Config{
Name: "weather",
Model: model,
})
if err != nil {
log.Fatal(err)
}
report, response, err := agent.GenerateStructured[WeatherReport](
ctx, assistant, "Report the weather in Tokyo.",
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s: %.1f°C (%d tokens)\n", report.City, report.Temperature, response.Usage.TotalTokens)
}
Use WithOutputSchema and Response.Unmarshal
When you want to pass additional options or decode manually (for example after Stream + Wait), attach agent.WithOutputSchema[T](opts...) and call response.Unmarshal(&target):
package main
import (
"context"
"fmt"
"log"
"github.com/ijaidev/gojent/core/agent"
"github.com/ijaidev/gojent/providers/mockllm"
)
type Summary struct {
Headline string `json:"headline" description:"One-line summary"`
}
func main() {
ctx := context.Background()
assistant, err := agent.New(agent.Config{
Name: "summarizer",
Model: mockllm.New("```json\n{\"headline\":\"All checks passed\"}\n```"),
})
if err != nil {
log.Fatal(err)
}
response, err := assistant.Generate(ctx, "Summarize the build.",
agent.WithOutputSchema[Summary](),
)
if err != nil {
log.Fatal(err)
}
var out Summary
if err := response.Unmarshal(&out); err != nil {
log.Fatal(err)
}
fmt.Println(out.Headline)
}
Response.Unmarshal tolerates a single Markdown JSON code fence around the payload. Pass tool.SchemaOption values to WithOutputSchema[T](opts...) to customize schema generation.
Strict capability enforcement
Because WithOutputSchema[T] sets Strict: true on llm.ResponseFormat, the runner checks model.Info().Features.StrictJSONSchema before sending the request. Adapters that do not advertise strict JSON Schema support (such as the current Anthropic Messages adapter) reject the request with llm.ErrUnsupported rather than silently dropping the schema.
See pkg.go.dev for GenerateStructured and Response details.