llamaIndex - ✅(Solved) Fix [Bug]: google_genai.utils [1 pull requests, 3 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
run-llama/llama_index#21137Fetched 2026-04-08 01:26:28
View on GitHub
Comments
3
Participants
3
Timeline
12
Reactions
0
Author
Timeline (top)
commented ×3labeled ×2mentioned ×2subscribed ×2

Fix Action

Fixed

PR fix notes

PR #21141: fix(google-genai): avoid mutating messages list in prepare_chat_params

Description (problem / solution / changelog)

Summary

Fixes #21137

prepare_chat_params used messages.pop(0) to extract the system message, which mutated the caller's original list. When the tenacity retry decorator retried after a transient error (e.g. 429 rate limit or 408 timeout), the system message had already been removed from the list and was silently dropped on subsequent attempts.

Changes

  • Copy the messages list with list(messages) before popping the system message, so the caller's original is never modified.
  • Added a test (test_prepare_chat_params_does_not_mutate_messages) that verifies the original list is preserved after calling prepare_chat_params, and that a simulated retry still correctly extracts the system message.

Test plan

  • Existing test test_prepare_chat_params_with_system_message still passes
  • New test test_prepare_chat_params_does_not_mutate_messages passes
  • Full test suite for llama-index-llms-google-genai passes (22 passed, 42 skipped due to missing API key)

Changed files

  • llama-index-integrations/llms/llama-index-llms-google-genai/llama_index/llms/google_genai/utils.py (modified, +3/-0)
  • llama-index-integrations/llms/llama-index-llms-google-genai/pyproject.toml (modified, +1/-1)
  • llama-index-integrations/llms/llama-index-llms-google-genai/tests/test_llms_google_genai.py (modified, +44/-0)

Code Example

The retry API calls on mutated original request
RAW_BUFFERClick to expand / collapse

Bug Description

The messages object is mutated when the system prompt is removed, and therefore this breaks the retry logic in the API call
Bug

Version

current version

Steps to Reproduce

Any call to gemini API with system prompt and retry

Relevant Logs/Tracbacks

The retry API calls on mutated original request

extent analysis

Fix Plan

To fix the issue, we need to ensure that the original request is not mutated when the system prompt is removed. We can achieve this by creating a copy of the original request before modifying it.

Code Changes

Here are the steps to fix the issue:

  • Create a copy of the original request using the copy() method or the deepcopy() function from the copy module.
  • Modify the copied request instead of the original request.
  • Use the copied request for the API call and retry logic.

Example code snippet:

import copy

# Create a copy of the original request
original_request = {'messages': [...]}

# Create a copy of the original request
copied_request = copy.deepcopy(original_request)

# Modify the copied request
copied_request['messages'] = [...]  # Remove system prompt

# Use the copied request for the API call and retry logic

Alternatively, you can use the dict() constructor to create a copy of the original request:

copied_request = dict(original_request)

Verification

To verify that the fix worked, you can add logging statements to check that the original request remains unchanged after modifying the copied request. You can also test the retry logic to ensure that it works as expected.

Extra Tips

  • Always create a copy of the original request when modifying it to avoid unintended side effects.
  • Use the deepcopy() function to create a deep copy of the original request, especially when working with nested data structures.
  • Consider using immutable data structures to avoid issues with mutable requests.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING