|
| 1 | +# Copyright 2025 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 | +from __future__ import annotations |
| 16 | + |
| 17 | +from google.adk.utils import streaming_utils |
| 18 | +from google.genai import types |
| 19 | +import pytest |
| 20 | + |
| 21 | + |
| 22 | +class TestStreamingResponseAggregator: |
| 23 | + |
| 24 | + @pytest.mark.asyncio |
| 25 | + async def test_process_response_with_text(self): |
| 26 | + aggregator = streaming_utils.StreamingResponseAggregator() |
| 27 | + response = types.GenerateContentResponse( |
| 28 | + candidates=[ |
| 29 | + types.Candidate( |
| 30 | + content=types.Content(parts=[types.Part(text="Hello")]) |
| 31 | + ) |
| 32 | + ] |
| 33 | + ) |
| 34 | + results = [] |
| 35 | + async for r in aggregator.process_response(response): |
| 36 | + results.append(r) |
| 37 | + assert len(results) == 1 |
| 38 | + assert results[0].content.parts[0].text == "Hello" |
| 39 | + assert results[0].partial |
| 40 | + |
| 41 | + @pytest.mark.asyncio |
| 42 | + async def test_process_response_with_thought(self): |
| 43 | + aggregator = streaming_utils.StreamingResponseAggregator() |
| 44 | + response = types.GenerateContentResponse( |
| 45 | + candidates=[ |
| 46 | + types.Candidate( |
| 47 | + content=types.Content( |
| 48 | + parts=[types.Part(text="Thinking...", thought=True)] |
| 49 | + ) |
| 50 | + ) |
| 51 | + ] |
| 52 | + ) |
| 53 | + results = [] |
| 54 | + async for r in aggregator.process_response(response): |
| 55 | + results.append(r) |
| 56 | + assert len(results) == 1 |
| 57 | + assert results[0].content.parts[0].text == "Thinking..." |
| 58 | + assert results[0].content.parts[0].thought |
| 59 | + assert results[0].partial |
| 60 | + |
| 61 | + @pytest.mark.asyncio |
| 62 | + async def test_process_response_multiple(self): |
| 63 | + aggregator = streaming_utils.StreamingResponseAggregator() |
| 64 | + response1 = types.GenerateContentResponse( |
| 65 | + candidates=[ |
| 66 | + types.Candidate( |
| 67 | + content=types.Content(parts=[types.Part(text="Hello ")]) |
| 68 | + ) |
| 69 | + ] |
| 70 | + ) |
| 71 | + response2 = types.GenerateContentResponse( |
| 72 | + candidates=[ |
| 73 | + types.Candidate( |
| 74 | + content=types.Content(parts=[types.Part(text="World!")]) |
| 75 | + ) |
| 76 | + ] |
| 77 | + ) |
| 78 | + async for _ in aggregator.process_response(response1): |
| 79 | + pass |
| 80 | + results = [] |
| 81 | + async for r in aggregator.process_response(response2): |
| 82 | + results.append(r) |
| 83 | + assert len(results) == 1 |
| 84 | + assert results[0].content.parts[0].text == "World!" |
| 85 | + |
| 86 | + closed_responses = [] |
| 87 | + async for r in aggregator.close(): |
| 88 | + closed_responses.append(r) |
| 89 | + assert len(closed_responses) == 1 |
| 90 | + assert closed_responses[0].content.parts[0].text == "Hello World!" |
| 91 | + |
| 92 | + @pytest.mark.asyncio |
| 93 | + async def test_process_response_interleaved_thought_and_text(self): |
| 94 | + aggregator = streaming_utils.StreamingResponseAggregator() |
| 95 | + response1 = types.GenerateContentResponse( |
| 96 | + candidates=[ |
| 97 | + types.Candidate( |
| 98 | + content=types.Content( |
| 99 | + parts=[types.Part(text="I am thinking...", thought=True)] |
| 100 | + ) |
| 101 | + ) |
| 102 | + ] |
| 103 | + ) |
| 104 | + response2 = types.GenerateContentResponse( |
| 105 | + candidates=[ |
| 106 | + types.Candidate( |
| 107 | + content=types.Content( |
| 108 | + parts=[types.Part(text="Okay, I have a result.")] |
| 109 | + ) |
| 110 | + ) |
| 111 | + ] |
| 112 | + ) |
| 113 | + response3 = types.GenerateContentResponse( |
| 114 | + candidates=[ |
| 115 | + types.Candidate( |
| 116 | + content=types.Content( |
| 117 | + parts=[types.Part(text=" The result is 42.")] |
| 118 | + ) |
| 119 | + ) |
| 120 | + ] |
| 121 | + ) |
| 122 | + |
| 123 | + async for _ in aggregator.process_response(response1): |
| 124 | + pass |
| 125 | + async for _ in aggregator.process_response(response2): |
| 126 | + pass |
| 127 | + async for _ in aggregator.process_response(response3): |
| 128 | + pass |
| 129 | + |
| 130 | + closed_responses = [] |
| 131 | + async for r in aggregator.close(): |
| 132 | + closed_responses.append(r) |
| 133 | + assert len(closed_responses) == 1 |
| 134 | + closed_response = closed_responses[0] |
| 135 | + assert len(closed_response.content.parts) == 2 |
| 136 | + assert closed_response.content.parts[0].text == "I am thinking..." |
| 137 | + assert closed_response.content.parts[0].thought |
| 138 | + assert ( |
| 139 | + closed_response.content.parts[1].text |
| 140 | + == "Okay, I have a result. The result is 42." |
| 141 | + ) |
| 142 | + assert not closed_response.content.parts[1].thought |
| 143 | + |
| 144 | + @pytest.mark.asyncio |
| 145 | + async def test_close_with_no_responses(self): |
| 146 | + aggregator = streaming_utils.StreamingResponseAggregator() |
| 147 | + closed_responses = [] |
| 148 | + async for r in aggregator.close(): |
| 149 | + closed_responses.append(r) |
| 150 | + assert len(closed_responses) == 0 |
| 151 | + |
| 152 | + @pytest.mark.asyncio |
| 153 | + async def test_close_with_finish_reason(self): |
| 154 | + aggregator = streaming_utils.StreamingResponseAggregator() |
| 155 | + response = types.GenerateContentResponse( |
| 156 | + candidates=[ |
| 157 | + types.Candidate( |
| 158 | + content=types.Content(parts=[types.Part(text="Hello")]), |
| 159 | + finish_reason=types.FinishReason.STOP, |
| 160 | + ) |
| 161 | + ] |
| 162 | + ) |
| 163 | + async for _ in aggregator.process_response(response): |
| 164 | + pass |
| 165 | + closed_responses = [] |
| 166 | + async for r in aggregator.close(): |
| 167 | + closed_responses.append(r) |
| 168 | + assert len(closed_responses) == 1 |
| 169 | + assert closed_responses[0].content.parts[0].text == "Hello" |
| 170 | + assert closed_responses[0].error_code is None |
| 171 | + assert closed_responses[0].error_message is None |
| 172 | + |
| 173 | + @pytest.mark.asyncio |
| 174 | + async def test_close_with_error(self): |
| 175 | + aggregator = streaming_utils.StreamingResponseAggregator() |
| 176 | + response = types.GenerateContentResponse( |
| 177 | + candidates=[ |
| 178 | + types.Candidate( |
| 179 | + content=types.Content(parts=[types.Part(text="Error")]), |
| 180 | + finish_reason=types.FinishReason.RECITATION, |
| 181 | + finish_message="Recitation error", |
| 182 | + ) |
| 183 | + ] |
| 184 | + ) |
| 185 | + async for _ in aggregator.process_response(response): |
| 186 | + pass |
| 187 | + closed_responses = [] |
| 188 | + async for r in aggregator.close(): |
| 189 | + closed_responses.append(r) |
| 190 | + assert len(closed_responses) == 1 |
| 191 | + assert closed_responses[0].content.parts[0].text == "Error" |
| 192 | + assert closed_responses[0].error_code == types.FinishReason.RECITATION |
| 193 | + assert closed_responses[0].error_message == "Recitation error" |
0 commit comments