Help us shape the future of Go! Take the 2026 Go Developer Survey

jsonformat

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package jsonformat provides JSON schema-based response formatting and validation for structured agent output, deriving schemas from Go types and validating and normalizing values against them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any() agent.ResponseFormat

Any returns a ResponseFormat representing the any type (no schema constraints).

func For

func For[T any]() (agent.ResponseFormat, error)

For creates a ResponseFormat for the type T.

func ForType

func ForType(rt reflect.Type) (agent.ResponseFormat, error)

ForType creates a ResponseFormat for the given reflect.Type. A nil rt is treated as Nothing.

func MustFor

func MustFor[T any]() agent.ResponseFormat

MustFor calls For and panics on error.

func New

func New(name, description string, schema *jsonschema.Schema) agent.ResponseFormat

New creates a new JSON response format with the given name, description, and schema. A nil schema represents an unconstrained JSON value.

func Nothing

func Nothing() agent.ResponseFormat

Nothing returns a ResponseFormat that matches no values.

func WrapNonObjectSchema added in v0.1.1

func WrapNonObjectSchema(format agent.ResponseFormat) (agent.ResponseFormat, error)

WrapNonObjectSchema returns a response format with an object-root schema. A non-object JSON schema is copied into a required "data" property. The supplied format and its schema are not modified; ForType still describes the unwrapped value for local validation.

Types

type Format

type Format struct {
	agent.ResponseFormat
	// contains filtered or unexported fields
}

Format provides JSON schema-based response formatting and validation.

func FromResponseFormat

func FromResponseFormat(format agent.ResponseFormat) (*Format, error)

FromResponseFormat creates a Format from a response format data value.

func (*Format) Marshal

func (f *Format) Marshal(v any) ([]byte, error)

Marshal marshals v into JSON according to the provided format, validating against the format's schema.

func (*Format) Normalize

func (f *Format) Normalize(v any) error

Normalize applies schema defaults to v and validates it against the format's schema.

v must be a non-nil pointer to the value being normalized.

func (*Format) Unmarshal

func (f *Format) Unmarshal(data []byte, v any) error

Unmarshal unmarshals data into v according to the provided format, validating against the format's schema.

func (*Format) UnmarshalStructuredOutput added in v0.1.1

func (f *Format) UnmarshalStructuredOutput(data []byte, target any) error

UnmarshalStructuredOutput validates and decodes a response formatted by WrapNonObjectSchema. It unwraps non-object results from "data", while accepting valid bare JSON as a fallback. Extra envelope properties are ignored. Object results use the first top-level JSON value; wrapped results require a single top-level value. Format.Unmarshal uses its schema as supplied.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL