schema

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package schema provides JSON schema generation utilities for AI agent tool definitions.

Use NewGenerator or the package-level Reflect and ReflectType helpers to derive JSON schemas from Go types. The generator is pre-configured with defaults suitable for tool input schemas.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reflect

func Reflect(v any) *jsonschema.Schema

Reflect derives the JSON schema for the provided value using a default generator.

func ReflectType

func ReflectType[T any]() *jsonschema.Schema

ReflectType allocates a zero value of T and reflects it to a schema.

Example

ExampleReflectType demonstrates generating a JSON schema from a Go type.

package main

import (
	"encoding/json"
	"fmt"

	"chainguard.dev/driftlessaf/agents/schema"
)

func main() {
	type Input struct {
		Path   string `json:"path" jsonschema:"required,description=File path to read"`
		Offset int    `json:"offset,omitempty" jsonschema:"description=Byte offset"`
	}

	s := schema.ReflectType[Input]()
	data, err := json.Marshal(s)
	if err != nil {
		panic(err)
	}

	var m map[string]any
	if err := json.Unmarshal(data, &m); err != nil {
		panic(err)
	}
	fmt.Println("type:", m["type"])
}
Output:
type: object

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator wraps jsonschema.Reflector with project defaults.

func NewGenerator

func NewGenerator() *Generator

NewGenerator constructs a generator wired with the defaults we need for tool schemas.

Example

ExampleNewGenerator demonstrates creating a generator and reflecting a value.

package main

import (
	"encoding/json"
	"fmt"

	"chainguard.dev/driftlessaf/agents/schema"
)

func main() {
	type Params struct {
		Query string `json:"query" jsonschema:"required"`
	}

	g := schema.NewGenerator()
	s := g.Reflect(&Params{})
	data, _ := json.Marshal(s)

	var m map[string]any
	_ = json.Unmarshal(data, &m)
	fmt.Println("type:", m["type"])
}
Output:
type: object

func (*Generator) Reflect

func (g *Generator) Reflect(v any) *jsonschema.Schema

Reflect returns the JSON schema for the provided value.

Directories

Path Synopsis
Package schematest provides test helpers for comparing JSON schemas.
Package schematest provides test helpers for comparing JSON schemas.

Jump to

Keyboard shortcuts

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