feat(ai): add GeminiLanguageModel guided generation support - #16619
feat(ai): add GeminiLanguageModel guided generation support#16619andrewheard wants to merge 2 commits into
Conversation
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. |
Summary of ChangesThis pull request enables guided generation (structured output) support for the GeminiLanguageModel. By translating Foundation Models schemas into Gemini-compatible JSON schemas and handling property ordering requirements, it allows developers to request structured responses from Gemini models. The changes include new translation logic, capability declaration, and robust testing to ensure reliability across various schema types. Highlights
New Features🧠 You can now enable Memory (public preview) 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. Activity
|
ce85664 to
e42323d
Compare
e42323d to
3b827ec
Compare
3b827ec to
13d277d
Compare
0afb7eb to
819c4a1
Compare
Add guided generation (structured output) support to GeminiLanguageModel conforming to Apple's LanguageModel. * Extend GenerationSchema to translate schemas into Gemini-compatible JSON Schema definitions, remapping x-order to propertyOrdering. * Detect unsupported regex pattern guides during encoding and throw LanguageModelError.unsupportedGenerationGuide. * Introduce GeminiRequestTranslator to map generation requests and schemas into GenerateContentRequest and GenerationConfig. * Delegate request translation in GeminiLanguageModelExecutor to GeminiRequestTranslator. * Declare the .guidedGeneration capability on GeminiLanguageModel. * Add comprehensive unit tests covering property ordering, nested definitions, enums, recursive schemas, and error throwing. * Add parameterized integration tests for single-turn and streaming structured output generation, enum classification, and recursive hierarchies.
Add an integration test to FirebaseAITestApp verifying structured content generation using GeminiLanguageModel and FoundationModels. Define a `@Generable` `CatProfile` struct with `@Guide` constraints, and validate that `session.respond(..., generating:)` returns strongly typed output adhering to the schema and range requirements across configured Firebase AI instances.
819c4a1 to
bf557f1
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request adds support for guided generation (structured outputs) in the Gemini Language Model by translating requests and converting schemas to a Gemini-compatible JSON format, including mapping property ordering and detecting unsupported regex patterns. It also adds comprehensive unit and integration tests. The feedback suggests a critical improvement in the schema translation logic to prevent false positives when a user defines a property named 'pattern' by verifying the coding path.
| if lastKey.stringValue == "pattern" { | ||
| hasPatternGuide.withLock { $0 = true } | ||
| } |
There was a problem hiding this comment.
To prevent false positives when a user defines a property named pattern in their model, we should verify the coding path. In JSON Schema, a property named pattern will always have "properties" as its parent key (the second-to-last key in the coding path). A regex pattern guide, however, will be a direct child of the property's schema itself. We can check keys.dropLast().last?.stringValue to distinguish them.
| if lastKey.stringValue == "pattern" { | |
| hasPatternGuide.withLock { $0 = true } | |
| } | |
| if lastKey.stringValue == "pattern" { | |
| let isPropertyNamedPattern = keys.dropLast().last?.stringValue == "properties" | |
| if !isPropertyNamedPattern { | |
| hasPatternGuide.withLock { $0 = true } | |
| } | |
| } |
Added guided generation (structured output) support to
GeminiLanguageModelconforming to Apple'sLanguageModel.GenerationSchemato translate schemas into Gemini-compatible JSON Schema definitions, remappingx-ordertopropertyOrdering.patternguides during encoding and throwsLanguageModelError.unsupportedGenerationGuidesince these are not supported by Gemini.GeminiRequestTranslatorto map generation requests and schemas intoGenerateContentRequestandGenerationConfig.GenerationConfigsettings, liketemperature, are not yet implemented. These are lower priority since most are deprecated for Gemini 3 models..guidedGenerationcapability onGeminiLanguageModel.#no-changelog