Skip to content

Conversation

@paulb777
Copy link
Member

@paulb777 paulb777 commented Oct 9, 2025

Apple implementation of Server Prompt Templates inspired by Flutterfire PR.
Googlers: see API review at go/firebase-ai-server-prompt-template

Summary of Changes

This pull request significantly enhances the Firebase AI SDK by introducing support for server-side prompt templates. This allows developers to define and use reusable templates for generative AI models, making it easier to manage complex prompts and integrate dynamic data. The changes include new model types for generative and image AI, a dedicated chat session for template-based conversations, and a refactored, thread-safe history management system. These additions provide a more robust and flexible framework for building AI-powered applications.

Highlights

  • Server-Side Prompt Templates: Introduced new TemplateGenerativeModel and TemplateImagenModel classes to support server-side prompt templates, allowing for more flexible and dynamic AI interactions.
  • Template Chat Sessions: Added TemplateChatSession to enable conversational interactions with generative models using predefined templates and dynamic variables, supporting both single messages and streaming responses.
  • Refactored Chat History Management: Extracted chat history logic into a new, dedicated History class, improving modularity, reusability, and thread safety for managing conversation history in both standard and template-based chat sessions.
  • Dynamic Template Variables: Implemented TemplateVariable enum to handle various data types (String, Int, Double, Bool, Array, Dictionary) for substituting values into prompt templates, including automatic conversion for Float to Double.
  • New API Request Structures: Created specific request types (TemplateGenerateContentRequest, TemplateGenerateImagesRequest) and internal API method enums (APIMethod, ImageAPIMethod) to support the new template-based API endpoints.
  • Comprehensive Testing: Added extensive integration and unit tests for the new template models and chat sessions, covering various scenarios including text generation, image generation, and chat history management.
Changelog
  • FirebaseAI/Sources/Chat.swift
    • Refactored to use the new History class for managing chat history, removing internal history management logic and related locks.
    • Updated appendHistory calls to use the _history instance of the new History class.
  • FirebaseAI/Sources/FirebaseAI.swift
    • Added public factory methods templateGenerativeModel() and templateImagenModel() to initialize new template-based AI models.
  • FirebaseAI/Sources/GenerateContentRequest.swift
    • Removed the APIMethod enum, which has been moved to a new, dedicated internal file.
  • FirebaseAI/Sources/GenerativeAIService.swift
    • Changed the firebaseInfo property from private let to let to allow access from newly introduced template models.
  • FirebaseAI/Sources/History.swift
    • Added new file: Introduces a History class to encapsulate chat history management, including thread-safe appending of ModelContent and aggregation of content chunks.
  • FirebaseAI/Sources/TemplateChatSession.swift
    • Added new file: Defines TemplateChatSession for managing chat conversations with server-side prompt templates, providing sendMessage and sendMessageStream methods.
  • FirebaseAI/Sources/TemplateGenerateContentRequest.swift
    • Added new file: Defines the request structure for generating content using prompt templates, including template, variables, history, and project ID.
  • FirebaseAI/Sources/TemplateGenerateImagesRequest.swift
    • Added new file: Defines the request structure for generating images using prompt templates, including template, variables, and project ID.
  • FirebaseAI/Sources/TemplateGenerativeModel.swift
    • Added new file: Introduces TemplateGenerativeModel for interacting with generative AI models using templates, including methods for content generation and starting chat sessions.
  • FirebaseAI/Sources/TemplateImagenModel.swift
    • Added new file: Introduces TemplateImagenModel for interacting with image generation models using templates.
  • FirebaseAI/Sources/TemplateVariable.swift
    • Added new file: Defines TemplateVariable enum to handle various data types for template variables, including a conversion from Float to Double during initialization.
  • FirebaseAI/Sources/Types/Internal/APIMethod.swift
    • Added new file: Moved the APIMethod enum (generateContent, streamGenerateContent, countTokens) here from GenerateContentRequest.swift.
  • FirebaseAI/Sources/Types/Internal/ImageAPIMethod.swift
    • Added new file: Defines ImageAPIMethod enum specifically for image generation API methods, such as templatePredict.
  • FirebaseAI/Tests/TestApp/Tests/Integration/ServerPromptTemplateIntegrationTests.swift
    • Added new file: Contains integration tests for TemplateGenerativeModel and TemplateImagenModel, covering text generation, image generation, and chat sessions with templates and media.
  • FirebaseAI/Tests/Unit/TemplateChatSessionTests.swift
    • Added new file: Provides unit tests for the TemplateChatSession class, verifying sendMessage and sendMessageStream functionality.
  • FirebaseAI/Tests/Unit/TemplateGenerativeModelTests.swift
    • Added new file: Includes unit tests for the TemplateGenerativeModel class, testing generateContent and generateContentStream methods.
  • FirebaseAI/Tests/Unit/TemplateImagenModelTests.swift
    • Added new file: Contains unit tests for the TemplateImagenModel class, specifically testing the generateImages method.
  • FirebaseAI/Tests/Unit/TestUtilities/GenerativeModelTestUtil.swift
    • Modified httpRequestHandler to include an isTemplateRequest parameter, allowing for conditional URL path assertions to support template-based requests in mock tests.
    • Added a new helper function collectTextFromStream to simplify collecting text from AsyncThrowingStream responses in tests.
Activity
  • The Danger bot initially flagged a missing changelog entry.
  • A review comment from andrewheard suggested extracting chat history management into a separate type, which was subsequently implemented by paulb777.
  • Multiple automated review comments from gemini-code-assist[bot] highlighted potential issues with force unwrapping URLs in new request types and suggested percent-encoding template names for robustness. paulb777 acknowledged these, noting consistency with existing code.
  • A suggestion from gemini-code-assist[bot] to handle Float values in TemplateVariable initialization was addressed and implemented in the changes.
  • Other low-priority comments from gemini-code-assist[bot] included a TODO for streaming and a hardcoded path in a test utility.

@google-oss-bot
Copy link

1 Warning
⚠️ Did you forget to add a changelog entry? (Add #no-changelog to the PR description to silence this warning.)

Generated by 🚫 Danger

@paulb777
Copy link
Member Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces server-side prompt templates, a significant feature addition. The changes include new models (TemplateGenerativeModel, TemplateImagenModel), request types, and chat session logic to support using templates with variables. The code is well-structured, with good separation of concerns, such as moving history management into its own History class. I've identified a couple of areas where force unwraps on URL creation could be made safer. Overall, this is a solid implementation with good test coverage.

@paulb777
Copy link
Member Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for server-side prompt templates for both generative and image generation models. This is a significant feature addition, including new model types like TemplateGenerativeModel and TemplateImagenModel, new request structures, and a TemplateChatSession for conversational AI with templates. The changes also involve some nice refactoring, such as abstracting history management into a dedicated History class. The overall implementation is well-structured and comes with a good set of unit and integration tests. My feedback is focused on improving robustness by avoiding force-unwraps in URL construction, ensuring proper URL encoding of user-provided data, and making the TemplateVariable type more versatile by handling Float values. I've also included a minor suggestion to clean up test code.

@paulb777
Copy link
Member Author

/gemini summarize

@firebase firebase deleted a comment from gemini-code-assist bot Oct 14, 2025
@paulb777 paulb777 marked this pull request as ready for review October 14, 2025 23:13
@paulb777
Copy link
Member Author

@andrewheard @cynthiajoan Still some test tweaks once the backend supports both API providers with the same templates and this won't merge until after M172, but should now be ready for review.

@paulb777 paulb777 added this to the 12.6.0 - M173 milestone Oct 14, 2025
paulb777 and others added 16 commits October 16, 2025 15:27
Updated `GenerateImagesRequest` to encode the `variables` property as `inputs`, similar to how `TemplateGenerateContentRequest` handles it. This ensures consistency across the API.
Updated `GenerateImagesRequest` to correctly construct the URL, including the `projectID` and `location`. This mirrors the URL construction in `TemplateGenerateContentRequest` and fixes a 404 error that was occurring in the `testGenerateImages` integration test.

The `TemplateImagenModel` was also updated to pass the `projectID` to the `GenerateImagesRequest` initializer.
@paulb777
Copy link
Member Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant enhancement to the Firebase AI SDK by adding support for server-side prompt templates. The changes are well-structured, with new classes for template-based models (TemplateGenerativeModel, TemplateImagenModel) and a dedicated TemplateChatSession. The refactoring of chat history into a separate History class is a great improvement for modularity and thread safety. Additionally, the move from force-unwrapping URLs to a throwing mechanism enhances the robustness of the networking layer. My review focuses on ensuring URL components are properly encoded, maintaining consistency in error handling, and improving the maintainability of test utilities. Overall, this is a high-quality contribution.

paulb777 and others added 2 commits October 24, 2025 14:44
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@paulb777 paulb777 requested a review from andrewheard October 27, 2025 14:12
@paulb777
Copy link
Member Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces significant new functionality by adding support for server-side prompt templates. The changes are well-structured, with new classes for template-based models (TemplateGenerativeModel, TemplateImagenModel) and chat sessions (TemplateChatSession). The refactoring of chat history management into a dedicated History class is a great improvement for modularity and thread safety. The move away from force-unwrapping URLs to throwing functions is also a commendable improvement in robustness. My review focuses on further improving the robustness of URL construction and enhancing test maintainability.

@paulb777 paulb777 merged commit 4f36a1c into main Oct 31, 2025
65 checks passed
@paulb777 paulb777 deleted the pb-spt branch October 31, 2025 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants