Repository navigation
Curated history keeps half a user turn when the model turn is invalid #3051
Description
Title: _extract_curated_history drops only the last user content when a model turn is invalid
An invalid model turn makes _extract_curated_history call curated_history.pop() once (chats.py:107-108). A user turn of two or more Content objects went in one at a time, so one pop removes part of it. The rest stays in the curated history with no model answer behind it.
The way in is chats.create(history=...), the only caller of that function (chats.py:136): a caller persisting raw exchanges stores one content per function response, and a blocked model turn as an empty content.
At 618f0aa, through the public constructor:
$ /var/tmp/p9bL16/genai/.venv/bin/python probe_hist2.py
import: /var/tmp/p9bL16/genai/google/genai/__init__.py
curated: [('user', 1)]
comprehensive: [('user', 1), ('user', 1), ('model', 0)]
hist = [
C(role="user", parts=[P(text="what is the weather")]),
C(role="user", parts=[P.from_function_response(name="get_weather", response={"c": 21})]),
C(role="model", parts=[]), # invalid model turn
]
chat = client.chats.create(model="gemini-2.0-flash", history=hist)The question survives and the tool result behind it disappears, so the next send_message sends a user turn the model already failed on, minus its evidence.
$ python -m pytest google/genai/tests/chats/test_get_history.py -q
17 passed, 1 warning in 0.06s
# not run: the replay suites, which need recorded sessions
Two of those tests bracket the gap. test_history_with_consecutive_valid_user_inputs (test_get_history.py:152) makes back-to-back user contents supported input, and test_history_with_invalid_turns (:401) sets the intent that the whole rejected user turn goes. One pop meets that intent only where the turn is a single content.
There is no CODEOWNERS here and every merge on GitHub is the Copybara import, so @jaycee-li, I am going by your last commit on this file. Should the pop walk back over the whole user run? I can write the diff out here if that helps the internal fix.
Activity
Venkaiahbabuneelam commentedon Oct 5, 2026
Hi @chrikrah, thanks for all the details! Could you share the exact code snippet of your receive loop? We'd love to try reproducing this on our end using the exact logic you have set up.
Thanks
Drop all trailing user turns when a model turn is invalid in _extract…
Drop all trailing user turns when a model turn is invalid in _extract…
chrikrah commentedon Oct 10, 2026
@Venkaiahbabuneelam, there is no receive loop. The history is built by hand and passed to
chats.create, and no request is sent, so a placeholder key works:The question stays and its function response goes. One wider case: with a model
function_callturn before the response, curated history ends on that unanswered call, so walking back over the user run (#3092) does not cover it. Does the script print the same for you?Venkaiahbabuneelam commentedon Oct 13, 2026
Hi @chrikrah,
Thank you for bringing this to our attention, We have reproduced and confirmed that when a model turn in history is invalid, _extract_curated_history in google/genai/chats.py only calls curated_history.pop() once, leaving earlier consecutive user Content items. We truly appreciate you flagging this issue, we will file a bug internally.
Thanks