Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(google): keep image resolution when aspect ratio is auto (#16677)

Lee Junho4b6cc25cd509
diff --git a/packages/model-runtime/src/providers/google/index.test.ts b/packages/model-runtime/src/providers/google/index.test.ts
index 67d63811236..53270bae2d8 100644
--- a/packages/model-runtime/src/providers/google/index.test.ts
+++ b/packages/model-runtime/src/providers/google/index.test.ts
@@ -1141,6 +1141,111 @@ describe('buildGoogleToolsWithSearch', () => {
expect(config.toolConfig).toBeUndefined();
});
+ it('should keep image resolution in imageConfig when aspect ratio is auto', async () => {
+ const mockStream = new ReadableStream({
+ start(controller) {
+ controller.enqueue({
+ text: 'test',
+ candidates: [
+ {
+ content: { parts: [{ text: 'test' }], role: 'model' },
+ finishReason: 'STOP',
+ index: 0,
+ },
+ ],
+ usageMetadata: { promptTokenCount: 1, totalTokenCount: 2 },
+ modelVersion: 'gemini-3.5-pro-image-preview',
+ });
+ controller.close();
+ },
+ });
+ vi.spyOn(instance['client'].models, 'generateContentStream').mockResolvedValue(
+ mockStream as any,
+ );
+
+ await instance.chat({
+ imageAspectRatio: 'auto',
+ imageResolution: '4K',
+ messages: [{ content: 'Hello', role: 'user' }],
+ model: 'gemini-3.5-pro-image-preview',
+ temperature: 1,
+ });
+
+ const callArgs = (instance['client'].models.generateContentStream as any).mock.calls[0];
+ const config = callArgs[0].config as any;
+ expect(config.imageConfig).toEqual({ imageSize: '4K' });
+ });
+
+ it('should omit imageConfig when aspect ratio is auto and no resolution is set', async () => {
+ const mockStream = new ReadableStream({
+ start(controller) {
+ controller.enqueue({
+ text: 'test',
+ candidates: [
+ {
+ content: { parts: [{ text: 'test' }], role: 'model' },
+ finishReason: 'STOP',
+ index: 0,
+ },
+ ],
+ usageMetadata: { promptTokenCount: 1, totalTokenCount: 2 },
+ modelVersion: 'gemini-3.5-pro-image-preview',
+ });
+ controller.close();
+ },
+ });
+ vi.spyOn(instance['client'].models, 'generateContentStream').mockResolvedValue(
+ mockStream as any,
+ );
+
+ await instance.chat({
+ imageAspectRatio: 'auto',
+ messages: [{ content: 'Hello', role: 'user' }],
+ model: 'gemini-3.5-pro-image-preview',
+ temperature: 1,
+ });
+
+ const callArgs = (instance['client'].models.generateContentStream as any).mock.calls[0];
+ const config = callArgs[0].config as any;
+ expect(config.imageConfig).toBeUndefined();
+ });
+
+ it('should not build imageConfig for non-image-response models', async () => {
+ const mockStream = new ReadableStream({
+ start(controller) {
+ controller.enqueue({
+ text: 'test',
+ candidates: [
+ {
+ content: { parts: [{ text: 'test' }], role: 'model' },
+ finishReason: 'STOP',
+ index: 0,
+ },
+ ],
+ usageMetadata: { promptTokenCount: 1, totalTokenCount: 2 },
+ modelVersion: 'gemini-2.0-flash',
+ });
+ controller.close();
+ },
+ });
+ vi.spyOn(instance['client'].models, 'generateContentStream').mockResolvedValue(
+ mockStream as any,
+ );
+
+ await instance.chat({
+ imageAspectRatio: '16:9',
+ imageResolution: '2K',
+ messages: [{ content: 'Hello', role: 'user' }],
+ model: 'gemini-2.0-flash',
+ temperature: 1,
+ });
+
+ const callArgs = (instance['client'].models.generateContentStream as any).mock.calls[0];
+ const config = callArgs[0].config as any;
+ expect(config.imageConfig).toBeUndefined();
+ expect(config.responseModalities).toBeUndefined();
+ });
+
it('should not set includeServerSideToolInvocations for Vertex AI', async () => {
const vertexInstance = new LobeGoogleAI({ apiKey: 'test', isVertexAi: true });
const mockStream = new ReadableStream({
diff --git a/packages/model-runtime/src/providers/google/index.ts b/packages/model-runtime/src/providers/google/index.ts
index ad2c69ef870..98e063570ab 100644
--- a/packages/model-runtime/src/providers/google/index.ts
+++ b/packages/model-runtime/src/providers/google/index.ts
@@ -180,15 +180,19 @@ export class LobeGoogleAI implements LobeRuntimeAI {
}
const tools = this.buildGoogleToolsWithSearch(payload.tools, payload);
+ const imageConfig: NonNullable<GenerateContentConfig['imageConfig']> = {};
+ if (isImageResponseModel) {
+ if (imageAspectRatio && imageAspectRatio !== 'auto') {
+ imageConfig.aspectRatio = imageAspectRatio;
+ }
+ if (imageResolution) {
+ imageConfig.imageSize = imageResolution;
+ }
+ }
+
const config: GenerateContentConfig = {
abortSignal: originalSignal,
- imageConfig:
- isImageResponseModel && imageAspectRatio && imageAspectRatio !== 'auto'
- ? {
- aspectRatio: imageAspectRatio,
- imageSize: imageResolution,
- }
- : undefined,
+ imageConfig: Object.keys(imageConfig).length > 0 ? imageConfig : undefined,
maxOutputTokens: payload.max_tokens,
responseModalities: isImageResponseModel ? ['Text', 'Image'] : undefined,
// avoid wide sensitive words