#2716·instructor

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

Author: chrikrahCreated Sep 28, 2026Updated Sep 29, 2026
  • This is actually a bug report.
  • I am not getting good LLM Results
    • A mutation of objects the caller owns, not output quality.
  • I have tried asking for help in the community on discord or discussions and have not received a response.
    • Read #2417 and #2545 instead, both named below.
  • I have tried searching the documentation and have not found an answer.
    • tests/v2/test_messages_not_mutated.py:87 pins this property for every other provider.

What Model are you using?

  • gpt-3.5-turbo
  • gpt-4-turbo
  • gpt-4
  • Other (please specify)
    • Provider.GEMINI with Mode.MD_JSON. Reproducing needs no model call and no SDK.

Describe the bug

GeminiJSONHandler.prepare_request copies the kwargs dict and not the message dicts inside it. patch.py:231 passes the incoming kwargs unchanged. With a system message first, handle_gemini_json appends the JSON instruction to a dict the caller owns, at gemini/utils.py:436. A second call appends it again. Without a system message, new_kwargs.setdefault("messages", []).insert(...) at :432 inserts into the list the caller passed.

tests/v2/test_messages_not_mutated.py:87 asserts exactly this for Mode.MD_JSON on the OpenAI-compat set, Mistral, Cohere, Writer and xAI. Gemini is the one pair absent from FIXED_PAIRS. The tuple cannot simply be added: that test passes model="test", and Gemini raises ConfigurationError first.

To Reproduce

Measured on e12f8b4, version 1.17.1, Python 3.13.15, pydantic 2.13.5:

python
from copy import deepcopy
from pydantic import BaseModel
from instructor.mode import Mode
from instructor.utils import Provider
import instructor.v2.providers                      # registers every handler
from instructor.v2.core.registry import mode_registry

class Answer(BaseModel):
    name: str

handlers = mode_registry.get_handlers(Provider.GEMINI, Mode.MD_JSON)

system = {"role": "system", "content": "You are terse."}
caller = [system, {"role": "user", "content": "hi"}]
handlers.request_handler(response_model=Answer, kwargs={"messages": caller})
print("leg 1, system dict :", repr(system["content"][:34]))
handlers.request_handler(response_model=Answer, kwargs={"messages": caller})
print("leg 1, second call :", system["content"].count("As a genius expert"), "instructions")

caller2 = [{"role": "user", "content": "hi"}]
snapshot = deepcopy(caller2)
handlers.request_handler(response_model=Answer, kwargs={"messages": caller2})
print("leg 2, caller list :", len(snapshot), "->", len(caller2), "| roles now", [m["role"] for m in caller2])
$ python probe.py
leg 1, system dict : 'You are terse.\n\n\n        As a geni'
leg 1, second call : 2 instructions
leg 2, caller list : 1 -> 2 | roles now ['system', 'user']

The same comparison over every registered pair, against a deepcopy taken before each call:

$ python sweep.py
registered pairs: 69   mutated: 1   clean: 53   raised: 15
  MUTATES CALLER: gemini/markdown_json_mode
  raised: anthropic/parallel_tool_call: TypeError
  raised: anyscale/parallel_tool_call: TypeError
  raised: cerebras/parallel_tool_call: TypeError
  raised: databricks/parallel_tool_call: TypeError
  raised: deepseek/parallel_tool_call: TypeError
  raised: gemini/tool_call: AttributeError
  raised: genai/json_mode: ModuleNotFoundError
  raised: genai/tool_call: ModuleNotFoundError
  raised: openai/parallel_tool_call: TypeError
  raised: openrouter/parallel_tool_call: TypeError
  raised: together/parallel_tool_call: TypeError
  raised: vertexai/markdown_json_mode: ImportError
  raised: vertexai/parallel_tool_call: ImportError
  raised: vertexai/tool_call: ImportError
  raised: xai/parallel_tool_call: TypeError

The TypeError pairs want an Iterable response model. vertexai/markdown_json_mode needs an SDK absent here. It reaches handle_vertexai_json at gemini/utils.py:650, which holds no such line. I did not run the test suite.

Expected behavior

The list and the dicts come back as they went in.

Screenshots

Not applicable.

#2417 is closed as completed and lists Gemini under "Unaffected (verified, no action needed)". Its scan tested new_kwargs["messages"] is caller_messages, and this path rebuilds contents while mutating in place. #2545 already carries the fix: copy_messages_for_mutation in handle_gemini_json, plus get_message_content returning a new list. Its author closed it as stale on August 21, and the head is still at e139923.

@jxnl, reopening #2545 costs less than a fifth diff in this queue. Shall I add the Gemini coverage to test_messages_not_mutated.py on top of it?