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
AGenerateContentErrorif the request failed, or anEncodingErrorifinputscannot be encoded.Declaration
Swift
public func generateContent(templateID: String, inputs: [String: Any] = [:]) async throws -> GenerateContentResponseParameters
templateIDThe ID of the server prompt template to use (for example,
"my-template-v1-0-0").inputsA dictionary of variable names and values to substitute into the template's Handlebars placeholders (such as
{{variable}}) orinput.schema. Supported value types includeString,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
AGenerateContentErrorif the request failed, or anEncodingErrorifinputscannot be encoded.Declaration
Swift
@available(macOS 12.0, watchOS 8.0, *) public func generateContentStream(templateID: String, inputs: [String: Any] = [:]) throws -> AsyncThrowingStream<GenerateContentResponse, Error>Parameters
templateIDThe ID of the server prompt template to use (for example,
"my-template-v1-0-0").inputsA dictionary of variable names and values to substitute into the template's Handlebars placeholders (such as
{{variable}}) orinput.schema. Supported value types includeString,Int,Double,Float,Bool, arrays, and nested dictionaries; defaults to no inputs.Return Value
An
AsyncThrowingStreamthat yieldsGenerateContentResponseobjects. -
[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
TemplateChattracks 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] = []) -> TemplateChatParameters
templateIDThe ID of the server prompt template to use.
inputsA 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.
historyThe previous conversation history to seed the chat session with.
Return Value
A new
TemplateChatinstance.