Skip to content

concatenate multple text parts for gemini models#1466

Merged
VisargD merged 1 commit into
Portkey-AI:mainfrom
narengogi:fix/concatenate-multiple-text-parts
Dec 15, 2025
Merged

concatenate multple text parts for gemini models#1466
VisargD merged 1 commit into
Portkey-AI:mainfrom
narengogi:fix/concatenate-multiple-text-parts

Conversation

@narengogi

Copy link
Copy Markdown
Member

Description

When using Gemini models (especially gemini-3-pro-image-preview) that return responses with multiple text parts, the content field only contains the last text fragment instead of all text parts concatenated together.

Root Cause

In the response transformation code, when iterating through multiple text parts:

for (const part of generation.content?.parts ?? []) {
  // ...
  else if (part.text) {
    if (part.thought) {
      contentBlocks.push({ type: 'thinking', thinking: part.text });
    } else {
      content = part.text;  // ❌ BUG: OVERWRITES instead of CONCATENATING
      contentBlocks.push({ type: 'text', text: part.text });
    }
  }
}

Example

Response received:

{
  "message": {
    "content": " mostly obscured by the red one and very blurred...",  // ❌ Only last fragment
    "content_blocks": [
      { "type": "thinking", "thinking": "..." },
      { "type": "text", "text": "VERIFIED: A photorealistic rendering shows..." },  // ✅ First text part
      { "type": "text", "text": ", a blue die is on the left..." },  // ✅ Second text part
      { "type": "text", "text": " mostly obscured by the red one..." }  // ❌ Only this ends up in content
    ]
  }
}

Expected content field:

"VERIFIED: A photorealistic rendering shows..., a blue die is on the left..., mostly obscured by the red one..."

Impact

  • System instructions appear to be ignored because the content field doesn't include the first text part which contains the instruction-following prefix
  • The full response is available in content_blocks, but OpenAI-compatible clients expect the complete text in content
  • Affects gemini-3-pro-image-preview, gemini-2.0-flash-thinking-exp, and other models that split responses into multiple text parts

Files Affected

Non-streaming responses:

  • src/providers/google-vertex-ai/chatComplete.ts line 468
  • src/providers/google/chatComplete.ts line 577

Streaming responses:

  • src/providers/google-vertex-ai/chatComplete.ts line 644
  • src/providers/google/chatComplete.ts line 704

Proposed Fix

Change from overwriting to concatenating:

// Current (buggy):
content = part.text;

// Fixed:
content = (content || '') + part.text;

Workaround for Users

Until fixed, users can manually concatenate text blocks:

const allText = message.content_blocks
  .filter(b => b.type === 'text')
  .map(b => b.text)
  .join('');

Requires x-portkey-strict-open-ai-compliance: false to receive content_blocks.

@narengogi narengogi requested review from b4s36t4 and roh26it December 10, 2025 17:49
@VisargD VisargD merged commit af1e6c7 into Portkey-AI:main Dec 15, 2025
1 check passed
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.

3 participants