object

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package object provides utilities for generating structured objects with automatic schema generation. It simplifies working with typed structured outputs by handling schema reflection and unmarshaling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate[T any](
	ctx context.Context,
	model fantasy.LanguageModel,
	opts fantasy.ObjectCall,
) (*fantasy.ObjectResult[T], error)

Generate generates a structured object that matches the given type T. The schema is automatically generated from T using reflection.

Example:

type Recipe struct {
    Name        string   `json:"name"`
    Ingredients []string `json:"ingredients"`
}

result, err := object.Generate[Recipe](ctx, model, fantasy.ObjectCall{
    Prompt: fantasy.Prompt{fantasy.NewUserMessage("Generate a lasagna recipe")},
})

func GenerateWithText

func GenerateWithText(
	ctx context.Context,
	model fantasy.LanguageModel,
	call fantasy.ObjectCall,
) (*fantasy.ObjectResponse, error)

GenerateWithText is a helper for providers without tool or JSON mode support. It adds the schema to the system prompt and parses the text response as JSON. This is a fallback for older models or simple providers.

func GenerateWithTool

func GenerateWithTool(
	ctx context.Context,
	model fantasy.LanguageModel,
	call fantasy.ObjectCall,
) (*fantasy.ObjectResponse, error)

GenerateWithTool is a helper for providers without native JSON mode. It converts the schema to a tool definition, forces the model to call it, and extracts the tool's input as the structured output.

func Stream

Stream streams a structured object that matches the given type T. Returns a StreamObjectResult[T] with progressive updates and deduplication.

Example:

stream, err := object.Stream[Recipe](ctx, model, fantasy.ObjectCall{
    Prompt: fantasy.Prompt{fantasy.NewUserMessage("Generate a lasagna recipe")},
})

for partial := range stream.PartialObjectStream() {
    fmt.Printf("Progress: %s\n", partial.Name)
}

result, err := stream.Object()  // Wait for final result

func StreamWithText

StreamWithText is a helper for providers without tool or JSON streaming support. It adds the schema to the system prompt and parses the streamed text as JSON progressively.

func StreamWithTool

StreamWithTool is a helper for providers without native JSON streaming. It uses streaming tool calls to extract and parse the structured output progressively.

Types

This section is empty.

Jump to

Keyboard shortcuts

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