|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests that function call IDs stay stable across streaming events.""" |
| 16 | + |
| 17 | +from google.adk.events.event import Event |
| 18 | +from google.adk.flows.llm_flows.base_llm_flow import _finalize_model_response_event |
| 19 | +from google.adk.flows.llm_flows.functions import populate_client_function_call_id |
| 20 | +from google.adk.models.llm_request import LlmRequest |
| 21 | +from google.adk.models.llm_response import LlmResponse |
| 22 | +from google.genai import types |
| 23 | +import pytest |
| 24 | + |
| 25 | + |
| 26 | +def _make_fc_response(name: str, args: dict | None = None, partial: bool = False) -> LlmResponse: |
| 27 | + """Create an LlmResponse containing a single function call.""" |
| 28 | + fc = types.FunctionCall(name=name, args=args or {}) |
| 29 | + return LlmResponse( |
| 30 | + content=types.Content(role='model', parts=[types.Part(function_call=fc)]), |
| 31 | + partial=partial, |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def _make_multi_fc_response(calls: list[tuple[str, dict]], partial: bool = False) -> LlmResponse: |
| 36 | + """Create an LlmResponse containing multiple function calls.""" |
| 37 | + parts = [ |
| 38 | + types.Part(function_call=types.FunctionCall(name=name, args=args)) |
| 39 | + for name, args in calls |
| 40 | + ] |
| 41 | + return LlmResponse( |
| 42 | + content=types.Content(role='model', parts=parts), |
| 43 | + partial=partial, |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +class TestPopulateClientFunctionCallIdWithCache: |
| 48 | + """Tests for populate_client_function_call_id with ID caching.""" |
| 49 | + |
| 50 | + def test_generates_id_and_stores_in_cache(self): |
| 51 | + event = Event(author='agent') |
| 52 | + event.content = types.Content( |
| 53 | + role='model', |
| 54 | + parts=[types.Part(function_call=types.FunctionCall(name='get_weather', args={}))], |
| 55 | + ) |
| 56 | + cache: dict[str, str] = {} |
| 57 | + populate_client_function_call_id(event, cache) |
| 58 | + fc = event.get_function_calls()[0] |
| 59 | + assert fc.id.startswith('adk-') |
| 60 | + assert 'get_weather:0' in cache |
| 61 | + assert cache['get_weather:0'] == fc.id |
| 62 | + |
| 63 | + def test_reuses_cached_id(self): |
| 64 | + cache: dict[str, str] = {'get_weather:0': 'adk-cached-id-123'} |
| 65 | + |
| 66 | + event = Event(author='agent') |
| 67 | + event.content = types.Content( |
| 68 | + role='model', |
| 69 | + parts=[types.Part(function_call=types.FunctionCall(name='get_weather', args={}))], |
| 70 | + ) |
| 71 | + populate_client_function_call_id(event, cache) |
| 72 | + assert event.get_function_calls()[0].id == 'adk-cached-id-123' |
| 73 | + |
| 74 | + def test_no_cache_generates_new_id_each_time(self): |
| 75 | + event1 = Event(author='agent') |
| 76 | + event1.content = types.Content( |
| 77 | + role='model', |
| 78 | + parts=[types.Part(function_call=types.FunctionCall(name='get_weather', args={}))], |
| 79 | + ) |
| 80 | + event2 = Event(author='agent') |
| 81 | + event2.content = types.Content( |
| 82 | + role='model', |
| 83 | + parts=[types.Part(function_call=types.FunctionCall(name='get_weather', args={}))], |
| 84 | + ) |
| 85 | + populate_client_function_call_id(event1) |
| 86 | + populate_client_function_call_id(event2) |
| 87 | + assert event1.get_function_calls()[0].id != event2.get_function_calls()[0].id |
| 88 | + |
| 89 | + def test_multiple_calls_same_name_get_separate_ids(self): |
| 90 | + event = Event(author='agent') |
| 91 | + event.content = types.Content( |
| 92 | + role='model', |
| 93 | + parts=[ |
| 94 | + types.Part(function_call=types.FunctionCall(name='search', args={'q': 'a'})), |
| 95 | + types.Part(function_call=types.FunctionCall(name='search', args={'q': 'b'})), |
| 96 | + ], |
| 97 | + ) |
| 98 | + cache: dict[str, str] = {} |
| 99 | + populate_client_function_call_id(event, cache) |
| 100 | + fcs = event.get_function_calls() |
| 101 | + assert fcs[0].id != fcs[1].id |
| 102 | + assert cache['search:0'] == fcs[0].id |
| 103 | + assert cache['search:1'] == fcs[1].id |
| 104 | + |
| 105 | + def test_skips_function_calls_that_already_have_ids(self): |
| 106 | + event = Event(author='agent') |
| 107 | + event.content = types.Content( |
| 108 | + role='model', |
| 109 | + parts=[types.Part(function_call=types.FunctionCall( |
| 110 | + name='get_weather', args={}, id='server-provided-id'))], |
| 111 | + ) |
| 112 | + cache: dict[str, str] = {} |
| 113 | + populate_client_function_call_id(event, cache) |
| 114 | + assert event.get_function_calls()[0].id == 'server-provided-id' |
| 115 | + assert len(cache) == 0 |
| 116 | + |
| 117 | + |
| 118 | +class TestFinalizeModelResponseEventWithCache: |
| 119 | + """Tests that _finalize_model_response_event preserves IDs via cache.""" |
| 120 | + |
| 121 | + def test_partial_and_final_share_same_function_call_id(self): |
| 122 | + model_response_event = Event( |
| 123 | + author='agent', |
| 124 | + invocation_id='inv-1', |
| 125 | + ) |
| 126 | + llm_request = LlmRequest(model='mock', contents=[]) |
| 127 | + cache: dict[str, str] = {} |
| 128 | + |
| 129 | + # Partial event |
| 130 | + partial_response = _make_fc_response('get_weather', partial=True) |
| 131 | + partial_event = _finalize_model_response_event( |
| 132 | + llm_request, partial_response, model_response_event, cache, |
| 133 | + ) |
| 134 | + partial_id = partial_event.get_function_calls()[0].id |
| 135 | + assert partial_id.startswith('adk-') |
| 136 | + |
| 137 | + # Final event — same function call must get the same ID |
| 138 | + final_response = _make_fc_response('get_weather', partial=False) |
| 139 | + final_event = _finalize_model_response_event( |
| 140 | + llm_request, final_response, model_response_event, cache, |
| 141 | + ) |
| 142 | + final_id = final_event.get_function_calls()[0].id |
| 143 | + assert final_id == partial_id |
| 144 | + |
| 145 | + def test_without_cache_ids_differ(self): |
| 146 | + model_response_event = Event( |
| 147 | + author='agent', |
| 148 | + invocation_id='inv-1', |
| 149 | + ) |
| 150 | + llm_request = LlmRequest(model='mock', contents=[]) |
| 151 | + |
| 152 | + partial_response = _make_fc_response('get_weather', partial=True) |
| 153 | + partial_event = _finalize_model_response_event( |
| 154 | + llm_request, partial_response, model_response_event, |
| 155 | + ) |
| 156 | + partial_id = partial_event.get_function_calls()[0].id |
| 157 | + |
| 158 | + final_response = _make_fc_response('get_weather', partial=False) |
| 159 | + final_event = _finalize_model_response_event( |
| 160 | + llm_request, final_response, model_response_event, |
| 161 | + ) |
| 162 | + final_id = final_event.get_function_calls()[0].id |
| 163 | + |
| 164 | + # Without cache, IDs are different (this is the bug scenario) |
| 165 | + assert final_id != partial_id |
| 166 | + |
| 167 | + def test_multi_function_call_streaming_preserves_all_ids(self): |
| 168 | + model_response_event = Event( |
| 169 | + author='agent', |
| 170 | + invocation_id='inv-1', |
| 171 | + ) |
| 172 | + llm_request = LlmRequest(model='mock', contents=[]) |
| 173 | + cache: dict[str, str] = {} |
| 174 | + |
| 175 | + # Partial with two function calls |
| 176 | + partial_response = _make_multi_fc_response( |
| 177 | + [('search', {'q': 'weather'}), ('lookup', {'id': '42'})], |
| 178 | + partial=True, |
| 179 | + ) |
| 180 | + partial_event = _finalize_model_response_event( |
| 181 | + llm_request, partial_response, model_response_event, cache, |
| 182 | + ) |
| 183 | + partial_ids = [fc.id for fc in partial_event.get_function_calls()] |
| 184 | + |
| 185 | + # Final with same two function calls |
| 186 | + final_response = _make_multi_fc_response( |
| 187 | + [('search', {'q': 'weather'}), ('lookup', {'id': '42'})], |
| 188 | + partial=False, |
| 189 | + ) |
| 190 | + final_event = _finalize_model_response_event( |
| 191 | + llm_request, final_response, model_response_event, cache, |
| 192 | + ) |
| 193 | + final_ids = [fc.id for fc in final_event.get_function_calls()] |
| 194 | + |
| 195 | + assert partial_ids == final_ids |
| 196 | + assert partial_ids[0] != partial_ids[1] # different calls have different IDs |
0 commit comments