From 119ce100cf01e2f4a29a49a9dc38d215ccb05aea Mon Sep 17 00:00:00 2001 From: BowedMars2 Date: Thu, 5 Mar 2026 14:51:14 -0800 Subject: [PATCH 1/9] Adds unit tests for example_util.convert_examples_to_text() --- tests/unittests/examples/__init__.py | 13 + tests/unittests/examples/test_example_util.py | 384 ++++++++++++++++++ 2 files changed, 397 insertions(+) create mode 100644 tests/unittests/examples/__init__.py create mode 100644 tests/unittests/examples/test_example_util.py diff --git a/tests/unittests/examples/__init__.py b/tests/unittests/examples/__init__.py new file mode 100644 index 0000000000..30cb974f00 --- /dev/null +++ b/tests/unittests/examples/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. \ No newline at end of file diff --git a/tests/unittests/examples/test_example_util.py b/tests/unittests/examples/test_example_util.py new file mode 100644 index 0000000000..7a365f3915 --- /dev/null +++ b/tests/unittests/examples/test_example_util.py @@ -0,0 +1,384 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests for example_util.""" + +from google.adk.examples import example_util +from google.adk.examples import example +from google.genai import types +import pytest + + +def test_text_only_example_conversion(): + """Tests converting a text-only Example object to a string for use in a system instruction.""" + input_content = types.Content( + role="user", + parts=[types.Part(text="test_input")] + ) + output_content = [types.Content( + role="model", + parts=[types.Part(text="test_output")] + )] + test_example = example.Example( + input=input_content, + output=output_content + ) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert example_util.convert_examples_to_text(examples=[test_example], model=None) == expected_output + + +def test_multi_part_text_example_conversion(): + """Tests converting an Example object with multiple text Parts to a string for use in a system instruction.""" + input_content = types.Content( + role="user", + parts=[types.Part(text="test_input")] + ) + output_content = [types.Content( + role="model", + parts=[ + types.Part(text="test_output_1"), + types.Part(text="test_output_2"), + types.Part(text="test_output_3"), + ] + )] + test_example = example.Example( + input=input_content, + output=output_content + ) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output_1\ntest_output_2\ntest_output_3\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert example_util.convert_examples_to_text(examples=[test_example], model=None) == expected_output + + +def test_example_conversion_prefix_insertion(): + """Tests if user and model prefixes are properly alternated when converting an Example object to text for use in a system instruction.""" + input_content = types.Content( + role="user", + parts=[types.Part(text="test_input")] + ) + output_content = [ + types.Content( + role="model", + parts=[types.Part(text="test_output_1")] + ), + types.Content( + role="user", + parts=[types.Part(text="test_output_2")] + ), + types.Content( + role="model", + parts=[types.Part(text="test_output_3")] + ) + ] + test_example = example.Example( + input=input_content, + output=output_content + ) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output_1\n" + f"{example_util._USER_PREFIX}test_output_2\n" + f"{example_util._MODEL_PREFIX}test_output_3\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert example_util.convert_examples_to_text(examples=[test_example], model=None) == expected_output + + +def test_example_conversion_output_clumping(): + """Tests whether user and model inputs are properly clumped when converting an Example object to text for use in a system instruction.""" + input_content = types.Content( + role="user", + parts=[types.Part(text="test_input")] + ) + output_content = [ + types.Content( + role="model", + parts=[types.Part(text="test_output_1")] + ), + types.Content( + role="model", + parts=[types.Part(text="test_output_2")] + ), + types.Content( + role="user", + parts=[types.Part(text="test_output_3")] + ), + types.Content( + role="user", + parts=[types.Part(text="test_output_4")] + ) + ] + test_example = example.Example( + input=input_content, + output=output_content + ) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output_1\ntest_output_2\n" + f"{example_util._USER_PREFIX}test_output_3\ntest_output_4\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert example_util.convert_examples_to_text(examples=[test_example], model=None) == expected_output + + +def test_empty_examples_list_conversion(): + """Tests Example conversion to text if the examples list is empty.""" + expected_output = f"{example_util._EXAMPLES_INTRO}{example_util._EXAMPLES_END}" + + assert example_util.convert_examples_to_text(examples=[], model=None) == expected_output + + +@pytest.mark.parametrize( + "model", + [ + "gemini-2.5-flash", + "llama3_vertex_agent", + None + ], +) +def test_example_conversion_with_function_call(model): + """Tests converting an Example object containing a function call to a string for use in a system instruction.""" + test_function_call = types.FunctionCall( + name="test_function", + args={ + "test_string_argument": "test_value", + "test_int_argument": 1 + } + ) + input_content = types.Content( + role="user", + parts=[types.Part(text="test_input")] + ) + output_content = [types.Content( + role="model", + parts=[types.Part(function_call=test_function_call)] + )] + test_example = example.Example( + input=input_content, + output=output_content + ) + + gemini2 = model == "gemini-2.5-flash" or model == None + prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_CALL_PREFIX + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}{prefix}" + f"test_function(test_string_argument='test_value', test_int_argument=1)" + f"{example_util._FUNCTION_CALL_SUFFIX}" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output + + +@pytest.mark.parametrize( + "model", + [ + "gemini-2.5-flash", + "llama3_vertex_agent", + None + ], +) +def test_example_conversion_with_function_response(model): + """Tests converting an Example object containing a function response to a string for use in a system instruction.""" + test_function_response = types.FunctionResponse( + name="test_function", + response={ + "test_string_argument": "test_value", + "test_int_argument": 1 + } + ) + input_content = types.Content( + role="user", + parts=[types.Part(text="test_input")] + ) + output_content = [types.Content( + role="model", + parts=[types.Part(function_response=test_function_response)] + )] + test_example = example.Example( + input=input_content, + output=output_content + ) + + gemini2 = model == "gemini-2.5-flash" or model == None + prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_RESPONSE_PREFIX + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}{prefix}" + f"{test_function_response.__dict__}" + f"{example_util._FUNCTION_RESPONSE_SUFFIX}" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output + + +@pytest.mark.parametrize( + "model", + [ + "gemini-2.5-flash", + "llama3_vertex_agent", + None + ], +) +def test_example_conversion_with_function_call_response(model): + """Tests converting an Example object containing a function call and response to a string for use in a system instruction.""" + test_function_call = types.FunctionCall( + name="test_function", + args={ + "test_string_argument": "test_value", + "test_int_argument": 1 + } + ) + test_function_response = types.FunctionResponse( + name="test_function", + response={ + "test_string_argument": "test_value", + "test_int_argument": 1 + } + ) + input_content = types.Content( + role="user", + parts=[types.Part(text="test_input")] + ) + output_content = [types.Content( + role="model", + parts=[ + types.Part(function_call=test_function_call), + types.Part(function_response=test_function_response) + ] + )] + test_example = example.Example( + input=input_content, + output=output_content + ) + + gemini2 = model == "gemini-2.5-flash" or model == None + response_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_RESPONSE_PREFIX + call_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_CALL_PREFIX + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}{call_prefix}" + f"test_function(test_string_argument='test_value', test_int_argument=1)" + f"{example_util._FUNCTION_CALL_SUFFIX}" + f"{response_prefix}" + f"{test_function_response.__dict__}" + f"{example_util._FUNCTION_RESPONSE_SUFFIX}" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output + + +@pytest.mark.parametrize( + "model", + [ + "gemini-2.5-flash", + "llama3_vertex_agent", + None + ], +) +def test_example_conversion_with_text_and_function_call_response(model): + """Tests converting an Example object containing text, a function call, and a function response to a string for use in a system instruction.""" + test_function_call = types.FunctionCall( + name="test_function", + args={ + "test_string_argument": "test_value", + "test_int_argument": 1 + } + ) + test_function_response = types.FunctionResponse( + name="test_function", + response={ + "test_string_argument": "test_value", + "test_int_argument": 1 + } + ) + input_content = types.Content( + role="user", + parts=[types.Part(text="test_input")] + ) + output_content = [types.Content( + role="model", + parts=[ + types.Part(text="test_output"), + types.Part(function_call=test_function_call), + types.Part(function_response=test_function_response) + ] + )] + test_example = example.Example( + input=input_content, + output=output_content + ) + + gemini2 = model == "gemini-2.5-flash" or model == None + response_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_RESPONSE_PREFIX + call_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_CALL_PREFIX + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output\n" + f"{call_prefix}" + f"test_function(test_string_argument='test_value', test_int_argument=1)" + f"{example_util._FUNCTION_CALL_SUFFIX}" + f"{response_prefix}" + f"{test_function_response.__dict__}" + f"{example_util._FUNCTION_RESPONSE_SUFFIX}" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output \ No newline at end of file From 76d88f96b0bace8550e9bf0e85144aab722b4cc1 Mon Sep 17 00:00:00 2001 From: BowedMars2 Date: Thu, 5 Mar 2026 14:52:41 -0800 Subject: [PATCH 2/9] Removes the TODO comment --- src/google/adk/examples/example_util.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/google/adk/examples/example_util.py b/src/google/adk/examples/example_util.py index 5f95b4953e..6c6f213d73 100644 --- a/src/google/adk/examples/example_util.py +++ b/src/google/adk/examples/example_util.py @@ -46,7 +46,6 @@ _FUNCTION_RESPONSE_SUFFIX = "\n```\n" -# TODO(yaojie): Add unit tests for this function. def convert_examples_to_text( examples: list[Example], model: Optional[str] ) -> str: From 6632fcb98eaa5623d1fb0de720b93ae066a32b4f Mon Sep 17 00:00:00 2001 From: BowedMars2 Date: Thu, 5 Mar 2026 14:55:32 -0800 Subject: [PATCH 3/9] Makes line 365 more readable --- tests/unittests/examples/test_example_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittests/examples/test_example_util.py b/tests/unittests/examples/test_example_util.py index 7a365f3915..948178a324 100644 --- a/tests/unittests/examples/test_example_util.py +++ b/tests/unittests/examples/test_example_util.py @@ -362,7 +362,7 @@ def test_example_conversion_with_text_and_function_call_response(model): output=output_content ) - gemini2 = model == "gemini-2.5-flash" or model == None + gemini2 = model is None or model == "gemini-2.5-flash" response_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_RESPONSE_PREFIX call_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_CALL_PREFIX From 8c63e872e20bbf3183a5b25ffba00db1502f584e Mon Sep 17 00:00:00 2001 From: BowedMars2 Date: Thu, 12 Mar 2026 11:02:56 -0700 Subject: [PATCH 4/9] Add tests for build_example_si --- tests/unittests/examples/test_example_util.py | 108 +++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/tests/unittests/examples/test_example_util.py b/tests/unittests/examples/test_example_util.py index 948178a324..678bd14028 100644 --- a/tests/unittests/examples/test_example_util.py +++ b/tests/unittests/examples/test_example_util.py @@ -16,10 +16,43 @@ from google.adk.examples import example_util from google.adk.examples import example +from google.adk.examples import base_example_provider from google.genai import types import pytest +class MockExampleProvider(base_example_provider.BaseExampleProvider): + """Mocks an ExampleProvider object. + + This class provides mock implementation of the get_examples() function, + allowing the user to test functions that rely on an ExampleProvider + without creating a real ExampleProvider class and check that the correct + inputs are being passed to it. + """ + + def __init__(self, test_examples: list[example.Example], test_query: str) -> None: + """Initializes a MockBlob. + + Args: + test_examples: The list of examples to be returned on a successful query. + test_query: The query necessary to return a correct output. + """ + self.test_examples = test_examples + self.test_query = test_query + + def get_examples(self, query: str) -> list[example.Example]: + """Mocks querying the ExampleProvider for examples. + Verifies the query is correct, and returns an empty list if not. + + Args: + query: The query to check examples for. + """ + if query == self.test_query: + return self.test_examples + else: + return [] + + def test_text_only_example_conversion(): """Tests converting a text-only Example object to a string for use in a system instruction.""" input_content = types.Content( @@ -381,4 +414,77 @@ def test_example_conversion_with_text_and_function_call_response(model): f"{example_util._EXAMPLES_END}" ) - assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output \ No newline at end of file + assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output + + +@pytest.mark.parametrize( + "model", + [ + "gemini-2.5-flash", + "llama3_vertex_agent", + None + ], +) +def test_building_si_from_list(model): + """Tests building System Information from a list of examples.""" + input_content = types.Content( + role="user", + parts=[types.Part(text="test_input")] + ) + output_content = [types.Content( + role="model", + parts=[types.Part(text="test_output")] + )] + test_example = example.Example( + input=input_content, + output=output_content + ) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert example_util.build_example_si(examples=[test_example], query="", model=model) == expected_output + + + +@pytest.mark.parametrize( + "model", + [ + "gemini-2.5-flash", + "llama3_vertex_agent", + None + ], +) +def test_building_si_from_base_example_provider(model): + """Tests building System Information from a BaseExampleProvider object.""" + input_content = types.Content( + role="user", + parts=[types.Part(text="test_input")] + ) + output_content = [types.Content( + role="model", + parts=[types.Part(text="test_output")] + )] + test_example = example.Example( + input=input_content, + output=output_content + ) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + example_provider = MockExampleProvider(test_examples=[test_example], test_query="test_query") + + assert example_util.build_example_si(examples=example_provider, query="test_query", model=model) == expected_output From ec5137989a4efff87149cfd15758263056a443ff Mon Sep 17 00:00:00 2001 From: BowedMars2 Date: Thu, 12 Mar 2026 11:13:20 -0700 Subject: [PATCH 5/9] Parametrizes all unit tests to ensure different models don't break anything --- tests/unittests/examples/test_example_util.py | 60 +++++++++++++++---- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/tests/unittests/examples/test_example_util.py b/tests/unittests/examples/test_example_util.py index 678bd14028..a73dee56e6 100644 --- a/tests/unittests/examples/test_example_util.py +++ b/tests/unittests/examples/test_example_util.py @@ -53,7 +53,15 @@ def get_examples(self, query: str) -> list[example.Example]: return [] -def test_text_only_example_conversion(): +@pytest.mark.parametrize( + "model", + [ + "gemini-2.5-flash", + "llama3_vertex_agent", + None + ], +) +def test_text_only_example_conversion(model): """Tests converting a text-only Example object to a string for use in a system instruction.""" input_content = types.Content( role="user", @@ -77,10 +85,18 @@ def test_text_only_example_conversion(): f"{example_util._EXAMPLES_END}" ) - assert example_util.convert_examples_to_text(examples=[test_example], model=None) == expected_output + assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output -def test_multi_part_text_example_conversion(): +@pytest.mark.parametrize( + "model", + [ + "gemini-2.5-flash", + "llama3_vertex_agent", + None + ], +) +def test_multi_part_text_example_conversion(model): """Tests converting an Example object with multiple text Parts to a string for use in a system instruction.""" input_content = types.Content( role="user", @@ -108,10 +124,18 @@ def test_multi_part_text_example_conversion(): f"{example_util._EXAMPLES_END}" ) - assert example_util.convert_examples_to_text(examples=[test_example], model=None) == expected_output + assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output -def test_example_conversion_prefix_insertion(): +@pytest.mark.parametrize( + "model", + [ + "gemini-2.5-flash", + "llama3_vertex_agent", + None + ], +) +def test_example_conversion_prefix_insertion(model): """Tests if user and model prefixes are properly alternated when converting an Example object to text for use in a system instruction.""" input_content = types.Content( role="user", @@ -147,10 +171,18 @@ def test_example_conversion_prefix_insertion(): f"{example_util._EXAMPLES_END}" ) - assert example_util.convert_examples_to_text(examples=[test_example], model=None) == expected_output + assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output -def test_example_conversion_output_clumping(): +@pytest.mark.parametrize( + "model", + [ + "gemini-2.5-flash", + "llama3_vertex_agent", + None + ], +) +def test_example_conversion_output_clumping(model): """Tests whether user and model inputs are properly clumped when converting an Example object to text for use in a system instruction.""" input_content = types.Content( role="user", @@ -189,14 +221,22 @@ def test_example_conversion_output_clumping(): f"{example_util._EXAMPLES_END}" ) - assert example_util.convert_examples_to_text(examples=[test_example], model=None) == expected_output + assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output -def test_empty_examples_list_conversion(): +@pytest.mark.parametrize( + "model", + [ + "gemini-2.5-flash", + "llama3_vertex_agent", + None + ], +) +def test_empty_examples_list_conversion(model): """Tests Example conversion to text if the examples list is empty.""" expected_output = f"{example_util._EXAMPLES_INTRO}{example_util._EXAMPLES_END}" - assert example_util.convert_examples_to_text(examples=[], model=None) == expected_output + assert example_util.convert_examples_to_text(examples=[], model=model) == expected_output @pytest.mark.parametrize( From 37b01de6032ce9c360e3ffdf6611a9c61ed83f46 Mon Sep 17 00:00:00 2001 From: BowedMars2 Date: Thu, 12 Mar 2026 11:15:02 -0700 Subject: [PATCH 6/9] Ensures gemini2 check aligns with the check in example_util --- tests/unittests/examples/test_example_util.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unittests/examples/test_example_util.py b/tests/unittests/examples/test_example_util.py index a73dee56e6..67ac270fcc 100644 --- a/tests/unittests/examples/test_example_util.py +++ b/tests/unittests/examples/test_example_util.py @@ -269,7 +269,7 @@ def test_example_conversion_with_function_call(model): output=output_content ) - gemini2 = model == "gemini-2.5-flash" or model == None + gemini2 = model is None or "gemini-2" in model prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_CALL_PREFIX expected_output = ( @@ -316,7 +316,7 @@ def test_example_conversion_with_function_response(model): output=output_content ) - gemini2 = model == "gemini-2.5-flash" or model == None + gemini2 = model is None or "gemini-2" in model prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_RESPONSE_PREFIX expected_output = ( @@ -373,7 +373,7 @@ def test_example_conversion_with_function_call_response(model): output=output_content ) - gemini2 = model == "gemini-2.5-flash" or model == None + gemini2 = model is None or "gemini-2" in model response_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_RESPONSE_PREFIX call_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_CALL_PREFIX @@ -435,7 +435,7 @@ def test_example_conversion_with_text_and_function_call_response(model): output=output_content ) - gemini2 = model is None or model == "gemini-2.5-flash" + gemini2 = model is None or "gemini-2" in model response_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_RESPONSE_PREFIX call_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_CALL_PREFIX From b4dd245085d1894482f97daf40a9942d871af19b Mon Sep 17 00:00:00 2001 From: BowedMars2 Date: Thu, 12 Mar 2026 11:52:10 -0700 Subject: [PATCH 7/9] Fixes a docstring --- tests/unittests/examples/test_example_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittests/examples/test_example_util.py b/tests/unittests/examples/test_example_util.py index 67ac270fcc..1802ba4f20 100644 --- a/tests/unittests/examples/test_example_util.py +++ b/tests/unittests/examples/test_example_util.py @@ -502,7 +502,7 @@ def test_building_si_from_list(model): ], ) def test_building_si_from_base_example_provider(model): - """Tests building System Information from a BaseExampleProvider object.""" + """Tests building System Information from an example provider.""" input_content = types.Content( role="user", parts=[types.Part(text="test_input")] From feeca3f72d2907e05bad618faf23ac2e19f97b28 Mon Sep 17 00:00:00 2001 From: BowedMars2 Date: Thu, 12 Mar 2026 13:06:47 -0700 Subject: [PATCH 8/9] Fixes a docstring and makes the code more concise --- tests/unittests/examples/test_example_util.py | 104 +++++------------- 1 file changed, 26 insertions(+), 78 deletions(-) diff --git a/tests/unittests/examples/test_example_util.py b/tests/unittests/examples/test_example_util.py index 1802ba4f20..e297c7956e 100644 --- a/tests/unittests/examples/test_example_util.py +++ b/tests/unittests/examples/test_example_util.py @@ -20,6 +20,21 @@ from google.genai import types import pytest +BASIC_INPUT = types.Content( + role="user", + parts=[types.Part(text="test_input")] +) + +BASIC_OUTPUT = [types.Content( + role="model", + parts=[types.Part(text="test_output")] +)] + +BASIC_EXAMPLE = example.Example( + input=BASIC_INPUT, + output=BASIC_OUTPUT +) + class MockExampleProvider(base_example_provider.BaseExampleProvider): """Mocks an ExampleProvider object. @@ -31,7 +46,7 @@ class MockExampleProvider(base_example_provider.BaseExampleProvider): """ def __init__(self, test_examples: list[example.Example], test_query: str) -> None: - """Initializes a MockBlob. + """Initializes a MockExampleProvider. Args: test_examples: The list of examples to be returned on a successful query. @@ -63,19 +78,6 @@ def get_examples(self, query: str) -> list[example.Example]: ) def test_text_only_example_conversion(model): """Tests converting a text-only Example object to a string for use in a system instruction.""" - input_content = types.Content( - role="user", - parts=[types.Part(text="test_input")] - ) - output_content = [types.Content( - role="model", - parts=[types.Part(text="test_output")] - )] - test_example = example.Example( - input=input_content, - output=output_content - ) - expected_output = ( f"{example_util._EXAMPLES_INTRO}" f"{example_util._EXAMPLE_START.format(1)}" @@ -85,7 +87,7 @@ def test_text_only_example_conversion(model): f"{example_util._EXAMPLES_END}" ) - assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output + assert example_util.convert_examples_to_text(examples=[BASIC_EXAMPLE], model=model) == expected_output @pytest.mark.parametrize( @@ -98,10 +100,6 @@ def test_text_only_example_conversion(model): ) def test_multi_part_text_example_conversion(model): """Tests converting an Example object with multiple text Parts to a string for use in a system instruction.""" - input_content = types.Content( - role="user", - parts=[types.Part(text="test_input")] - ) output_content = [types.Content( role="model", parts=[ @@ -111,7 +109,7 @@ def test_multi_part_text_example_conversion(model): ] )] test_example = example.Example( - input=input_content, + input=BASIC_INPUT, output=output_content ) @@ -137,10 +135,6 @@ def test_multi_part_text_example_conversion(model): ) def test_example_conversion_prefix_insertion(model): """Tests if user and model prefixes are properly alternated when converting an Example object to text for use in a system instruction.""" - input_content = types.Content( - role="user", - parts=[types.Part(text="test_input")] - ) output_content = [ types.Content( role="model", @@ -156,7 +150,7 @@ def test_example_conversion_prefix_insertion(model): ) ] test_example = example.Example( - input=input_content, + input=BASIC_INPUT, output=output_content ) @@ -184,10 +178,6 @@ def test_example_conversion_prefix_insertion(model): ) def test_example_conversion_output_clumping(model): """Tests whether user and model inputs are properly clumped when converting an Example object to text for use in a system instruction.""" - input_content = types.Content( - role="user", - parts=[types.Part(text="test_input")] - ) output_content = [ types.Content( role="model", @@ -207,7 +197,7 @@ def test_example_conversion_output_clumping(model): ) ] test_example = example.Example( - input=input_content, + input=BASIC_INPUT, output=output_content ) @@ -256,16 +246,12 @@ def test_example_conversion_with_function_call(model): "test_int_argument": 1 } ) - input_content = types.Content( - role="user", - parts=[types.Part(text="test_input")] - ) output_content = [types.Content( role="model", parts=[types.Part(function_call=test_function_call)] )] test_example = example.Example( - input=input_content, + input=BASIC_INPUT, output=output_content ) @@ -303,16 +289,12 @@ def test_example_conversion_with_function_response(model): "test_int_argument": 1 } ) - input_content = types.Content( - role="user", - parts=[types.Part(text="test_input")] - ) output_content = [types.Content( role="model", parts=[types.Part(function_response=test_function_response)] )] test_example = example.Example( - input=input_content, + input=BASIC_INPUT, output=output_content ) @@ -357,10 +339,6 @@ def test_example_conversion_with_function_call_response(model): "test_int_argument": 1 } ) - input_content = types.Content( - role="user", - parts=[types.Part(text="test_input")] - ) output_content = [types.Content( role="model", parts=[ @@ -369,7 +347,7 @@ def test_example_conversion_with_function_call_response(model): ] )] test_example = example.Example( - input=input_content, + input=BASIC_INPUT, output=output_content ) @@ -418,10 +396,6 @@ def test_example_conversion_with_text_and_function_call_response(model): "test_int_argument": 1 } ) - input_content = types.Content( - role="user", - parts=[types.Part(text="test_input")] - ) output_content = [types.Content( role="model", parts=[ @@ -431,7 +405,7 @@ def test_example_conversion_with_text_and_function_call_response(model): ] )] test_example = example.Example( - input=input_content, + input=BASIC_INPUT, output=output_content ) @@ -467,19 +441,6 @@ def test_example_conversion_with_text_and_function_call_response(model): ) def test_building_si_from_list(model): """Tests building System Information from a list of examples.""" - input_content = types.Content( - role="user", - parts=[types.Part(text="test_input")] - ) - output_content = [types.Content( - role="model", - parts=[types.Part(text="test_output")] - )] - test_example = example.Example( - input=input_content, - output=output_content - ) - expected_output = ( f"{example_util._EXAMPLES_INTRO}" f"{example_util._EXAMPLE_START.format(1)}" @@ -489,7 +450,7 @@ def test_building_si_from_list(model): f"{example_util._EXAMPLES_END}" ) - assert example_util.build_example_si(examples=[test_example], query="", model=model) == expected_output + assert example_util.build_example_si(examples=[BASIC_EXAMPLE], query="", model=model) == expected_output @@ -503,19 +464,6 @@ def test_building_si_from_list(model): ) def test_building_si_from_base_example_provider(model): """Tests building System Information from an example provider.""" - input_content = types.Content( - role="user", - parts=[types.Part(text="test_input")] - ) - output_content = [types.Content( - role="model", - parts=[types.Part(text="test_output")] - )] - test_example = example.Example( - input=input_content, - output=output_content - ) - expected_output = ( f"{example_util._EXAMPLES_INTRO}" f"{example_util._EXAMPLE_START.format(1)}" @@ -525,6 +473,6 @@ def test_building_si_from_base_example_provider(model): f"{example_util._EXAMPLES_END}" ) - example_provider = MockExampleProvider(test_examples=[test_example], test_query="test_query") + example_provider = MockExampleProvider(test_examples=[BASIC_EXAMPLE], test_query="test_query") assert example_util.build_example_si(examples=example_provider, query="test_query", model=model) == expected_output From 3045c35e2ec43f4ac5f85f1088c25aa53db26465 Mon Sep 17 00:00:00 2001 From: BowedMars2 Date: Thu, 12 Mar 2026 16:27:45 -0700 Subject: [PATCH 9/9] Autoformat.sh --- contributing/samples/gepa/experiment.py | 1 - contributing/samples/gepa/run_experiment.py | 1 - tests/unittests/examples/__init__.py | 2 +- tests/unittests/examples/test_example_util.py | 752 +++++++++--------- 4 files changed, 367 insertions(+), 389 deletions(-) diff --git a/contributing/samples/gepa/experiment.py b/contributing/samples/gepa/experiment.py index f3751206a8..2710c3894c 100644 --- a/contributing/samples/gepa/experiment.py +++ b/contributing/samples/gepa/experiment.py @@ -43,7 +43,6 @@ from tau_bench.types import EnvRunResult from tau_bench.types import RunConfig import tau_bench_agent as tau_bench_agent_lib - import utils diff --git a/contributing/samples/gepa/run_experiment.py b/contributing/samples/gepa/run_experiment.py index d857da9635..e31db15788 100644 --- a/contributing/samples/gepa/run_experiment.py +++ b/contributing/samples/gepa/run_experiment.py @@ -25,7 +25,6 @@ from absl import flags import experiment from google.genai import types - import utils _OUTPUT_DIR = flags.DEFINE_string( diff --git a/tests/unittests/examples/__init__.py b/tests/unittests/examples/__init__.py index 30cb974f00..58d482ea38 100644 --- a/tests/unittests/examples/__init__.py +++ b/tests/unittests/examples/__init__.py @@ -10,4 +10,4 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and -# limitations under the License. \ No newline at end of file +# limitations under the License. diff --git a/tests/unittests/examples/test_example_util.py b/tests/unittests/examples/test_example_util.py index e297c7956e..7950552bd8 100644 --- a/tests/unittests/examples/test_example_util.py +++ b/tests/unittests/examples/test_example_util.py @@ -14,465 +14,445 @@ """Tests for example_util.""" -from google.adk.examples import example_util -from google.adk.examples import example from google.adk.examples import base_example_provider +from google.adk.examples import example +from google.adk.examples import example_util from google.genai import types import pytest -BASIC_INPUT = types.Content( - role="user", - parts=[types.Part(text="test_input")] -) +BASIC_INPUT = types.Content(role="user", parts=[types.Part(text="test_input")]) -BASIC_OUTPUT = [types.Content( - role="model", - parts=[types.Part(text="test_output")] -)] +BASIC_OUTPUT = [ + types.Content(role="model", parts=[types.Part(text="test_output")]) +] -BASIC_EXAMPLE = example.Example( - input=BASIC_INPUT, - output=BASIC_OUTPUT -) +BASIC_EXAMPLE = example.Example(input=BASIC_INPUT, output=BASIC_OUTPUT) class MockExampleProvider(base_example_provider.BaseExampleProvider): - """Mocks an ExampleProvider object. + """Mocks an ExampleProvider object. + + This class provides mock implementation of the get_examples() function, + allowing the user to test functions that rely on an ExampleProvider + without creating a real ExampleProvider class and check that the correct + inputs are being passed to it. + """ + + def __init__( + self, test_examples: list[example.Example], test_query: str + ) -> None: + """Initializes a MockExampleProvider. + + Args: + test_examples: The list of examples to be returned on a successful query. + test_query: The query necessary to return a correct output. + """ + self.test_examples = test_examples + self.test_query = test_query + + def get_examples(self, query: str) -> list[example.Example]: + """Mocks querying the ExampleProvider for examples. + Verifies the query is correct, and returns an empty list if not. - This class provides mock implementation of the get_examples() function, - allowing the user to test functions that rely on an ExampleProvider - without creating a real ExampleProvider class and check that the correct - inputs are being passed to it. + Args: + query: The query to check examples for. """ - - def __init__(self, test_examples: list[example.Example], test_query: str) -> None: - """Initializes a MockExampleProvider. - - Args: - test_examples: The list of examples to be returned on a successful query. - test_query: The query necessary to return a correct output. - """ - self.test_examples = test_examples - self.test_query = test_query - - def get_examples(self, query: str) -> list[example.Example]: - """Mocks querying the ExampleProvider for examples. - Verifies the query is correct, and returns an empty list if not. - - Args: - query: The query to check examples for. - """ - if query == self.test_query: - return self.test_examples - else: - return [] + if query == self.test_query: + return self.test_examples + else: + return [] @pytest.mark.parametrize( "model", - [ - "gemini-2.5-flash", - "llama3_vertex_agent", - None - ], + ["gemini-2.5-flash", "llama3_vertex_agent", None], ) def test_text_only_example_conversion(model): - """Tests converting a text-only Example object to a string for use in a system instruction.""" - expected_output = ( - f"{example_util._EXAMPLES_INTRO}" - f"{example_util._EXAMPLE_START.format(1)}" - f"{example_util._USER_PREFIX}test_input\n" - f"{example_util._MODEL_PREFIX}test_output\n" - f"{example_util._EXAMPLE_END}" - f"{example_util._EXAMPLES_END}" - ) - - assert example_util.convert_examples_to_text(examples=[BASIC_EXAMPLE], model=model) == expected_output + """Tests converting a text-only Example object to a string for use in a system instruction.""" + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert ( + example_util.convert_examples_to_text( + examples=[BASIC_EXAMPLE], model=model + ) + == expected_output + ) @pytest.mark.parametrize( "model", - [ - "gemini-2.5-flash", - "llama3_vertex_agent", - None - ], + ["gemini-2.5-flash", "llama3_vertex_agent", None], ) def test_multi_part_text_example_conversion(model): - """Tests converting an Example object with multiple text Parts to a string for use in a system instruction.""" - output_content = [types.Content( - role="model", - parts=[ - types.Part(text="test_output_1"), - types.Part(text="test_output_2"), - types.Part(text="test_output_3"), - ] - )] - test_example = example.Example( - input=BASIC_INPUT, - output=output_content - ) - - expected_output = ( - f"{example_util._EXAMPLES_INTRO}" - f"{example_util._EXAMPLE_START.format(1)}" - f"{example_util._USER_PREFIX}test_input\n" - f"{example_util._MODEL_PREFIX}test_output_1\ntest_output_2\ntest_output_3\n" - f"{example_util._EXAMPLE_END}" - f"{example_util._EXAMPLES_END}" - ) - - assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output + """Tests converting an Example object with multiple text Parts to a string for use in a system instruction.""" + output_content = [ + types.Content( + role="model", + parts=[ + types.Part(text="test_output_1"), + types.Part(text="test_output_2"), + types.Part(text="test_output_3"), + ], + ) + ] + test_example = example.Example(input=BASIC_INPUT, output=output_content) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output_1\ntest_output_2\ntest_output_3\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert ( + example_util.convert_examples_to_text( + examples=[test_example], model=model + ) + == expected_output + ) @pytest.mark.parametrize( "model", - [ - "gemini-2.5-flash", - "llama3_vertex_agent", - None - ], + ["gemini-2.5-flash", "llama3_vertex_agent", None], ) def test_example_conversion_prefix_insertion(model): - """Tests if user and model prefixes are properly alternated when converting an Example object to text for use in a system instruction.""" - output_content = [ - types.Content( - role="model", - parts=[types.Part(text="test_output_1")] - ), - types.Content( - role="user", - parts=[types.Part(text="test_output_2")] - ), - types.Content( - role="model", - parts=[types.Part(text="test_output_3")] - ) - ] - test_example = example.Example( - input=BASIC_INPUT, - output=output_content - ) - - expected_output = ( - f"{example_util._EXAMPLES_INTRO}" - f"{example_util._EXAMPLE_START.format(1)}" - f"{example_util._USER_PREFIX}test_input\n" - f"{example_util._MODEL_PREFIX}test_output_1\n" - f"{example_util._USER_PREFIX}test_output_2\n" - f"{example_util._MODEL_PREFIX}test_output_3\n" - f"{example_util._EXAMPLE_END}" - f"{example_util._EXAMPLES_END}" - ) - - assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output + """Tests if user and model prefixes are properly alternated when converting an Example object to text for use in a system instruction.""" + output_content = [ + types.Content(role="model", parts=[types.Part(text="test_output_1")]), + types.Content(role="user", parts=[types.Part(text="test_output_2")]), + types.Content(role="model", parts=[types.Part(text="test_output_3")]), + ] + test_example = example.Example(input=BASIC_INPUT, output=output_content) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output_1\n" + f"{example_util._USER_PREFIX}test_output_2\n" + f"{example_util._MODEL_PREFIX}test_output_3\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert ( + example_util.convert_examples_to_text( + examples=[test_example], model=model + ) + == expected_output + ) @pytest.mark.parametrize( "model", - [ - "gemini-2.5-flash", - "llama3_vertex_agent", - None - ], + ["gemini-2.5-flash", "llama3_vertex_agent", None], ) def test_example_conversion_output_clumping(model): - """Tests whether user and model inputs are properly clumped when converting an Example object to text for use in a system instruction.""" - output_content = [ - types.Content( - role="model", - parts=[types.Part(text="test_output_1")] - ), - types.Content( - role="model", - parts=[types.Part(text="test_output_2")] - ), - types.Content( - role="user", - parts=[types.Part(text="test_output_3")] - ), - types.Content( - role="user", - parts=[types.Part(text="test_output_4")] - ) - ] - test_example = example.Example( - input=BASIC_INPUT, - output=output_content - ) - - expected_output = ( - f"{example_util._EXAMPLES_INTRO}" - f"{example_util._EXAMPLE_START.format(1)}" - f"{example_util._USER_PREFIX}test_input\n" - f"{example_util._MODEL_PREFIX}test_output_1\ntest_output_2\n" - f"{example_util._USER_PREFIX}test_output_3\ntest_output_4\n" - f"{example_util._EXAMPLE_END}" - f"{example_util._EXAMPLES_END}" - ) - - assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output + """Tests whether user and model inputs are properly clumped when converting an Example object to text for use in a system instruction.""" + output_content = [ + types.Content(role="model", parts=[types.Part(text="test_output_1")]), + types.Content(role="model", parts=[types.Part(text="test_output_2")]), + types.Content(role="user", parts=[types.Part(text="test_output_3")]), + types.Content(role="user", parts=[types.Part(text="test_output_4")]), + ] + test_example = example.Example(input=BASIC_INPUT, output=output_content) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output_1\ntest_output_2\n" + f"{example_util._USER_PREFIX}test_output_3\ntest_output_4\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert ( + example_util.convert_examples_to_text( + examples=[test_example], model=model + ) + == expected_output + ) @pytest.mark.parametrize( "model", - [ - "gemini-2.5-flash", - "llama3_vertex_agent", - None - ], + ["gemini-2.5-flash", "llama3_vertex_agent", None], ) def test_empty_examples_list_conversion(model): - """Tests Example conversion to text if the examples list is empty.""" - expected_output = f"{example_util._EXAMPLES_INTRO}{example_util._EXAMPLES_END}" + """Tests Example conversion to text if the examples list is empty.""" + expected_output = ( + f"{example_util._EXAMPLES_INTRO}{example_util._EXAMPLES_END}" + ) - assert example_util.convert_examples_to_text(examples=[], model=model) == expected_output + assert ( + example_util.convert_examples_to_text(examples=[], model=model) + == expected_output + ) @pytest.mark.parametrize( "model", - [ - "gemini-2.5-flash", - "llama3_vertex_agent", - None - ], + ["gemini-2.5-flash", "llama3_vertex_agent", None], ) def test_example_conversion_with_function_call(model): - """Tests converting an Example object containing a function call to a string for use in a system instruction.""" - test_function_call = types.FunctionCall( - name="test_function", - args={ - "test_string_argument": "test_value", - "test_int_argument": 1 - } - ) - output_content = [types.Content( - role="model", - parts=[types.Part(function_call=test_function_call)] - )] - test_example = example.Example( - input=BASIC_INPUT, - output=output_content - ) - - gemini2 = model is None or "gemini-2" in model - prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_CALL_PREFIX - - expected_output = ( - f"{example_util._EXAMPLES_INTRO}" - f"{example_util._EXAMPLE_START.format(1)}" - f"{example_util._USER_PREFIX}test_input\n" - f"{example_util._MODEL_PREFIX}{prefix}" - f"test_function(test_string_argument='test_value', test_int_argument=1)" - f"{example_util._FUNCTION_CALL_SUFFIX}" - f"{example_util._EXAMPLE_END}" - f"{example_util._EXAMPLES_END}" - ) - - assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output + """Tests converting an Example object containing a function call to a string for use in a system instruction.""" + test_function_call = types.FunctionCall( + name="test_function", + args={"test_string_argument": "test_value", "test_int_argument": 1}, + ) + output_content = [ + types.Content( + role="model", parts=[types.Part(function_call=test_function_call)] + ) + ] + test_example = example.Example(input=BASIC_INPUT, output=output_content) + + gemini2 = model is None or "gemini-2" in model + prefix = ( + example_util._FUNCTION_PREFIX + if gemini2 + else example_util._FUNCTION_CALL_PREFIX + ) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}{prefix}" + "test_function(test_string_argument='test_value', test_int_argument=1)" + f"{example_util._FUNCTION_CALL_SUFFIX}" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert ( + example_util.convert_examples_to_text( + examples=[test_example], model=model + ) + == expected_output + ) @pytest.mark.parametrize( "model", - [ - "gemini-2.5-flash", - "llama3_vertex_agent", - None - ], + ["gemini-2.5-flash", "llama3_vertex_agent", None], ) def test_example_conversion_with_function_response(model): - """Tests converting an Example object containing a function response to a string for use in a system instruction.""" - test_function_response = types.FunctionResponse( - name="test_function", - response={ - "test_string_argument": "test_value", - "test_int_argument": 1 - } - ) - output_content = [types.Content( - role="model", - parts=[types.Part(function_response=test_function_response)] - )] - test_example = example.Example( - input=BASIC_INPUT, - output=output_content - ) - - gemini2 = model is None or "gemini-2" in model - prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_RESPONSE_PREFIX - - expected_output = ( - f"{example_util._EXAMPLES_INTRO}" - f"{example_util._EXAMPLE_START.format(1)}" - f"{example_util._USER_PREFIX}test_input\n" - f"{example_util._MODEL_PREFIX}{prefix}" - f"{test_function_response.__dict__}" - f"{example_util._FUNCTION_RESPONSE_SUFFIX}" - f"{example_util._EXAMPLE_END}" - f"{example_util._EXAMPLES_END}" - ) - - assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output + """Tests converting an Example object containing a function response to a string for use in a system instruction.""" + test_function_response = types.FunctionResponse( + name="test_function", + response={"test_string_argument": "test_value", "test_int_argument": 1}, + ) + output_content = [ + types.Content( + role="model", + parts=[types.Part(function_response=test_function_response)], + ) + ] + test_example = example.Example(input=BASIC_INPUT, output=output_content) + + gemini2 = model is None or "gemini-2" in model + prefix = ( + example_util._FUNCTION_PREFIX + if gemini2 + else example_util._FUNCTION_RESPONSE_PREFIX + ) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}{prefix}" + f"{test_function_response.__dict__}" + f"{example_util._FUNCTION_RESPONSE_SUFFIX}" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert ( + example_util.convert_examples_to_text( + examples=[test_example], model=model + ) + == expected_output + ) @pytest.mark.parametrize( "model", - [ - "gemini-2.5-flash", - "llama3_vertex_agent", - None - ], + ["gemini-2.5-flash", "llama3_vertex_agent", None], ) def test_example_conversion_with_function_call_response(model): - """Tests converting an Example object containing a function call and response to a string for use in a system instruction.""" - test_function_call = types.FunctionCall( - name="test_function", - args={ - "test_string_argument": "test_value", - "test_int_argument": 1 - } - ) - test_function_response = types.FunctionResponse( - name="test_function", - response={ - "test_string_argument": "test_value", - "test_int_argument": 1 - } - ) - output_content = [types.Content( - role="model", - parts=[ - types.Part(function_call=test_function_call), - types.Part(function_response=test_function_response) - ] - )] - test_example = example.Example( - input=BASIC_INPUT, - output=output_content - ) - - gemini2 = model is None or "gemini-2" in model - response_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_RESPONSE_PREFIX - call_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_CALL_PREFIX - - expected_output = ( - f"{example_util._EXAMPLES_INTRO}" - f"{example_util._EXAMPLE_START.format(1)}" - f"{example_util._USER_PREFIX}test_input\n" - f"{example_util._MODEL_PREFIX}{call_prefix}" - f"test_function(test_string_argument='test_value', test_int_argument=1)" - f"{example_util._FUNCTION_CALL_SUFFIX}" - f"{response_prefix}" - f"{test_function_response.__dict__}" - f"{example_util._FUNCTION_RESPONSE_SUFFIX}" - f"{example_util._EXAMPLE_END}" - f"{example_util._EXAMPLES_END}" - ) - - assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output + """Tests converting an Example object containing a function call and response to a string for use in a system instruction.""" + test_function_call = types.FunctionCall( + name="test_function", + args={"test_string_argument": "test_value", "test_int_argument": 1}, + ) + test_function_response = types.FunctionResponse( + name="test_function", + response={"test_string_argument": "test_value", "test_int_argument": 1}, + ) + output_content = [ + types.Content( + role="model", + parts=[ + types.Part(function_call=test_function_call), + types.Part(function_response=test_function_response), + ], + ) + ] + test_example = example.Example(input=BASIC_INPUT, output=output_content) + + gemini2 = model is None or "gemini-2" in model + response_prefix = ( + example_util._FUNCTION_PREFIX + if gemini2 + else example_util._FUNCTION_RESPONSE_PREFIX + ) + call_prefix = ( + example_util._FUNCTION_PREFIX + if gemini2 + else example_util._FUNCTION_CALL_PREFIX + ) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}{call_prefix}" + "test_function(test_string_argument='test_value', test_int_argument=1)" + f"{example_util._FUNCTION_CALL_SUFFIX}" + f"{response_prefix}" + f"{test_function_response.__dict__}" + f"{example_util._FUNCTION_RESPONSE_SUFFIX}" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert ( + example_util.convert_examples_to_text( + examples=[test_example], model=model + ) + == expected_output + ) @pytest.mark.parametrize( "model", - [ - "gemini-2.5-flash", - "llama3_vertex_agent", - None - ], + ["gemini-2.5-flash", "llama3_vertex_agent", None], ) def test_example_conversion_with_text_and_function_call_response(model): - """Tests converting an Example object containing text, a function call, and a function response to a string for use in a system instruction.""" - test_function_call = types.FunctionCall( - name="test_function", - args={ - "test_string_argument": "test_value", - "test_int_argument": 1 - } - ) - test_function_response = types.FunctionResponse( - name="test_function", - response={ - "test_string_argument": "test_value", - "test_int_argument": 1 - } - ) - output_content = [types.Content( - role="model", - parts=[ - types.Part(text="test_output"), - types.Part(function_call=test_function_call), - types.Part(function_response=test_function_response) - ] - )] - test_example = example.Example( - input=BASIC_INPUT, - output=output_content - ) - - gemini2 = model is None or "gemini-2" in model - response_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_RESPONSE_PREFIX - call_prefix = example_util._FUNCTION_PREFIX if gemini2 else example_util._FUNCTION_CALL_PREFIX - - expected_output = ( - f"{example_util._EXAMPLES_INTRO}" - f"{example_util._EXAMPLE_START.format(1)}" - f"{example_util._USER_PREFIX}test_input\n" - f"{example_util._MODEL_PREFIX}test_output\n" - f"{call_prefix}" - f"test_function(test_string_argument='test_value', test_int_argument=1)" - f"{example_util._FUNCTION_CALL_SUFFIX}" - f"{response_prefix}" - f"{test_function_response.__dict__}" - f"{example_util._FUNCTION_RESPONSE_SUFFIX}" - f"{example_util._EXAMPLE_END}" - f"{example_util._EXAMPLES_END}" - ) - - assert example_util.convert_examples_to_text(examples=[test_example], model=model) == expected_output - - + """Tests converting an Example object containing text, a function call, and a function response to a string for use in a system instruction.""" + test_function_call = types.FunctionCall( + name="test_function", + args={"test_string_argument": "test_value", "test_int_argument": 1}, + ) + test_function_response = types.FunctionResponse( + name="test_function", + response={"test_string_argument": "test_value", "test_int_argument": 1}, + ) + output_content = [ + types.Content( + role="model", + parts=[ + types.Part(text="test_output"), + types.Part(function_call=test_function_call), + types.Part(function_response=test_function_response), + ], + ) + ] + test_example = example.Example(input=BASIC_INPUT, output=output_content) + + gemini2 = model is None or "gemini-2" in model + response_prefix = ( + example_util._FUNCTION_PREFIX + if gemini2 + else example_util._FUNCTION_RESPONSE_PREFIX + ) + call_prefix = ( + example_util._FUNCTION_PREFIX + if gemini2 + else example_util._FUNCTION_CALL_PREFIX + ) + + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output\n" + f"{call_prefix}" + "test_function(test_string_argument='test_value', test_int_argument=1)" + f"{example_util._FUNCTION_CALL_SUFFIX}" + f"{response_prefix}" + f"{test_function_response.__dict__}" + f"{example_util._FUNCTION_RESPONSE_SUFFIX}" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert ( + example_util.convert_examples_to_text( + examples=[test_example], model=model + ) + == expected_output + ) + + @pytest.mark.parametrize( "model", - [ - "gemini-2.5-flash", - "llama3_vertex_agent", - None - ], + ["gemini-2.5-flash", "llama3_vertex_agent", None], ) def test_building_si_from_list(model): - """Tests building System Information from a list of examples.""" - expected_output = ( - f"{example_util._EXAMPLES_INTRO}" - f"{example_util._EXAMPLE_START.format(1)}" - f"{example_util._USER_PREFIX}test_input\n" - f"{example_util._MODEL_PREFIX}test_output\n" - f"{example_util._EXAMPLE_END}" - f"{example_util._EXAMPLES_END}" - ) - - assert example_util.build_example_si(examples=[BASIC_EXAMPLE], query="", model=model) == expected_output + """Tests building System Information from a list of examples.""" + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + assert ( + example_util.build_example_si( + examples=[BASIC_EXAMPLE], query="", model=model + ) + == expected_output + ) - @pytest.mark.parametrize( "model", - [ - "gemini-2.5-flash", - "llama3_vertex_agent", - None - ], + ["gemini-2.5-flash", "llama3_vertex_agent", None], ) def test_building_si_from_base_example_provider(model): - """Tests building System Information from an example provider.""" - expected_output = ( - f"{example_util._EXAMPLES_INTRO}" - f"{example_util._EXAMPLE_START.format(1)}" - f"{example_util._USER_PREFIX}test_input\n" - f"{example_util._MODEL_PREFIX}test_output\n" - f"{example_util._EXAMPLE_END}" - f"{example_util._EXAMPLES_END}" - ) - - example_provider = MockExampleProvider(test_examples=[BASIC_EXAMPLE], test_query="test_query") - - assert example_util.build_example_si(examples=example_provider, query="test_query", model=model) == expected_output + """Tests building System Information from an example provider.""" + expected_output = ( + f"{example_util._EXAMPLES_INTRO}" + f"{example_util._EXAMPLE_START.format(1)}" + f"{example_util._USER_PREFIX}test_input\n" + f"{example_util._MODEL_PREFIX}test_output\n" + f"{example_util._EXAMPLE_END}" + f"{example_util._EXAMPLES_END}" + ) + + example_provider = MockExampleProvider( + test_examples=[BASIC_EXAMPLE], test_query="test_query" + ) + + assert ( + example_util.build_example_si( + examples=example_provider, query="test_query", model=model + ) + == expected_output + )