[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.
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.
…b.com/firebase/firebase-android-sdk into mila-support-structured-ouput-locally
| classDeclaration.simpleName.asString() + ".Companion" | ||
| ) | ||
| ) | ||
| .receiver(className.nestedClass("Companion")) |
There was a problem hiding this comment.
@VinayGuthal , this should be the reason.
There was a problem hiding this comment.
why do we require changes to generateFileSpec function?
There was a problem hiding this comment.
To be able to support nested Generable classes. We could have create top level shadow classes for nested classes, but there is a chance that 2 classes could have nested classes with same name, eg:
@Generable
data class User(val name: String) {
@Generable
data class Settings(...)
}
@Generable
data class Account(val id: String) {
@Generable
data class Settings(...)
}
With the original way, the 2 nested Settings could end up with name collison. but with the current changes, they would generate seperate files like User_SettingsGeneratedSchema.kt and Account_SettingsGeneratedSchema.kt.
There was a problem hiding this comment.
This could be an overkill, but we can't rule out the possibility of such cases.
6c82cfd to
fee271c
Compare
| if (generableClasses.isNotEmpty()) { | ||
| val hasMlKitOnClasspath = | ||
| resolver.getClassDeclarationByName( | ||
| resolver.getKSNameFromString("com.google.mlkit.genai.schema.guided.GenerableProvider") |
There was a problem hiding this comment.
is it okay to hard code mlkit package name here? do we have any other type of generated classes?
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