|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2026 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +"""Unit tests for `vertexai.types` resolving against `agentplatform.types`.""" |
| 18 | + |
| 19 | +import sys |
| 20 | + |
| 21 | +import agentplatform |
| 22 | +import vertexai |
| 23 | +from agentplatform._genai import types as agentplatform_types |
| 24 | +from vertexai._genai import types as legacy_types |
| 25 | +import pytest |
| 26 | + |
| 27 | +# Both modules resolve these out of `_evals_metric_loaders` on first access, so |
| 28 | +# they are checked on their own rather than in the bulk comparisons below. |
| 29 | +_LAZY_NAMES = frozenset({"PrebuiltMetric", "RubricMetric"}) |
| 30 | + |
| 31 | +_SHARED_NAMES = sorted( |
| 32 | + (set(agentplatform_types.__all__) & set(legacy_types.__all__)) - _LAZY_NAMES |
| 33 | +) |
| 34 | +_LEGACY_ONLY_NAMES = sorted( |
| 35 | + set(legacy_types.__all__) - set(agentplatform_types.__all__) |
| 36 | +) |
| 37 | +_AGENT_PLATFORM_ONLY_NAMES = sorted( |
| 38 | + set(agentplatform_types.__all__) - set(legacy_types.__all__) |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +def test_vertexai_types_is_the_alias_module(): |
| 43 | + assert sys.modules[vertexai.types.__name__] is vertexai.types |
| 44 | + assert vertexai.types is not legacy_types |
| 45 | + assert vertexai.types is not agentplatform_types |
| 46 | + |
| 47 | + |
| 48 | +def test_shared_names_are_the_agentplatform_objects(): |
| 49 | + """The point of the alias: one class per message rather than two.""" |
| 50 | + assert _SHARED_NAMES, "expected the two modules to share generated types" |
| 51 | + mismatched = [ |
| 52 | + name |
| 53 | + for name in _SHARED_NAMES |
| 54 | + if getattr(vertexai.types, name) is not getattr(agentplatform_types, name) |
| 55 | + ] |
| 56 | + assert not mismatched |
| 57 | + |
| 58 | + |
| 59 | +def test_memory_profile_survives_an_isinstance_check(): |
| 60 | + """Regression: an agentplatform object checked against the vertexai name.""" |
| 61 | + profile = agentplatform_types.MemoryProfile(schema_id="user-profile", profile={}) |
| 62 | + assert isinstance(profile, vertexai.types.MemoryProfile) |
| 63 | + |
| 64 | + |
| 65 | +def test_shared_types_still_nest_inside_a_legacy_only_model(): |
| 66 | + """The two halves have to interoperate, not just coexist. |
| 67 | +
|
| 68 | + `GenerateAgentEngineMemoriesConfig` stays a `vertexai` class because |
| 69 | + agentplatform renamed it, but its `metadata` values are now agentplatform |
| 70 | + objects. Pydantic accepts them because both models derive from the genai |
| 71 | + `BaseModel`, which sets `from_attributes=True`. |
| 72 | + """ |
| 73 | + config = vertexai.types.GenerateAgentEngineMemoriesConfig( |
| 74 | + metadata={"record": vertexai.types.MemoryMetadataValue(string_value="123")} |
| 75 | + ) |
| 76 | + assert type(config) is legacy_types.GenerateAgentEngineMemoriesConfig |
| 77 | + assert config.metadata["record"].string_value == "123" |
| 78 | + |
| 79 | + |
| 80 | +def test_legacy_only_names_still_resolve(): |
| 81 | + """The `AgentEngine*` surface agentplatform renamed is not taken away.""" |
| 82 | + assert "AgentEngine" in _LEGACY_ONLY_NAMES |
| 83 | + mismatched = [ |
| 84 | + name |
| 85 | + for name in _LEGACY_ONLY_NAMES |
| 86 | + if getattr(vertexai.types, name) is not getattr(legacy_types, name) |
| 87 | + ] |
| 88 | + assert not mismatched |
| 89 | + |
| 90 | + |
| 91 | +def test_agentplatform_only_names_are_reachable(): |
| 92 | + assert "Runtime" in _AGENT_PLATFORM_ONLY_NAMES |
| 93 | + mismatched = [ |
| 94 | + name |
| 95 | + for name in _AGENT_PLATFORM_ONLY_NAMES |
| 96 | + if getattr(vertexai.types, name) is not getattr(agentplatform_types, name) |
| 97 | + ] |
| 98 | + assert not mismatched |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize("name", sorted(_LAZY_NAMES)) |
| 102 | +def test_lazily_loaded_metric_names_resolve_to_agentplatform(name): |
| 103 | + assert getattr(vertexai.types, name) is getattr(agentplatform_types, name) |
| 104 | + |
| 105 | + |
| 106 | +def test_all_is_the_union_of_both_modules(): |
| 107 | + expected = set(agentplatform_types.__all__) | set(legacy_types.__all__) |
| 108 | + assert set(vertexai.types.__all__) == expected |
| 109 | + assert set(dir(vertexai.types)) == expected |
| 110 | + |
| 111 | + |
| 112 | +def test_unknown_name_still_raises_attribute_error(): |
| 113 | + with pytest.raises(AttributeError): |
| 114 | + _ = vertexai.types.NoSuchTypeName |
| 115 | + |
| 116 | + |
| 117 | +def test_agentplatform_registers_its_own_types_alias(): |
| 118 | + """`agentplatform.types` must no longer claim the `vertexai.types` key.""" |
| 119 | + assert agentplatform.types is agentplatform_types |
| 120 | + assert sys.modules[f"{agentplatform.__name__}.types"] is agentplatform_types |
| 121 | + assert sys.modules[f"{vertexai.__name__}.types"] is not agentplatform_types |
0 commit comments