Skip to content

Conversation

@paulb777
Copy link
Member

@paulb777 paulb777 commented Nov 22, 2025

wip

Add func generateObject<T: FirebaseGenerable>(as type: T.Type, from prompt: String) async throws -> T
The new tests pass locally

Summary of Changes

This pull request significantly enhances the Firebase AI SDK by introducing the capability to generate structured Swift objects directly from generative model responses. It achieves this through a new FirebaseGenerable protocol and a Swift macro that automatically creates a schema for Decodable types. This feature streamlines the development workflow for applications requiring structured data output from AI models, reducing boilerplate and improving type safety.

Highlights

  • Structured Object Generation: Introduced a new generateObject function in GenerativeModel that allows generating structured Swift objects directly from the model's response, automating schema generation and JSON decoding.
  • FirebaseGenerable Protocol and Macro: Added a FirebaseGenerable protocol and an associated Swift macro (@FirebaseGenerable) to automatically derive a Schema for Decodable types, simplifying the process of defining the expected output structure for the model.
  • Enhanced Configuration Inheritance: Improved the GenerationConfig to allow for proper inheritance of existing settings when creating a new configuration for object generation, ensuring that parameters like temperature and token limits are respected.
  • Robust Schema Handling for Optional Types: Implemented correct schema generation for optional properties within FirebaseGenerable types, ensuring that the model understands which fields can be null.
  • Comprehensive Error Reporting: Added specific error types (GenerateObjectError) to provide clearer feedback when object generation fails due to issues like missing response text or JSON decoding problems.

@google-oss-bot
Copy link

2 Warnings
⚠️ Did you forget to add a changelog entry? (Add #no-changelog to the PR description to silence this warning.)
⚠️ New public headers were added, did you remember to add them to the umbrella header?

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 a powerful new feature, FirebaseGenerable, which simplifies generating structured Swift objects from the generative model. The use of a macro to automatically derive the schema is a great developer experience improvement. The overall implementation is solid, but I've identified a few critical issues that could lead to unexpected behavior or difficult debugging. My main concerns are:

  • The @FirebaseGenerable macro does not correctly handle optional properties, leading to an incorrect schema.
  • The generateObject method silently discards existing GenerationConfig settings like temperature, which will likely surprise users.
  • The schema derivation for nested types fails silently if a type is not FirebaseGenerable, which can make debugging difficult.
    I've also included a minor suggestion to improve the schema accuracy for Float types. Addressing these points will make this new feature more robust and predictable for developers.

paulb777 and others added 3 commits November 22, 2025 14:30
…erableMacro.swift

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@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 the FirebaseGenerable protocol and a corresponding @FirebaseGenerable macro to simplify the generation of structured objects from the language model. A new generateObject convenience method is added to GenerativeModel for this purpose. The changes include new error types, package dependencies for the macro, and integration tests.

My review has identified a critical bug in the macro implementation that affects integer properties. I've also pointed out a couple of areas where the code could be made more robust against future changes to reduce maintenance overhead, and a suggestion to improve test coverage for the new macro.

@paulb777
Copy link
Member Author

/gemini review

@paulb777
Copy link
Member Author

/gemini summary

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 valuable feature: the ability to generate strongly-typed objects from model prompts using a new FirebaseGenerable protocol and a corresponding @FirebaseGenerable macro. The implementation is well-thought-out, adding a new generateObject convenience method, supporting infrastructure like a copying initializer for GenerationConfig, and providing comprehensive integration tests. My review includes a critical fix for the macro implementation to correctly handle multiple property bindings and filter computed properties, along with a few minor suggestions to improve code style and test robustness. Overall, this is a great addition.

@firebase firebase deleted a comment from gemini-code-assist bot Nov 22, 2025
@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 is a well-structured pull request that introduces a powerful new feature for generating structured objects. The use of a Swift macro to automate schema generation is a great approach that will significantly simplify the developer experience. The code is clear, and the addition of comprehensive integration tests is much appreciated. I have a few suggestions to further improve the code.

@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 is an excellent pull request that introduces a significant and powerful feature: the ability to generate structured Swift objects directly from the model. The implementation is very well done, with a clean, user-friendly API (generateObject and @FirebaseGenerable), robust macro implementation, and thorough testing.

The design choices are solid, such as creating a temporary model instance with an overridden configuration to avoid side effects. The new error types and comprehensive integration tests, especially the one verifying configuration inheritance, contribute to a high-quality addition to the SDK.

I have one minor suggestion to improve the macro's robustness, but overall, this is a fantastic contribution.

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.

2 participants