From 1ffa74f17fd7c5ec8db51afef0187d8221b9f751 Mon Sep 17 00:00:00 2001 From: Ayush Agrawal Date: Sat, 7 Nov 2026 16:56:04 -0800 Subject: [PATCH] fix: Drop the whole AFC exchange from curated chat history when the final model turn is invalid fixes #3314 FUTURE_COPYBARA_INTEGRATE_REVIEW=https://github.com/googleapis/python-genai/pull/3249 from googleapis:release-please--branches--main 4b4b0f38d1551bbfa327b705553695f72d1658c8 PiperOrigin-RevId: 1016479900 --- google/genai/chats.py | 80 +++++- google/genai/tests/chats/test_get_history.py | 287 ++++++++++++++++++- 2 files changed, 352 insertions(+), 15 deletions(-) diff --git a/google/genai/chats.py b/google/genai/chats.py index 5dd6c35036..2b1be8198b 100644 --- a/google/genai/chats.py +++ b/google/genai/chats.py @@ -62,6 +62,36 @@ def _validate_response(response: GenerateContentResponse) -> bool: return _validate_content(response.candidates[0].content) +def _has_function_call(content: Content) -> bool: + return any(part.function_call is not None for part in content.parts or []) + + +def _has_function_response(content: Content) -> bool: + return any(part.function_response is not None for part in content.parts or []) + + +def _drop_rejected_input(curated_history: list[Content]) -> None: + """Drops the input of a rejected turn from the end of the curated history. + + If that input is a function response, the model turn holding the call it + answers is dropped too, and so is the input before that call. This repeats + back to the input that started the exchange. + + Args: + curated_history: The curated history to trim in place. + """ + while curated_history: + rejected_input = curated_history.pop() + if not _has_function_response(rejected_input): + return + call_start = len(curated_history) + while call_start > 0 and curated_history[call_start - 1].role == "model": + call_start -= 1 + if not any(_has_function_call(c) for c in curated_history[call_start:]): + return + del curated_history[call_start:] + + def _extract_curated_history( comprehensive_history: list[Content], ) -> list[Content]: @@ -104,8 +134,8 @@ def _extract_curated_history( i += 1 if is_valid: curated_history.extend(current_output) - elif curated_history: - curated_history.pop() + else: + _drop_rejected_input(curated_history) return curated_history @@ -166,6 +196,18 @@ def record_history( self._curated_history.extend(input_contents) self._curated_history.extend(output_contents) + def _drop_afc_exchange(self, curated_length: int) -> None: + """Drops a rejected AFC exchange from the curated history. + + AFC records each function call and its response as it runs. If the final + model output is invalid, the whole exchange is dropped, as the input of + any invalid turn is. + + Args: + curated_length: The curated history length before the exchange began. + """ + del self._curated_history[curated_length:] + def get_history(self, curated: bool = False) -> list[Content]: """Returns the chat history. @@ -334,6 +376,7 @@ def send_message( response = types.GenerateContentResponse() function_map = _extra_utils.get_function_map(parsed_config) i = 0 + curated_length = len(self._curated_history) while remaining_remote_calls_afc > 0: i += 1 response = self._modules.generate_content( @@ -383,11 +426,14 @@ def send_message( if response.candidates and response.candidates[0].content else [] ) + is_valid = _validate_response(response) self.record_history( user_input=user_input, model_output=model_output, - is_valid=_validate_response(response), + is_valid=is_valid, ) + if not is_valid: + self._drop_afc_exchange(curated_length) return response def send_message_stream( @@ -503,6 +549,7 @@ def send_message_stream( function_map = _extra_utils.get_function_map(parsed_config) i = 0 if isinstance(self._modules, Models): + curated_length = len(self._curated_history) while remaining_remote_calls_afc > 0: i += 1 response_stream = self._modules.generate_content_stream( @@ -570,15 +617,16 @@ def send_message_stream( ) user_input = func_response_content + is_valid = bool( + is_valid and model_output is not None and finish_reason is not None + ) self.record_history( user_input=user_input, model_output=model_output, - is_valid=bool( - is_valid - and model_output is not None - and finish_reason is not None - ), + is_valid=is_valid, ) + if not is_valid: + self._drop_afc_exchange(curated_length) class Chats: @@ -822,6 +870,7 @@ async def send_message( ) i = 0 + curated_length = len(self._curated_history) while remaining_remote_calls_afc > 0: i += 1 response = await self._modules.generate_content( @@ -878,11 +927,14 @@ async def send_message( if response.candidates and response.candidates[0].content else [] ) + is_valid = _validate_response(response) self.record_history( user_input=user_input, model_output=model_output, - is_valid=_validate_response(response), + is_valid=is_valid, ) + if not is_valid: + self._drop_afc_exchange(curated_length) return response async def send_message_stream( @@ -1075,6 +1127,7 @@ async def async_generator(): # type: ignore[no-untyped-def] finish_reason = None is_valid = True + curated_length = len(self._curated_history) while remaining_remote_calls_afc > 0: i += 1 response_stream = await self._modules.generate_content_stream( @@ -1144,15 +1197,14 @@ async def async_generator(): # type: ignore[no-untyped-def] ) user_input = func_response_content + is_valid = bool(is_valid and model_output and finish_reason is not None) self.record_history( user_input=user_input, model_output=model_output, - is_valid=bool( - is_valid - and model_output - and finish_reason is not None - ), + is_valid=is_valid, ) + if not is_valid: + self._drop_afc_exchange(curated_length) return async_generator() # type: ignore[no-untyped-call, no-any-return] diff --git a/google/genai/tests/chats/test_get_history.py b/google/genai/tests/chats/test_get_history.py index 37d5eacac1..ca2c8b13e9 100644 --- a/google/genai/tests/chats/test_get_history.py +++ b/google/genai/tests/chats/test_get_history.py @@ -46,7 +46,7 @@ @pytest.fixture def mock_api_client(vertexai=False): - api_client = mock.MagicMock(spec=client.ApiClient) + api_client = mock.MagicMock(spec=client.BaseApiClient) api_client.api_key = 'TEST_API_KEY' api_client._host = lambda: 'test_host' api_client._http_options = {'headers': {}} # Ensure headers exist @@ -519,3 +519,288 @@ def test_chat_stream_with_empty_content( ] assert chat.get_history() == expected_comprehensive_history assert not chat.get_history(curated=True) + + +def get_weather(city: str) -> dict[str, int]: + """Returns the weather for a city.""" + del city # Unused. + return {'c': 21} + + +_AFC_CONFIG = types.GenerateContentConfig(tools=[get_weather]) + +_PRIOR_VALID_TURN = [ + types.Content(role='user', parts=[types.Part.from_text(text='hi')]), + types.Content(role='model', parts=[types.Part.from_text(text='hello')]), +] + + +def _function_call_content() -> types.Content: + return types.Content( + role='model', + parts=[ + types.Part.from_function_call( + name='get_weather', args={'city': 'Paris'} + ) + ], + ) + + +def _function_response_content() -> types.Content: + return types.Content( + role='user', + parts=[ + types.Part.from_function_response( + name='get_weather', response={'result': {'c': 21}} + ) + ], + ) + + +def _call_response() -> types.GenerateContentResponse: + return types.GenerateContentResponse( + candidates=[ + types.Candidate( + content=_function_call_content(), + finish_reason=types.FinishReason.STOP, + ) + ] + ) + + +def _blocked_response() -> types.GenerateContentResponse: + return types.GenerateContentResponse( + candidates=[types.Candidate(finish_reason=types.FinishReason.SAFETY)] + ) + + +def _text_response(text: str) -> types.GenerateContentResponse: + return types.GenerateContentResponse( + candidates=[ + types.Candidate( + content=types.Content( + role='model', parts=[types.Part.from_text(text=text)] + ), + finish_reason=types.FinishReason.STOP, + ) + ] + ) + + +def _user_text(text: str) -> types.Content: + return types.UserContent(parts=[types.Part.from_text(text=text)]) + + +_EMPTY_MODEL_CONTENT = types.Content(role='model', parts=[]) + + +def test_chat_afc_invalid_final_turn_drops_exchange(mock_api_client): + """A rejected final AFC answer drops the exchange from later requests.""" + chat = chats.Chats(modules=models.Models(mock_api_client)).create( + model='gemini-2.5-flash', config=_AFC_CONFIG + ) + with mock.patch.object( + models.Models, + 'generate_content', + side_effect=[_call_response(), _blocked_response(), _text_response('ok')], + ) as mock_generate_content: + chat.send_message('what is the weather in Paris') + + assert chat.get_history() == [ + _user_text('what is the weather in Paris'), + _function_call_content(), + _function_response_content(), + _EMPTY_MODEL_CONTENT, + ] + assert not chat.get_history(curated=True) + + chat.send_message('try again') + assert mock_generate_content.call_args_list[2].kwargs['contents'] == [ + _user_text('try again') + ] + + +def test_chat_afc_invalid_final_turn_keeps_earlier_turns(mock_api_client): + """Dropping a rejected AFC exchange keeps the valid turns before it.""" + chat = chats.Chats(modules=models.Models(mock_api_client)).create( + model='gemini-2.5-flash', config=_AFC_CONFIG, history=_PRIOR_VALID_TURN + ) + with mock.patch.object( + models.Models, + 'generate_content', + side_effect=[_call_response(), _blocked_response()], + ): + chat.send_message('what is the weather in Paris') + + assert chat.get_history(curated=True) == _PRIOR_VALID_TURN + assert len(chat.get_history()) == 6 + + +def test_chat_afc_invalid_final_turn_after_two_calls_drops_exchange( + mock_api_client, +): + """Every function call and response of a rejected exchange is dropped.""" + chat = chats.Chats(modules=models.Models(mock_api_client)).create( + model='gemini-2.5-flash', config=_AFC_CONFIG + ) + with mock.patch.object( + models.Models, + 'generate_content', + side_effect=[_call_response(), _call_response(), _blocked_response()], + ): + chat.send_message('what is the weather in Paris') + + assert len(chat.get_history()) == 6 + assert not chat.get_history(curated=True) + + +def test_chat_stream_afc_invalid_final_turn_drops_exchange(mock_api_client): + """The streaming AFC path drops the exchange when the final stream is empty.""" + chat = chats.Chats(modules=models.Models(mock_api_client)).create( + model='gemini-2.5-flash', config=_AFC_CONFIG + ) + with mock.patch.object( + models.Models, + 'generate_content_stream', + side_effect=[ + iter([_call_response()]), + iter([types.GenerateContentResponse(candidates=[])]), + ], + ): + for _ in chat.send_message_stream('what is the weather in Paris'): + pass + + assert chat.get_history() == [ + _user_text('what is the weather in Paris'), + _function_call_content(), + _function_response_content(), + _EMPTY_MODEL_CONTENT, + ] + assert not chat.get_history(curated=True) + + +@pytest.mark.asyncio +async def test_async_chat_afc_invalid_final_turn_drops_exchange( + mock_api_client, +): + """The async AFC path drops the exchange when the final answer is invalid.""" + chat = chats.AsyncChats(modules=models.AsyncModels(mock_api_client)).create( + model='gemini-2.5-flash', config=_AFC_CONFIG + ) + with mock.patch.object( + models.AsyncModels, + 'generate_content', + new_callable=mock.AsyncMock, + side_effect=[_call_response(), _blocked_response()], + ): + await chat.send_message('what is the weather in Paris') + + assert len(chat.get_history()) == 4 + assert not chat.get_history(curated=True) + + +@pytest.mark.asyncio +async def test_async_chat_stream_afc_invalid_final_turn_drops_exchange( + mock_api_client, +): + """The async streaming AFC path drops the exchange on an empty final stream.""" + + async def call_stream(): + yield _call_response() + + async def empty_stream(): + yield types.GenerateContentResponse(candidates=[]) + + chat = chats.AsyncChats(modules=models.AsyncModels(mock_api_client)).create( + model='gemini-2.5-flash', config=_AFC_CONFIG + ) + with mock.patch.object( + models.AsyncModels, + 'generate_content_stream', + new_callable=mock.AsyncMock, + side_effect=[call_stream(), empty_stream()], + ): + async for _ in await chat.send_message_stream( + 'what is the weather in Paris' + ): + pass + + assert len(chat.get_history()) == 4 + assert not chat.get_history(curated=True) + + +def test_history_afc_invalid_final_turn_drops_exchange(mock_api_client): + """A saved history with a rejected AFC exchange curates to the turns before.""" + history = _PRIOR_VALID_TURN + [ + _user_text('what is the weather in Paris'), + _function_call_content(), + _function_response_content(), + _function_call_content(), + _function_response_content(), + _EMPTY_MODEL_CONTENT, + ] + chat = chats.Chats(modules=models.Models(mock_api_client)).create( + model='gemini-2.5-flash', history=history + ) + + assert chat.get_history() == history + assert chat.get_history(curated=True) == _PRIOR_VALID_TURN + + +def test_chat_afc_valid_final_turn_keeps_exchange(mock_api_client): + """A valid AFC exchange stays in the curated history.""" + chat = chats.Chats(modules=models.Models(mock_api_client)).create( + model='gemini-2.5-flash', config=_AFC_CONFIG + ) + with mock.patch.object( + models.Models, + 'generate_content', + side_effect=[_call_response(), _text_response('21 degrees')], + ): + chat.send_message('what is the weather in Paris') + + assert chat.get_history(curated=True) == [ + _user_text('what is the weather in Paris'), + _function_call_content(), + _function_response_content(), + _text_response('21 degrees').candidates[0].content, + ] + + +def test_chat_afc_spent_budget_keeps_function_call(mock_api_client): + """With no remote calls left, the unanswered function call is kept.""" + config = types.GenerateContentConfig( + tools=[get_weather], + automatic_function_calling=types.AutomaticFunctionCallingConfig( + maximum_remote_calls=1 + ), + ) + chat = chats.Chats(modules=models.Models(mock_api_client)).create( + model='gemini-2.5-flash', config=config + ) + with mock.patch.object( + models.Models, 'generate_content', side_effect=[_call_response()] + ): + chat.send_message('what is the weather in Paris') + + assert chat.get_history(curated=True) == [ + _user_text('what is the weather in Paris'), + _function_call_content(), + ] + + +def test_history_spent_budget_tail_survives_rejected_next_turn( + mock_api_client, +): + """A rejected plain turn after a spent-budget tail drops only its input.""" + history = [ + _user_text('what is the weather in Paris'), + _function_call_content(), + _user_text('and in Rome?'), + _EMPTY_MODEL_CONTENT, + ] + chat = chats.Chats(modules=models.Models(mock_api_client)).create( + model='gemini-2.5-flash', history=history + ) + + assert chat.get_history(curated=True) == history[:2]