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 ¶
- func GenerateStructured[T any](ctx context.Context, p niro.Provider, req *niro.Request, ...) (T, *niro.ResponseMeta, niro.Usage, error)
- func WithSchema(req *niro.Request, schema json.RawMessage) *niro.Request
- func WithSchemaAny(req *niro.Request, schema any) (*niro.Request, error)
- type StructuredEvent
- type StructuredStream
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 ¶
WithSchema returns a shallow copy of req configured for JSON schema output.
Types ¶
type StructuredEvent ¶
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.