Skip to content

✨ feat: add auto aspect ratio and image search support for Nano Banana 2#12537

Merged
arvinxx merged 9 commits into
lobehub:canaryfrom
sxjeru:22888
Mar 3, 2026
Merged

✨ feat: add auto aspect ratio and image search support for Nano Banana 2#12537
arvinxx merged 9 commits into
lobehub:canaryfrom
sxjeru:22888

Conversation

@sxjeru

@sxjeru sxjeru commented Feb 28, 2026

Copy link
Copy Markdown
Contributor

💻 Change Type

  • ✨ feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 👷 build
  • ⚡️ perf
  • ✅ test
  • 📝 docs
  • 🔨 chore

🔗 Related Issue

🔀 Description of Change

  1. 宽高比添加 auto 项,以留空参数生成和提供图片尺寸匹配的图。

  2. 尝试支持图片搜索。(3.1 Flash 引入)

https://ai.google.dev/gemini-api/docs/image-generation#image-search

确认可用。预览图在下面。

🧪 How to Test

  • Tested locally
  • Added/updated tests
  • No tests needed

📸 Screenshots / Videos

Before After
... ...

📝 Additional Information

Summary by Sourcery

Support auto image aspect ratio for Nano Banana image models and extend Google Gemini/Vertex search integration with image search results.

New Features:

  • Allow Nano Banana image models to use an auto aspect ratio option in both backend parameters and UI controls.
  • Propagate Google image search results, including image metadata and queries, through grounding metadata for Gemini and Vertex AI streams.
  • Enable Google search tools to request image search alongside web search for supported Gemini image-preview models.
  • Treat image-specific aspect ratio configuration as optional when auto is selected for Gemini image generation APIs.

Enhancements:

  • Extend GroundingSearch types and validation schemas to include image result items and image search query metadata.
  • Refine Google streaming transformers to separate web citations from image results and omit empty citation/image arrays.
  • Bump @google/genai dependency to a newer version to support updated image and search capabilities.

Tests:

  • Add unit tests for handling image search grounding metadata in Google and Vertex AI stream transformers, including image-only and mixed web/image cases.
  • Add tests ensuring Google provider builds search tools with imageSearch enabled for supported models and plain web search for others.
  • Add tests verifying imageConfig is skipped when aspectRatio is set to auto in Google image creation.

Copilot AI review requested due to automatic review settings February 28, 2026 15:13
@vercel

vercel Bot commented Feb 28, 2026

Copy link
Copy Markdown

@sxjeru is attempting to deploy a commit to the LobeHub OSS Team on Vercel.

A member of the Team first needs to authorize it.

@sourcery-ai

sourcery-ai Bot commented Feb 28, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds support for auto aspect ratio for Nano Banana image models and integrates Google image search results into grounding metadata and search tools, updating Google/Vertex streaming transforms, search tooling, and tests accordingly.

Sequence diagram for GoogleGenerativeAIStream grounding with image search

sequenceDiagram
  participant GoogleAPIStream
  participant GoogleGenerativeAIStream
  participant transformGoogleGenerativeAIStream
  participant SSEClient

  GoogleAPIStream->>GoogleGenerativeAIStream: ReadableStream<Chunk>
  loop for each chunk
    GoogleGenerativeAIStream->>transformGoogleGenerativeAIStream: candidate.groundingMetadata
    activate transformGoogleGenerativeAIStream
    transformGoogleGenerativeAIStream->>transformGoogleGenerativeAIStream: Extract groundingChunks, webSearchQueries, imageSearchQueries
    transformGoogleGenerativeAIStream->>transformGoogleGenerativeAIStream: Split webChunks = filter(web)
    transformGoogleGenerativeAIStream->>transformGoogleGenerativeAIStream: Split imageChunks = filter(image)
    alt has webChunks
      transformGoogleGenerativeAIStream->>SSEClient: event grounding
      Note over transformGoogleGenerativeAIStream,SSEClient: data.citations = map(webChunks)
    end
    alt has imageChunks
      transformGoogleGenerativeAIStream->>SSEClient: event grounding
      Note over transformGoogleGenerativeAIStream,SSEClient: data.imageResults = map(imageChunks)
    end
    alt has imageSearchQueries
      transformGoogleGenerativeAIStream->>SSEClient: event grounding
      Note over transformGoogleGenerativeAIStream,SSEClient: data.imageSearchQueries
    end
    transformGoogleGenerativeAIStream-->>GoogleGenerativeAIStream: text + grounding events
    deactivate transformGoogleGenerativeAIStream
  end
  GoogleGenerativeAIStream-->>SSEClient: usage + stop events
Loading

Sequence diagram for VertexAIStream grounding with image search

sequenceDiagram
  participant VertexAPIStream
  participant VertexAIStream
  participant transformVertexAIStream
  participant SSEClient

  VertexAPIStream->>VertexAIStream: ReadableStream<Chunk>
  loop for each chunk
    VertexAIStream->>transformVertexAIStream: candidate.groundingMetadata
    activate transformVertexAIStream
    transformVertexAIStream->>transformVertexAIStream: Extract groundingChunks, webSearchQueries, imageSearchQueries
    transformVertexAIStream->>transformVertexAIStream: Split webChunks = filter(web)
    transformVertexAIStream->>transformVertexAIStream: Split imageChunks = filter(image)
    alt has webChunks
      transformVertexAIStream->>SSEClient: event grounding with citations
    end
    alt has imageChunks
      transformVertexAIStream->>SSEClient: event grounding with imageResults
    end
    alt has imageSearchQueries
      transformVertexAIStream->>SSEClient: event grounding with imageSearchQueries
    end
    transformVertexAIStream-->>VertexAIStream: text + grounding events
    deactivate transformVertexAIStream
  end
  VertexAIStream-->>SSEClient: usage + stop events
Loading

Class diagram for updated GroundingSearch and image citation types

classDiagram
  class CitationItem {
    +string favicon
    +string title
    +string url
  }

  class ImageCitationItem {
    +string domain
    +string imageUri
    +string sourceUri
    +string title
  }

  class GroundingSearch {
    +CitationItem[] citations
    +ImageCitationItem[] imageResults
    +string[] imageSearchQueries
    +string[] searchQueries
  }

  class GoogleGenerativeAIStream {
    +ReadableStream rawStream
    +GoogleGenerativeAIStream(rawStream)
  }

  class VertexAIStream {
    +ReadableStream rawStream
    +VertexAIStream(rawStream)
  }

  class LobeGoogleAI {
    +Set modelsWithModalities
    +Set modelsWithImageSearch
    +chat(payload)
    +buildGoogleToolsWithSearch(tools)
  }

  GroundingSearch "0..*" --> CitationItem : uses
  GroundingSearch "0..*" --> ImageCitationItem : uses
  GoogleGenerativeAIStream --> GroundingSearch : emits_grounding_events
  VertexAIStream --> GroundingSearch : emits_grounding_events
  LobeGoogleAI --> GroundingSearch : parses_grounding_metadata
Loading

Flow diagram for building Google search tools with optional image search

flowchart TD
  A_start[Start buildGoogleToolsWithSearch] --> B_hasSearch{hasSearch?}
  B_hasSearch -- No --> Z_returnTools[Return function/urlContext tools only]
  B_hasSearch -- Yes --> C_hasUrlContext{hasUrlContext?}
  C_hasUrlContext -- Yes --> D_modelSupportsImageSearch{model in modelsWithImageSearch?}
  C_hasUrlContext -- No --> G_modelSupportsImageSearch2{model in modelsWithImageSearch?}

  D_modelSupportsImageSearch -- Yes --> E_toolsUrlAndImageSearch["tools = [{ urlContext: {} }, { googleSearch: { searchTypes: { imageSearch: {}, webSearch: {} } } }]"]
  D_modelSupportsImageSearch -- No --> F_toolsUrlAndWebSearch["tools = [{ urlContext: {} }, { googleSearch: {} }]"]

  G_modelSupportsImageSearch2 -- Yes --> H_toolsImageSearchOnly["tools = [{ googleSearch: { searchTypes: { imageSearch: {}, webSearch: {} } } }]"]
  G_modelSupportsImageSearch2 -- No --> I_toolsWebSearchOnly["tools = [{ googleSearch: {} }]"]

  E_toolsUrlAndImageSearch --> J_done[Return tools]
  F_toolsUrlAndWebSearch --> J_done
  H_toolsImageSearchOnly --> J_done
  I_toolsWebSearchOnly --> J_done
  Z_returnTools --> J_done
Loading

File-Level Changes

Change Details Files
Emit structured image search results from Google and Vertex streams in grounding events, alongside existing web citations.
  • Extend GroundingSearch and its Zod schema to support imageResults and imageSearchQueries fields.
  • Update GoogleGenerativeAIStream and VertexAIStream transforms to split groundingChunks into web and image chunks, building citations only from web chunks and imageResults only from image chunks.
  • Include imageSearchQueries when present, omit citations/imageResults/imageSearchQueries when corresponding data is absent, and keep searchQueries mapped from webSearchQueries.
  • Add tests for GoogleGenerativeAIStream and VertexAIStream to verify grounding payloads for mixed web+image results and image-only results and to ensure usage events remain unchanged.
packages/types/src/search.ts
packages/model-runtime/src/core/streams/google/index.ts
packages/model-runtime/src/core/streams/google/google-ai.test.ts
packages/model-runtime/src/core/streams/vertex-ai.ts
packages/model-runtime/src/core/streams/vertex-ai.test.ts
Add Google image search tooling support for image-capable Gemini models and ensure correct searchTypes are passed when search is enabled.
  • Introduce modelsWithImageSearch set and use it when building googleSearch tools so that imageSearch+webSearch searchTypes are sent only for supported models.
  • Adjust buildGoogleToolsWithSearch logic to construct a shared googleSearch tool config and reuse it across combinations with urlContext.
  • Add tests to verify that models in modelsWithImageSearch send searchTypes.imageSearch and searchTypes.webSearch, while other models send plain googleSearch without searchTypes.
packages/model-runtime/src/providers/google/index.ts
packages/model-runtime/src/providers/google/index.test.ts
Support auto aspect ratio for Nano Banana image models and avoid sending imageConfig when aspectRatio is auto.
  • Add 'auto' to Nano Banana aspect ratio enums in both model-bank and app UI, and change default aspectRatio from '1:1' to 'auto' for Nano Banana, Nano Banana Pro, and Nano Banana 2 models.
  • Update image aspect ratio select components to include 'auto' in the selectable ratios and use 'auto' as the default value.
  • Guard imageConfig construction in LobeGoogleAI and createGoogleImage so that imageConfig is only included when aspectRatio is provided and not equal to 'auto'.
  • Add a test ensuring createGoogleImage does not include imageConfig when aspectRatio is 'auto' and update usage of GenerateContentConfig accordingly.
packages/model-bank/src/aiModels/google.ts
packages/model-bank/src/aiModels/lobehub/utils.ts
src/features/ModelSwitchPanel/components/ControlsForm/ImageAspectRatioSelect.tsx
src/features/ModelSwitchPanel/components/ControlsForm/ImageAspectRatio2Select.tsx
packages/model-runtime/src/providers/google/index.ts
packages/model-runtime/src/providers/google/createImage.ts
packages/model-runtime/src/providers/google/createImage.test.ts
Miscellaneous cleanups and dependency update in model-runtime.
  • Use object shorthand for enableStreaming in createTokenSpeedCalculator options for both GoogleGenerativeAIStream and VertexAIStream.
  • Bump @google/genai dependency from ^1.29.0 to ^1.43.0 in model-runtime package.json to support newer features, including image search.
packages/model-runtime/src/core/streams/google/index.ts
packages/model-runtime/src/core/streams/vertex-ai.ts
packages/model-runtime/package.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai 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.

Hey - I've left some high level feedback:

  • The grounding metadata mapping logic for Google and Vertex streams is now effectively duplicated (splitting web vs image chunks and building GroundingSearch), which might be worth extracting into a shared helper to ensure behavior stays in sync across providers.
  • The NANO_BANANA_ASPECT_RATIOS/NANO_BANANA_2_ASPECT_RATIOS definitions (including the new auto option and defaults) are now maintained in multiple places (model-bank and UI controls); consider centralizing these constants to reduce the risk of future divergence between runtime and UI.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The grounding metadata mapping logic for Google and Vertex streams is now effectively duplicated (splitting web vs image chunks and building `GroundingSearch`), which might be worth extracting into a shared helper to ensure behavior stays in sync across providers.
- The `NANO_BANANA_ASPECT_RATIOS`/`NANO_BANANA_2_ASPECT_RATIOS` definitions (including the new `auto` option and defaults) are now maintained in multiple places (`model-bank` and UI controls); consider centralizing these constants to reduce the risk of future divergence between runtime and UI.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov

codecov Bot commented Feb 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.56436% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.50%. Comparing base (466f713) to head (baef0d1).
⚠️ Report is 17 commits behind head on canary.

Additional details and impacted files
@@            Coverage Diff             @@
##           canary   #12537      +/-   ##
==========================================
+ Coverage   73.47%   73.50%   +0.02%     
==========================================
  Files        1389     1392       +3     
  Lines      113570   113772     +202     
  Branches    14691    12904    -1787     
==========================================
+ Hits        83441    83623     +182     
- Misses      30024    30041      +17     
- Partials      105      108       +3     
Flag Coverage Δ
app 66.75% <100.00%> (+0.04%) ⬆️
database 90.90% <ø> (ø)
packages/agent-runtime 88.58% <100.00%> (+<0.01%) ⬆️
packages/context-engine 82.04% <ø> (+<0.01%) ⬆️
packages/conversation-flow 92.37% <100.00%> (+0.02%) ⬆️
packages/file-loaders 87.02% <ø> (ø)
packages/memory-user-memory 66.61% <ø> (ø)
packages/model-bank 99.84% <ø> (ø)
packages/model-runtime 84.86% <78.33%> (+<0.01%) ⬆️
packages/prompts 75.16% <ø> (ø)
packages/python-interpreter 92.90% <ø> (ø)
packages/ssrf-safe-fetch 0.00% <ø> (ø)
packages/utils 90.07% <ø> (ø)
packages/web-crawler 88.81% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Store 65.69% <100.00%> (+<0.01%) ⬆️
Services 48.92% <ø> (+0.04%) ⬆️
Server 68.48% <100.00%> (ø)
Libs 43.86% <ø> (-0.19%) ⬇️
Utils 93.72% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Pull request overview

This PR updates the Nano Banana (Gemini image-capable) configuration to support an auto aspect ratio (by omitting the provider aspectRatio parameter) and adds preliminary plumbing for Google image search tooling and image-search grounding parsing.

Changes:

  • Add auto to Nano Banana/Nano Banana 2 aspect-ratio enums and switch defaults to auto in UI + model parameter definitions.
  • In Google image-capable calls, omit imageConfig.aspectRatio when the selected ratio is auto.
  • Add image-search related types/schemas and parse image grounding chunks into imageResults/imageSearchQueries; enable googleSearch.searchTypes.imageSearch for the Nano Banana 2 chat model; bump @google/genai.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/features/ModelSwitchPanel/components/ControlsForm/ImageAspectRatioSelect.tsx Adds auto option and makes it the default for Nano Banana aspect ratio control.
src/features/ModelSwitchPanel/components/ControlsForm/ImageAspectRatio2Select.tsx Adds auto option and makes it the default for Nano Banana 2 aspect ratio control.
packages/types/src/search.ts Extends grounding/search types with image search result structures + zod schemas.
packages/model-runtime/src/providers/google/index.ts Omits imageConfig on auto and enables imageSearch tool config for supported model(s).
packages/model-runtime/src/providers/google/index.test.ts Adds coverage for the new imageSearch tool configuration.
packages/model-runtime/src/providers/google/createImage.ts Omits imageConfig when aspect ratio is auto for image generation.
packages/model-runtime/src/providers/google/createImage.test.ts Tests that auto does not send imageConfig.
packages/model-runtime/src/core/streams/vertex-ai.ts Parses image grounding chunks into imageResults/imageSearchQueries.
packages/model-runtime/src/core/streams/vertex-ai.test.ts Adds tests for image grounding parsing in VertexAI stream.
packages/model-runtime/src/core/streams/google/index.ts Parses image grounding chunks into imageResults/imageSearchQueries.
packages/model-runtime/src/core/streams/google/google-ai.test.ts Adds tests for image grounding parsing in Google stream.
packages/model-runtime/package.json Bumps @google/genai dependency version.
packages/model-bank/src/aiModels/lobehub/utils.ts Adds auto to Nano Banana aspect ratios and sets default to auto.
packages/model-bank/src/aiModels/google.ts Adds auto to Nano Banana aspect ratios and sets default to auto for Google model cards.
Comments suppressed due to low confidence (1)

packages/model-runtime/src/providers/google/index.ts:179

  • The chat path now treats imageAspectRatio === 'auto' as “omit imageConfig”. There’s test coverage for tool building in index.test.ts, but no test asserting the generated GenerateContentConfig.imageConfig behavior for 'auto' vs a concrete ratio in LobeGoogleAI.chat. Adding a unit test here would help prevent regressions in how aspect ratio is forwarded to Gemini.
      const config: GenerateContentConfig = {
        abortSignal: originalSignal,
        imageConfig:
          modelsWithModalities.has(model) && imageAspectRatio && imageAspectRatio !== 'auto'
            ? {
                aspectRatio: imageAspectRatio,
                imageSize: imageResolution,
              }
            : undefined,

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/model-runtime/src/core/streams/vertex-ai.ts Outdated
Comment thread packages/model-runtime/src/providers/google/index.ts
Comment thread packages/model-runtime/src/core/streams/google/index.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a545ff646a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/model-runtime/src/core/streams/google/index.ts Outdated
@sxjeru

sxjeru commented Mar 1, 2026

Copy link
Copy Markdown
Contributor Author

Further improvement to make website logos display correctly.

image

Make tool call (Google search) token consumption reflected.

image

This comment was translated by Claude.

Original Content

进一步改进,使网站 logo 正常显示。

image

使工具调用(google 搜索)token 消耗能被体现。

image

@sxjeru

sxjeru commented Mar 2, 2026

Copy link
Copy Markdown
Contributor Author

Image search results can now be displayed.

image

This comment was translated by Claude.

Original Content

搜图结果已可显示。

image

@sxjeru

sxjeru commented Mar 2, 2026

Copy link
Copy Markdown
Contributor Author

Implement parsing of search result references in the body text, such as image_0.png, etc.

image

This comment was translated by Claude.

Original Content

实现解析正文中的如 image_0.png 等搜索结果引用。

image

@vercel

vercel Bot commented Mar 3, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lobehub Ready Ready Preview, Comment Mar 3, 2026 7:28am

Request Review

@arvinxx arvinxx merged commit 1c1af17 into lobehub:canary Mar 3, 2026
22 checks passed
@lobehubbot

Copy link
Copy Markdown
Member

❤️ Great PR @sxjeru ❤️

The growth of project is inseparable from user feedback and contribution, thanks for your contribution! If you are interesting with the lobehub developer community, please join our discord and then dm @arvinxx or @canisminor1990. They will invite you to our private developer channel. We are talking about the lobe-chat development or sharing ai newsletter around the world.

@sxjeru sxjeru deleted the 22888 branch March 3, 2026 11:32
arvinxx pushed a commit that referenced this pull request Mar 4, 2026
…a 2 (#12537)

* Update sync.yml

* ✨ feat: update aspect ratio defaults to 'auto' for image generation models

* ✨ feat: enhance grounding metadata handling with image search results support

* ✨ feat: filter empty strings from searchQueries in groundingMetadata and update favicon handling in SearchGrounding component

* ✨ feat: add inputToolTokens tracking and update related components for tool usage

* ✨ feat: enhance search grounding with image results and update related components

* ✨ feat: add ImageSearchRef component and related tests for image reference handling

* fix test: rename VertexAIStream to GoogleGenerativeAIStream for consistency in test cases

* Update sync.yml
MarioJames pushed a commit to MarioJames/lobehub that referenced this pull request Mar 9, 2026
…a 2 (lobehub#12537)

* Update sync.yml

* ✨ feat: update aspect ratio defaults to 'auto' for image generation models

* ✨ feat: enhance grounding metadata handling with image search results support

* ✨ feat: filter empty strings from searchQueries in groundingMetadata and update favicon handling in SearchGrounding component

* ✨ feat: add inputToolTokens tracking and update related components for tool usage

* ✨ feat: enhance search grounding with image results and update related components

* ✨ feat: add ImageSearchRef component and related tests for image reference handling

* fix test: rename VertexAIStream to GoogleGenerativeAIStream for consistency in test cases

* Update sync.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants