Contents

Generable

A type that the model uses when responding to prompts.

Declaration

protocol Generable : ConvertibleFromGeneratedContent, ConvertibleToGeneratedContent

Mentioned in

Overview

Annotate your Swift structure or enumeration with the Generable macro to allow the model to respond to prompts by generating an instance of your type. Use the Guide macro to provide natural language descriptions of your properties, and programmatically control the values that the model can generate.

@Generable
struct SearchSuggestions {
    @Guide(description: "A list of suggested search terms.", .count(4))
    var searchTerms: [SearchTerm]
    @Generable
    struct SearchTerm {
        // Use a generation identifier for data structures the framework generates.
        var id: GenerationID
        @Guide(description: "A two- or three- word search term, like 'Beautiful sunsets'.")
        var searchTerm: String
    }
}

For every Generable type in a request, the framework converts its type and format information to a JSON schema and provides it to the model. This contributes to the available context window size. If the LanguageModelSession exceeds the available context size, it throws LanguageModelSession.GenerationError.exceededContextWindowSize(_:). To reduce the size of your generable type:

If the Generable type includes properties with clear names the model may have all it needs to generate your type, eliminating the need of Guide(description:). For more information on managing the context window size, see TN3193: Managing the on-device foundation model’s context window.

Topics

Defining a generable type

Creating a guide

Getting the schema

Generating a unique identifier

Converting to partially generated

Generate dynamic shemas

See Also

Guided generation