Skip to content

Conversation

@paulb777
Copy link
Member

@paulb777 paulb777 commented Dec 30, 2025

  1. Refactored GenerativeModel.swift to support generateObject with FirebaseGenerable.
  2. Consolidated logic for JSON response parsing and cleaning.
  3. Implemented robust JSON cleanup (stripping markdown blocks).
  4. Verified compatibility with Apple's @generable macro.
  5. Added integration tests (including verifying @generable support via FoundationModels path).
  6. Updated documentation for generateObject to correctly describe parameters and order.

#no-changelog

@gemini-code-assist
Copy link
Contributor

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

@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 valuable feature by adding a generateObject overload for FirebaseGenerable types, enabling structured output without a dependency on FoundationModels. The implementation is well-executed, with clean and effective use of Swift's Codable for schema conversion. The changes to JSONSchema to support on-the-fly generation of its internal representation are a great improvement, fixing a potential crash. The addition of comprehensive integration tests ensures the new functionality is robust and works as expected across different models. Overall, this is a high-quality contribution. I have one minor suggestion regarding the JSON response parsing for improved robustness.

@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 structured output generation for types conforming to the new FirebaseGenerable protocol. The changes include a new generateObject method in GenerativeModel, schema conversion logic in JSONSchema, and helper methods for parsing the JSON response. A potential crash in JSONSchema encoding has also been fixed by ensuring an internal representation is always created. New integration tests are added to verify the end-to-end functionality. The changes are well-implemented and tested. I have one minor suggestion to improve documentation clarity.

@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 structured output support using FirebaseGenerable, refactors JSON parsing, and adds corresponding integration tests. The changes are well-structured. My review includes suggestions to improve documentation clarity, refactor duplicated code for better maintainability, and enhance test consistency.

@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 structured output using FirebaseGenerable, refactoring existing logic for FoundationModels.Generable to share common functionality. The changes include consolidating JSON response parsing, cleaning up JSON strings by removing markdown, and adding comprehensive integration tests for the new capabilities. The code is well-organized, and the documentation has been updated accordingly. I have one minor suggestion to enhance code style.

@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 structured output generation for types conforming to FirebaseGenerable, aligning its functionality with the existing support for FoundationModels.Generable. The changes include a significant refactoring to consolidate JSON response parsing, cleaning, and generation configuration logic into shared helper functions, which greatly improves code clarity and maintainability. Additionally, robust integration tests have been added to verify the new functionality, including support for Apple's @Generable macro. My feedback focuses on a suggestion to further reduce code duplication between the two generateObject methods.

@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 structured output support for FirebaseGenerable, refactoring the existing generateObject method to accommodate this and reduce code duplication. The changes include consolidating JSON response parsing, implementing a more robust JSON cleanup mechanism, and adding comprehensive integration tests to verify compatibility with both FirebaseGenerable and Apple's @Generable macro. Overall, the changes are well-executed and significantly enhance the SDK's capabilities. I have one suggestion to further improve the robustness of the JSON cleaning logic.

@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 and well-executed refactoring to support structured output via FirebaseGenerable. The consolidation of logic into a shared _generateObject method is a great improvement for code clarity and maintainability. The addition of comprehensive integration tests for both FirebaseGenerable and Apple's @Generable is also a valuable contribution. I've identified one high-severity issue that could lead to a runtime crash if a user defines a schema with duplicate property names. My review includes suggestions to address this by adding validation and making the schema generation function throwing.

@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 structured output with FirebaseGenerable, refactors GenerativeModel to consolidate shared logic, and adds comprehensive integration tests. The changes are well-structured and significantly improve the SDK's capabilities. The refactoring of generateObject into a private helper is a good design choice that enhances code reuse and maintainability. The new integration tests are thorough, covering both the new FirebaseGenerable and existing FoundationModels.Generable paths. Overall, this is an excellent contribution. I have one minor suggestion to further improve the new test file's maintainability.

@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 is a great improvement. It refactors the generateObject functionality to support both FoundationModels.Generable and a new FirebaseGenerable protocol, consolidating the implementation into a shared private helper function. The logic for cleaning and parsing JSON responses from the model has been centralized and made more robust. The changes to JSONSchema to correctly build the internal schema representation and remove a potential fatalError are excellent. The addition of a comprehensive suite of integration tests for the new functionality is also a major plus.

I have a couple of minor suggestions for improving code efficiency and clarity, but overall this is a high-quality contribution.

@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 is a well-executed refactoring and feature addition. It successfully consolidates the logic for generateObject to support both FoundationModels.Generable and the new FirebaseGenerable, significantly reducing code duplication. The introduction of helper functions for cleaning and parsing JSON responses improves robustness and maintainability. The addition of comprehensive integration tests, including for Apple's @Generable macro compatibility, is a great enhancement. The code is clean, well-structured, and the documentation updates are clear. I have one minor suggestion to improve code conciseness in the new test file.

@paulb777
Copy link
Member Author

/gemini summarize

@gemini-code-assist
Copy link
Contributor

Summary of Changes

This pull request significantly enhances the Firebase AI SDK by introducing structured output capabilities for generative models. It allows developers to define Swift types that the model can populate directly, improving type safety and simplifying data handling. The changes involve refactoring core generation logic, consolidating JSON processing, and strengthening JSON schema validation, all backed by new integration tests to ensure reliability and compatibility with existing frameworks.

Highlights

  • Structured Output for FirebaseGenerable: Refactored GenerativeModel.swift to introduce a new generateObject method that supports structured output for types conforming to FirebaseGenerable, allowing the model to directly populate custom Swift types.
  • Consolidated JSON Parsing and Cleanup: Extracted and consolidated logic for JSON response parsing and cleaning into private helper methods (cleanedJSON, parseModelOutput, _generateObject), improving code maintainability and robustness, especially for stripping markdown blocks from model responses.
  • Enhanced JSONSchema Handling: Improved JSONSchema encoding by introducing makeInternal() for a more robust internal representation and added validation to prevent duplicate property names, ensuring schema integrity.
  • Integration Tests: Added comprehensive integration tests for the generateObject functionality, covering both FirebaseGenerable types and types using Apple's @Generable macro (via FoundationModels), verifying correct structured output and schema adherence.
  • Documentation Updates: Updated documentation for the generateObject method to accurately describe its parameters and return types, enhancing clarity for developers.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • FirebaseAI/Sources/GenerativeModel.swift
    • Refactored generateObject into a private helper _generateObject to centralize common logic.
    • Added a new generateObject overload specifically for FirebaseGenerable types.
    • Introduced private helper methods cleanedJSON, parseModelOutput, and generationConfig for robust JSON processing and configuration.
    • Updated documentation for generateObject parameters and return types.
    • Added an init(jsonValue: JSONValue) extension to ModelOutput for structured object initialization.
  • FirebaseAI/Sources/Types/Public/Generable/JSONSchema.swift
    • Added asGeminiJSONSchema() method to convert JSONSchema to JSONObject.
    • Implemented makeInternal() and JSONSchema.Kind.makeInternal() for internal schema representation and validation, including checks for duplicate property names.
    • Modified encode(to:) to use the new makeInternal() method for more reliable encoding.
  • FirebaseAI/Tests/TestApp/Tests/Integration/GenerateObjectIntegrationTests.swift
    • Added a new file containing integration tests for the generateObject functionality.
    • Tests cover various scenarios for FirebaseGenerable types (e.g., Recipe) and @Generable types from FoundationModels (e.g., MacroRecipe, ComplexGuidedObject, IntegrationSearchSuggestions, GuidedOldCat).
Activity
  • The author, paulb777, made multiple requests for /gemini review and /gemini summarize, indicating active engagement with automated feedback.
  • gemini-code-assist[bot] provided feedback on improving JSON cleaning by handling various markdown block prefixes and suffixes more robustly.
  • gemini-code-assist[bot] suggested extracting duplicated generationConfig logic into a private helper function for better maintainability.
  • gemini-code-assist[bot] recommended consolidating the common generateObject logic into a private _generateObject helper to reduce code duplication.
  • gemini-code-assist[bot] identified a high-priority issue regarding unsafe Dictionary initialization in JSONSchema's makeInternal() and provided a solution to validate for duplicate property names and propagate errors.
  • gemini-code-assist[bot] suggested minor improvements to documentation clarity, test consistency, and code conciseness (e.g., in-place string modifications, functional programming constructs, shared test constants).

Comment on lines 35 to 39
let kind: Kind?
let source: String?
let schema: JSONSchema.Internal?

init(kind: Kind, source: String) {
Copy link
Member

Choose a reason for hiding this comment

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

Question (non-blocking), why are these fields optional? Could a explanation via code comment be added? The initializer takes in non-optionals, and defaults the constant schema to be nil. There is property unwrapping elsewhere in the implementation that I was wondering if could be avoided.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressing in #15656

Copy link
Contributor

Choose a reason for hiding this comment

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

The JSONSchema.Internal approach was entirely a workaround because I didn't have time to implement the rest before vacation but it looks like we're going with it now?

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