Skip to content

feat(ai): add GeminiLanguageModel guided generation support - #16619

Open
andrewheard wants to merge 2 commits into
mainfrom
ah/ai-glm-guided-generation
Open

feat(ai): add GeminiLanguageModel guided generation support#16619
andrewheard wants to merge 2 commits into
mainfrom
ah/ai-glm-guided-generation

Conversation

@andrewheard

Copy link
Copy Markdown
Contributor

Added guided generation (structured output) support to GeminiLanguageModel conforming to Apple's LanguageModel.

  • Extended GenerationSchema to translate schemas into Gemini-compatible JSON Schema definitions, remapping x-order to propertyOrdering.
  • Detects regex pattern guides during encoding and throws
    LanguageModelError.unsupportedGenerationGuide since these are not supported by Gemini.
  • Introduced GeminiRequestTranslator to map generation requests and schemas into GenerateContentRequest and GenerationConfig.
    • Note: Other GenerationConfig settings, like temperature, are not yet implemented. These are lower priority since most are deprecated for Gemini 3 models.
  • Declared the .guidedGeneration capability on GeminiLanguageModel.
  • Added 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.

#no-changelog

@gemini-code-assist

Copy link
Copy Markdown
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
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .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 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.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

This 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

  • Guided Generation Support: Implemented structured output support for GeminiLanguageModel by conforming to the LanguageModel protocol.
  • Schema Translation: Added GeminiRequestTranslator to map generation requests and schemas, including translating x-order to propertyOrdering for Gemini compatibility.
  • Error Handling: Introduced detection for unsupported regex pattern guides, throwing LanguageModelError.unsupportedGenerationGuide when encountered.
  • Testing: Added comprehensive unit tests for schema transformations and parameterized integration tests covering single-turn, streaming, and recursive structured outputs.
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
  • The pull request was created by andrewheard.
  • The Gemini Code Assist bot provided instructions on how to use its features.
  • The author requested a summary of the pull request using the /gemini summary command.

@andrewheard
andrewheard force-pushed the ah/ai-glm-guided-generation branch 2 times, most recently from ce85664 to e42323d Compare September 6, 2026 03:38
@andrewheard
andrewheard force-pushed the ah/ai-glm-guided-generation branch from e42323d to 3b827ec Compare September 7, 2026 02:54
@andrewheard
andrewheard force-pushed the ah/ai-glm-guided-generation branch from 3b827ec to 13d277d Compare September 7, 2026 02:56
@andrewheard
andrewheard marked this pull request as ready for review September 7, 2026 05:12
@andrewheard
andrewheard force-pushed the ah/ai-glm-guided-generation branch 2 times, most recently from 0afb7eb to 819c4a1 Compare September 7, 2026 16:50
Base automatically changed from ah/ai-glm-integration to main September 7, 2026 22:28
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.
@andrewheard
andrewheard force-pushed the ah/ai-glm-guided-generation branch from 819c4a1 to bf557f1 Compare September 7, 2026 22:28
@andrewheard

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 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.

Comment on lines +41 to +43
if lastKey.stringValue == "pattern" {
hasPatternGuide.withLock { $0 = true }
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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 }
}
}

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.

1 participant