structured

package
v0.4.0-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package structured provides typed decoding of JSON Schema-constrained LLM output.

Use when the model is instructed to return JSON matching a schema and you want a typed Go struct instead of raw bytes.

One-shot: GenerateStructured runs the request and unmarshals the final response into T.

Streaming: StreamStructured parses partial JSON as it arrives and emits StructuredEvent with Partial and Final fields. Use for progressive UI or validation.

Request must have ResponseFormat = "json_schema" and ResponseSchema set. See WithSchema and WithSchemaAny to configure the request.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateStructured

func GenerateStructured[T any](ctx context.Context, p niro.Provider, req *niro.Request, schema json.RawMessage) (T, *niro.ResponseMeta, niro.Usage, error)

GenerateStructured runs a request with JSON schema output and returns the final typed result.

func WithSchema

func WithSchema(req *niro.Request, schema json.RawMessage) *niro.Request

WithSchema returns a shallow copy of req configured for JSON schema output.

func WithSchemaAny

func WithSchemaAny(req *niro.Request, schema any) (*niro.Request, error)

WithSchemaAny marshals schema using the configured JSON library and applies it.

Types

type StructuredEvent

type StructuredEvent[T any] struct {
	Partial *T
	Final   *T
	Done    bool
}

StructuredEvent is emitted by StructuredStream.Next.

Partial is set when a valid JSON object/array can be parsed from the stream before completion. Final is set once at the end of the stream.

The pointers are only valid until the next call to Next.

type StructuredStream

type StructuredStream[T any] struct {
	// contains filtered or unexported fields
}

StructuredStream decodes a stream of text frames into typed JSON output.

func StreamStructured

func StreamStructured[T any](ctx context.Context, p niro.Provider, req *niro.Request, schema json.RawMessage) (*StructuredStream[T], error)

StreamStructured runs a request with JSON schema output and returns a decoder stream that yields partial and final structured outputs.

func (*StructuredStream[T]) Err

func (s *StructuredStream[T]) Err() error

Err returns the first error encountered during decoding.

func (*StructuredStream[T]) Event

func (s *StructuredStream[T]) Event() StructuredEvent[T]

Event returns the current structured event.

func (*StructuredStream[T]) Next

func (s *StructuredStream[T]) Next(ctx context.Context) bool

Next advances the structured stream and emits either a partial or final event.

func (*StructuredStream[T]) Response

func (s *StructuredStream[T]) Response() *niro.ResponseMeta

Response returns provider response metadata.

func (*StructuredStream[T]) Usage

func (s *StructuredStream[T]) Usage() niro.Usage

Usage returns the accumulated token usage.

Jump to

Keyboard shortcuts

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