[AI] support on-device structured output - #8395
Conversation
📝 PRs merging into main branchOur main branch should always be in a releasable state. If you are working on a larger change, or if you don't want this change to see the light of the day just yet, consider using a feature branch first, and only merge into the main branch when the code complete and ready to be released. |
Generated by 🚫 Danger |
Using Gemini Code AssistThe 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
Customization To customize the 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 Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. 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. |
|
The public api surface has changed for the subproject ai-logic_firebase-ai-ondevice-interop: Please update the api.txt files for the subprojects being affected by this change by running ./gradlew ${subproject}:generateApiTxtFile. Also perform a major/minor bump accordingly. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for structured object generation (generateObject) in the on-device model interop and implementation layers, including updates to the KSP processor to generate ML Kit companion classes and support enum values in the @Guide annotation. Feedback on these changes highlights a type-safety issue in GenerateObjectResponse.kt regarding MutableList invariance, as well as an opportunity to replace unsafe non-null assertions (!!) with idiomatic safe calls in SchemaSymbolProcessorVisitor.kt.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for structured object generation on-device by integrating with ML Kit's schema-based generation. It updates the KSP processor to generate companion classes (_MlKitCompanion) for @Generable classes, adds the generateObject API to the on-device interop and implementation layers, and updates GenerateObjectResponse to support lazy-loading and caching of deserialized instances. The review feedback highlights several important improvements: handling nested classes correctly when resolving companion class names, refining the caching logic in GenerateObjectResponse to prevent redundant deserialization of null values, safely escaping enum values using KotlinPoet's %S specifier, and propagating KDoc-based class and property descriptions to the generated companion annotations.
| assertThat(mlKitRequest.topK).isEqualTo(3) | ||
| assertThat(mlKitRequest.seed).isEqualTo(0) | ||
| assertThat(mlKitRequest.candidateCount).isEqualTo(1) | ||
| assertThat(mlKitRequest.maxOutputTokens).isEqualTo(256) |
There was a problem hiding this comment.
this is failing after the mlkit version update
There was a problem hiding this comment.
what are the new values? are they similar to the values here?
There was a problem hiding this comment.
maxOutputTokens is different, others are all the same. But the default value checks are completely removed as it could change in later versions and diesn't make sense to verify numbers that we did not set (default)
emilypgoogle
left a comment
There was a problem hiding this comment.
Gave a quick pass and left some comments
|
|
||
| private fun isListOfGenerableClass(type: KSType): Boolean { | ||
| val qualifiedName = type.declaration.qualifiedName?.asString() | ||
| if (qualifiedName == "kotlin.collections.List" || qualifiedName == "java.util.List") { |
There was a problem hiding this comment.
Is it prudent to analyze hierarchy here? I can imagine weird issues if you wanted to use an immutable list or ArrayList or otherwise.
There was a problem hiding this comment.
We can't safely rely on full hierarchy analysis here because we need to know exactly how to reconstruct the collection when generating the toSdk() map the MLkit response back to developer code. If a developer provided a custom list implementation and we just allowed it through, our KSP processor would generate invalid mapping code that fails to compile on their end.
However, I completely agree that we should support the common variants. I've just updated the KSP processor to explicitly support MutableList and ArrayList properties in @generable classes, along with the correct construction logic to convert them safely in the generated toSdk() method.
|
|
||
| // 3. Save to instances list for future accesses (lazy loading) and return | ||
| val currentInstances = | ||
| insts ?: MutableList<T?>(response.candidates.size) { null }.also { instances = it } |
There was a problem hiding this comment.
not sure what u meant by +1.
…b.com/firebase/firebase-android-sdk into mila-support-structured-ouput-locally
This PR introduces Structured Output support on device, allowing developers to generate strongly-typed objects using ML Kit's backend. It defines the public API (GenerateObjectResponse and generateObject()), handles the necessary Kotlin Symbol Processing (KSP) logic for schema translation.
How to use On-Device Structured Output