FirebaseAILogic Framework Reference

TemplateGenerativeModel

public final class TemplateGenerativeModel : Sendable

[Public Preview] A multimodal model (like Gemini) that generates content using server-managed prompt templates.

Warning

This API is a public preview and may be subject to change.

When using server prompt templates, prompts, system instructions, tools, and model configurations are defined and stored server-side in the Firebase console or via the Firebase AI Logic REST API. Client applications only pass a template identifier and any runtime input variables required by the template.

Benefits of this approach include:

  • Protecting prompts and system instructions from exposure in client code.
  • Updating prompts, schemas, and configurations without releasing a new app version.

Tip

We recommend versioning your template IDs (for example, my-template-v1-0-0) and using Firebase Remote Config to dynamically manage template IDs in client requests.

For more details, see the Server Prompt Templates guide.

  • [Public Preview] Generates content from a prompt template and inputs.

    Warning

    This API is a public preview and may be subject to change.

    Throws

    A GenerateContentError if the request failed, or an EncodingError if inputs cannot be encoded.

    Declaration

    Swift

    public func generateContent(templateID: String,
                                inputs: [String: Any] = [:]) async throws
      -> GenerateContentResponse

    Parameters

    templateID

    The ID of the server prompt template to use (for example, "my-template-v1-0-0").

    inputs

    A dictionary of variable names and values to substitute into the template's Handlebars placeholders (such as {{variable}}) or input.schema. Supported value types include String, Int, Double, Float, Bool, arrays, and nested dictionaries; defaults to no inputs.

    Return Value

    The content generated by the model.

  • [Public Preview] Generates content from a prompt template and inputs, with streaming responses.

    Warning

    This API is a public preview and may be subject to change.

    Throws

    A GenerateContentError if the request failed, or an EncodingError if inputs cannot be encoded.

    Declaration

    Swift

    @available(macOS 12.0, watchOS 8.0, *)
    public func generateContentStream(templateID: String, inputs: [String: Any] = [:]) throws
      -> AsyncThrowingStream<GenerateContentResponse, Error>

    Parameters

    templateID

    The ID of the server prompt template to use (for example, "my-template-v1-0-0").

    inputs

    A dictionary of variable names and values to substitute into the template's Handlebars placeholders (such as {{variable}}) or input.schema. Supported value types include String, Int, Double, Float, Bool, arrays, and nested dictionaries; defaults to no inputs.

    Return Value

    An AsyncThrowingStream that yields GenerateContentResponse objects.

  • [Public Preview] Creates a new chat conversation using this model with the provided template, inputs, and history.

    Warning

    This API is a public preview and may be subject to change.

    The template's model, system instructions, and configurations apply to every turn automatically, while the returned TemplateChat tracks conversation history.

    For more details, see Multi-turn interactions with server prompt templates.

    Declaration

    Swift

    public func startChat(templateID: String,
                          inputs: [String: Any] = [:],
                          history: [ModelContent] = []) -> TemplateChat

    Parameters

    templateID

    The ID of the server prompt template to use.

    inputs

    A dictionary of variable names and values to substitute into the template; the same inputs are sent with every turn of the conversation. Defaults to no inputs.

    history

    The previous conversation history to seed the chat session with.

    Return Value

    A new TemplateChat instance.