Repository navigation
Conversation
chrikrah
left a comment
There was a problem hiding this comment.
@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.
cd478b4 to
3e03154
Compare
chrikrah
left a comment
There was a problem hiding this comment.
@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.
Describe your changes
handle_gemini_jsoninjected its JSON schema instruction into the caller-ownedmessageslist and system message dict (appended again on every call when a system message was present; inserted into the caller list otherwise).transform_to_gemini_promptadditionally hoisted the system prompt into the caller own content list via the aliased list returned byget_message_content.handle_gemini_jsonnow works on a copy via the existingcopy_messages_for_mutationhelper (same helper the OpenAI/Mistral/Cohere/Writer handlers use), and writes the copy back tonew_kwargs["messages"].get_message_contentalways returns a new list instead of aliasing a caller-owned content list.tests/v2/test_messages_not_mutated.pycover all three mutation legs (existing system message, no system message,response_model=Noneprompt hoisting). Gemini cant joinFIXED_PAIRSbecause it rejectsmodel=in create kwargs, hence a separate test.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 outgoingcontents.Issue ticket number and link
Fixes #2716. Same fix direction as #2545 (closed as stale by its author).
Checklist before requesting a review
Test evidence:
tests/v2/test_messages_not_mutated.py69/69 pass (66 pre-existing + 3 new);test_gemini_handlers_prepare_expected_tool_and_json_requestspasses; ruff check + format clean.