@@ -356,6 +356,63 @@ def test_api_client_preserves_custom_base_url_path():
356356 assert client ._api_client ._http_options .api_version == "v1beta"
357357
358358
359+ def test_api_client_default_api_version_unchanged (monkeypatch ):
360+ """Without configuration, ADK does not force an api_version (SDK default)."""
361+ monkeypatch .delenv ("GOOGLE_GENAI_API_VERSION" , raising = False )
362+ model = Gemini (model = "gemini-2.5-flash" )
363+
364+ # ADK leaves api_version unset so the google-genai SDK applies its own
365+ # default (v1beta1 for Vertex AI), preserving existing behavior.
366+ assert model ._base_url_and_api_version == (None , None )
367+ client = model .api_client
368+ assert client ._api_client ._http_options .api_version == "v1beta"
369+
370+
371+ def test_api_client_uses_api_version_field ():
372+ """The api_version field flows into the constructed client's http_options."""
373+ model = Gemini (model = "gemini-2.5-flash" , api_version = "v1" )
374+
375+ client = model .api_client
376+
377+ assert client ._api_client ._http_options .api_version == "v1"
378+
379+
380+ def test_api_client_uses_api_version_env_var (monkeypatch ):
381+ """The GOOGLE_GENAI_API_VERSION env var flows into http_options."""
382+ monkeypatch .setenv ("GOOGLE_GENAI_API_VERSION" , "v1" )
383+ model = Gemini (model = "gemini-2.5-flash" )
384+
385+ client = model .api_client
386+
387+ assert client ._api_client ._http_options .api_version == "v1"
388+
389+
390+ def test_api_version_field_overrides_env_var (monkeypatch ):
391+ """The explicit api_version field takes precedence over the env var."""
392+ monkeypatch .setenv ("GOOGLE_GENAI_API_VERSION" , "v1beta1" )
393+ model = Gemini (model = "gemini-2.5-flash" , api_version = "v1" )
394+
395+ client = model .api_client
396+
397+ assert client ._api_client ._http_options .api_version == "v1"
398+
399+
400+ def test_base_url_api_version_overrides_field ():
401+ """A version embedded in base_url wins over the api_version field."""
402+ model = Gemini (
403+ model = "gemini-2.5-flash" ,
404+ base_url = "https://generativelanguage.googleapis.com/v1alpha" ,
405+ api_version = "v1" ,
406+ )
407+
408+ client = model .api_client
409+
410+ assert client ._api_client ._http_options .base_url == (
411+ "https://generativelanguage.googleapis.com/"
412+ )
413+ assert client ._api_client ._http_options .api_version == "v1alpha"
414+
415+
359416def test_maybe_append_user_content (gemini_llm , llm_request ):
360417 # Test with user content already present
361418 gemini_llm ._maybe_append_user_content (llm_request )
@@ -788,6 +845,90 @@ async def mock_coro():
788845 assert len (responses ) == 2 if stream else 1
789846
790847
848+ @pytest .mark .asyncio
849+ async def test_generate_content_async_patches_api_version_from_field (
850+ llm_request , generate_content_response
851+ ):
852+ """The configured api_version field is patched onto the request config."""
853+ gemini_llm = Gemini (model = "gemini-2.5-flash" , api_version = "v1" )
854+ llm_request .config .http_options = types .HttpOptions (
855+ headers = {"custom-header" : "custom-value" }
856+ )
857+
858+ with mock .patch .object (gemini_llm , "api_client" ) as mock_client :
859+
860+ async def mock_coro ():
861+ return generate_content_response
862+
863+ mock_client .aio .models .generate_content .return_value = mock_coro ()
864+
865+ _ = [
866+ resp
867+ async for resp in gemini_llm .generate_content_async (
868+ llm_request , stream = False
869+ )
870+ ]
871+
872+ call_args = mock_client .aio .models .generate_content .call_args
873+ final_config = call_args .kwargs ["config" ]
874+ assert final_config .http_options .api_version == "v1"
875+
876+
877+ @pytest .mark .asyncio
878+ async def test_generate_content_async_does_not_override_request_api_version (
879+ llm_request , generate_content_response
880+ ):
881+ """Request-level api_version takes precedence over model-level configuration."""
882+ gemini_llm = Gemini (model = "gemini-2.5-flash" , api_version = "v1" )
883+ llm_request .config .http_options = types .HttpOptions (api_version = "v2" )
884+
885+ with mock .patch .object (gemini_llm , "api_client" ) as mock_client :
886+
887+ async def mock_coro ():
888+ return generate_content_response
889+
890+ mock_client .aio .models .generate_content .return_value = mock_coro ()
891+
892+ _ = [
893+ resp
894+ async for resp in gemini_llm .generate_content_async (
895+ llm_request , stream = False
896+ )
897+ ]
898+
899+ call_args = mock_client .aio .models .generate_content .call_args
900+ final_config = call_args .kwargs ["config" ]
901+ assert final_config .http_options .api_version == "v2"
902+
903+
904+ @pytest .mark .asyncio
905+ async def test_generate_content_async_env_var_does_not_override_custom_client_api_version (
906+ llm_request , generate_content_response , monkeypatch
907+ ):
908+ """The GOOGLE_GENAI_API_VERSION env var does not override client-level custom configuration."""
909+ monkeypatch .setenv ("GOOGLE_GENAI_API_VERSION" , "env-version" )
910+ gemini_llm = Gemini (model = "gemini-2.5-flash" )
911+ llm_request .config .http_options = types .HttpOptions ()
912+
913+ with mock .patch .object (gemini_llm , "api_client" ) as mock_client :
914+
915+ async def mock_coro ():
916+ return generate_content_response
917+
918+ mock_client .aio .models .generate_content .return_value = mock_coro ()
919+
920+ _ = [
921+ resp
922+ async for resp in gemini_llm .generate_content_async (
923+ llm_request , stream = False
924+ )
925+ ]
926+
927+ call_args = mock_client .aio .models .generate_content .call_args
928+ final_config = call_args .kwargs ["config" ]
929+ assert final_config .http_options .api_version is None
930+
931+
791932def test_live_api_version_vertex_ai (gemini_llm ):
792933 """Test that _live_api_version returns 'v1beta1' for Vertex AI backend."""
793934 with mock .patch .object (
@@ -796,6 +937,28 @@ def test_live_api_version_vertex_ai(gemini_llm):
796937 assert gemini_llm ._live_api_version == "v1beta1"
797938
798939
940+ def test_live_api_version_ignores_configured_field ():
941+ """Test that _live_api_version ignores the configured api_version field."""
942+ gemini_llm = Gemini (model = "gemini-2.5-flash" , api_version = "v1" )
943+
944+ with mock .patch .object (
945+ gemini_llm , "_api_backend" , GoogleLLMVariant .VERTEX_AI
946+ ):
947+ assert gemini_llm ._live_api_version == "v1beta1"
948+
949+
950+ def test_live_api_client_ignores_configured_field ():
951+ """Test that _live_api_client http_options ignores the api_version field."""
952+ gemini_llm = Gemini (model = "gemini-2.5-flash" , api_version = "v1" )
953+
954+ with mock .patch .object (
955+ gemini_llm , "_api_backend" , GoogleLLMVariant .VERTEX_AI
956+ ):
957+ client = gemini_llm ._live_api_client
958+
959+ assert client ._api_client ._http_options .api_version == "v1beta1"
960+
961+
799962def test_live_api_version_uses_google_base_url_version ():
800963 gemini_llm = Gemini (
801964 model = "gemini-2.5-flash" ,
0 commit comments