Skip to content

fix(gemini): stop MD_JSON mutating the caller messages - #2718

Closed
rupak-eng wants to merge 1 commit into
567-labs:mainfrom
rupak-eng:fix/gemini-md-json-no-mutate-caller
Closed

rupak-eng wants to merge 1 commit into
567-labs:mainfrom
rupak-eng:fix/gemini-md-json-no-mutate-caller

Conversation

@rupak-eng

Copy link
Copy Markdown

Describe your changes

handle_gemini_json injected its JSON schema instruction into the caller-owned messages list and system message dict (appended again on every call when a system message was present; inserted into the caller list otherwise). transform_to_gemini_prompt additionally hoisted the system prompt into the caller own content list via the aliased list returned by get_message_content.

  • handle_gemini_json now works on a copy via the existing copy_messages_for_mutation helper (same helper the OpenAI/Mistral/Cohere/Writer handlers use), and writes the copy back to new_kwargs["messages"].
  • get_message_content always returns a new list instead of aliasing a caller-owned content list.
  • Regression tests in tests/v2/test_messages_not_mutated.py cover all three mutation legs (existing system message, no system message, response_model=None prompt hoisting). Gemini cant join FIXED_PAIRS because it rejects model= in create kwargs, hence a separate test.
  • Updated tests/coverage/test_gemini_coverage.py::test_gemini_handlers_prepare_expected_tool_and_json_requests, which pinned the old caller-mutating behavior, to assert the caller stays untouched and the schema instruction lands in the outgoing contents.
  • CHANGELOG entry under [Unreleased].

Issue ticket number and link

Fixes #2716. Same fix direction as #2545 (closed as stale by its author).

Checklist before requesting a review

  • I have performed a self-review of my code
  • If it is a core feature, I have added thorough tests.
  • If it is a core feature, I have added documentation. (bug fix; behavior restored to the documented non-mutating contract)

Test evidence: tests/v2/test_messages_not_mutated.py 69/69 pass (66 pre-existing + 3 new); test_gemini_handlers_prepare_expected_tool_and_json_requests passes; ruff check + format clean.

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

@rupak-eng approving the code. At cd478b4 this fixes #2716: the repro comes back clean, the list-content case as well, and your three new tests and the rewritten Gemini coverage test fail with the old source.

$ python probe.py     # the #2716 repro plus a list-content user message, CPython 3.13.15
# base e12f8b4
leg 1, second call : 2 instructions
leg 2, caller list : 1 -> 2 | roles now ['system', 'user']
list content, response_model=None: caller unchanged=False, user parts now 2
# head cd478b4
leg 1, second call : 0 instructions
leg 2, caller list : 1 -> 1 | roles now ['user']
list content, response_model=None: caller unchanged=True, user parts now 1

$ python -m pytest tests/v2/test_messages_not_mutated.py tests/coverage/test_gemini_coverage.py -q
# base e12f8b4: 89 passed
# head cd478b4: 92 passed
# head tests, messages.py and gemini/utils.py from e12f8b4: 4 failed, 88 passed
# head without CHANGELOG.md, applied onto main 035c77b: 92 passed
$ python -m pytest tests/v2 tests/coverage -q
# base e12f8b4: 54 failed, 2582 passed     # head cd478b4: 54 failed, 2585 passed
# the 54 are the same tests on both, missing provider SDKs (groq, bedrock, cerebras, fireworks, vertexai, litellm)

blocking: CHANGELOG.md conflicts with the 1.18 entries that #2708 added to main, so GitHub cannot merge it yet. The code applies onto 035c77b as it is. Could you rebase and keep both entries?

@jxnl, this is ready for your review once that rebase is in.

handle_gemini_json injected its schema instruction into the caller's own
messages list and system message dict (re-appended on every call), and
transform_to_gemini_prompt hoisted the system prompt into the caller's own
content list. Work on a copy via copy_messages_for_mutation instead, and
have get_message_content always return a new list.

Fixes 567-labs#2716.
@rupak-eng
rupak-eng force-pushed the fix/gemini-md-json-no-mutate-caller branch from cd478b4 to 3e03154 Compare October 12, 2026 04:33

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

@rupak-eng the rebase onto 035c77b keeps both changelog sections and changes no source line, so the conflict I blocked on is gone. The #2716 repro stays clean, and the four tests still fail on the old messages.py and gemini/utils.py.

# head 3e03154, base main 035c77b, CPython 3.12.3, ruff 0.11.13
$ python ins2716_probe.py
# base: leg 1 2 instructions | leg 2 roles now ['system', 'user'] | list content caller unchanged=False
# head: leg 1 0 instructions | leg 2 roles now ['user']           | list content caller unchanged=True
$ python -m pytest tests/v2/test_messages_not_mutated.py tests/coverage/test_gemini_coverage.py -q
# base 035c77b: 89 passed
# head 3e03154: 92 passed
# head tests, messages.py and gemini/utils.py from 035c77b: 4 failed, 88 passed
$ ruff check <4 changed .py files> && ruff format --check <same>
All checks passed!
4 files already formatted
# not run: tests/v2 and tests/coverage in full at this head

@jxnl #2717 targets the same issue; this one is now conflict-free at 3e03154. I can run the same probe against #2717 if that helps the comparison.

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.

Gemini MD_JSON mutates the caller's messages: the list without a system message, the system dict with one

3 participants