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 ¶
- func Generate[T any](ctx context.Context, model fantasy.LanguageModel, opts fantasy.ObjectCall) (*fantasy.ObjectResult[T], error)
- func GenerateWithText(ctx context.Context, model fantasy.LanguageModel, call fantasy.ObjectCall) (*fantasy.ObjectResponse, error)
- func GenerateWithTool(ctx context.Context, model fantasy.LanguageModel, call fantasy.ObjectCall) (*fantasy.ObjectResponse, error)
- func Stream[T any](ctx context.Context, model fantasy.LanguageModel, opts fantasy.ObjectCall) (*fantasy.StreamObjectResult[T], error)
- func StreamWithText(ctx context.Context, model fantasy.LanguageModel, call fantasy.ObjectCall) (fantasy.ObjectStreamResponse, error)
- func StreamWithTool(ctx context.Context, model fantasy.LanguageModel, call fantasy.ObjectCall) (fantasy.ObjectStreamResponse, error)
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 ¶
func Stream[T any]( ctx context.Context, model fantasy.LanguageModel, opts fantasy.ObjectCall, ) (*fantasy.StreamObjectResult[T], error)
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 ¶
func StreamWithText( ctx context.Context, model fantasy.LanguageModel, call fantasy.ObjectCall, ) (fantasy.ObjectStreamResponse, error)
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 ¶
func StreamWithTool( ctx context.Context, model fantasy.LanguageModel, call fantasy.ObjectCall, ) (fantasy.ObjectStreamResponse, error)
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.