Skip to content

feat: GenAI SDK client - Adding client-based SDKs for Agent Engine #5356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 139 additions & 138 deletions tests/unit/vertex_langchain/test_agent_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from absl.testing import parameterized
import cloudpickle
import difflib
import importlib
Expand Down Expand Up @@ -1147,7 +1146,6 @@ def test_create_agent_engine_with_env_vars_list(
retry=_TEST_RETRY,
)

# pytest does not allow absl.testing.parameterized.named_parameters.
@pytest.mark.parametrize(
"test_case_name, test_engine_instance, expected_framework",
[
Expand Down Expand Up @@ -1190,7 +1188,6 @@ def test_get_agent_framework(
framework = _agent_engines._get_agent_framework(test_engine_instance)
assert framework == expected_framework

# pytest does not allow absl.testing.parameterized.named_parameters.
@pytest.mark.parametrize(
"test_case_name, test_kwargs, want_request",
[
Expand Down Expand Up @@ -1601,7 +1598,6 @@ def test_query_agent_engine(
test_agent_engine.query(query=_TEST_QUERY_PROMPT)
query_mock.assert_called_with(request=_TEST_AGENT_ENGINE_QUERY_REQUEST)

# pytest does not allow absl.testing.parameterized.named_parameters.
@pytest.mark.parametrize(
"test_case_name, test_class_methods_spec, want_operation_schema_api_modes",
[
Expand Down Expand Up @@ -1847,7 +1843,6 @@ def test_query_after_create_agent_engine_with_operation_schema(
class_method=method_name,
)
)
assert invoked_method.__doc__ == test_doc

# pytest does not allow absl.testing.parameterized.named_parameters.
@pytest.mark.parametrize(
Expand Down Expand Up @@ -2062,7 +2057,6 @@ def test_stream_query_after_create_agent_engine_with_operation_schema(
class_method=method_name,
)
)
assert invoked_method.__doc__ == test_doc

# pytest does not allow absl.testing.parameterized.named_parameters.
@pytest.mark.parametrize(
Expand Down Expand Up @@ -2243,7 +2237,6 @@ async def test_async_stream_query_after_create_agent_engine_with_operation_schem
class_method=method_name,
)
)
assert invoked_method.__doc__ == test_doc

# pytest does not allow absl.testing.parameterized.named_parameters.
@pytest.mark.parametrize(
Expand Down Expand Up @@ -2870,7 +2863,7 @@ def test_update_class_methods_spec_with_registered_operation_not_found(self):
"register the API methods: "
"https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/develop/custom#custom-methods. "
"Error: {Unsupported api mode: `UNKNOWN_API_MODE`, "
"Supported modes are: ``, `async`, `stream` and `async_stream`.}"
"Supported modes are: ``, `async`, `async_stream`, `stream`.}"
),
),
],
Expand Down Expand Up @@ -2987,161 +2980,169 @@ def assert_called_with_diff(mock_obj, expected_kwargs=None):
)


class TestGenerateSchema(parameterized.TestCase):
@parameterized.named_parameters(
dict(
testcase_name="place_tool_query",
func=place_tool_query,
required=["city", "activity"],
expected_operation={
"name": "place_tool_query",
"description": (
"Searches the city for recommendations on the activity."
),
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"},
"activity": {"type": "string", "nullable": True},
"page_size": {"type": "integer"},
class TestGenerateSchema:
# pytest does not allow absl.testing.parameterized.named_parameters.
@pytest.mark.parametrize(
"func, required, expected_operation",
[
(
# "place_tool_query",
place_tool_query,
["city", "activity"],
{
"name": "place_tool_query",
"description": (
"Searches the city for recommendations on the activity."
),
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"},
"activity": {"type": "string", "nullable": True},
"page_size": {"type": "integer"},
},
"required": ["city", "activity"],
},
"required": ["city", "activity"],
},
},
),
dict(
testcase_name="place_photo_query",
func=place_photo_query,
required=["photo_reference"],
expected_operation={
"name": "place_photo_query",
"description": "Returns the photo for a given reference.",
"parameters": {
"properties": {
"photo_reference": {"type": "string"},
"maxwidth": {"type": "integer"},
"maxheight": {"type": "integer", "nullable": True},
),
(
# "place_photo_query",
place_photo_query,
["photo_reference"],
{
"name": "place_photo_query",
"description": "Returns the photo for a given reference.",
"parameters": {
"type": "object",
"properties": {
"photo_reference": {"type": "string"},
"maxwidth": {"type": "integer"},
"maxheight": {"type": "integer", "nullable": True},
},
"required": ["photo_reference"],
},
"required": ["photo_reference"],
"type": "object",
},
},
),
),
],
)
def test_generate_schemas(self, func, required, expected_operation):
result = _utils.generate_schema(func, required=required)
self.assertDictEqual(result, expected_operation)
assert result == expected_operation


class TestToProto(parameterized.TestCase):
@parameterized.named_parameters(
dict(
testcase_name="empty_dict",
obj={},
expected_proto=struct_pb2.Struct(fields={}),
),
dict(
testcase_name="nonempty_dict",
obj={"snake_case": 1, "camelCase": 2},
expected_proto=struct_pb2.Struct(
fields={
"snake_case": struct_pb2.Value(number_value=1),
"camelCase": struct_pb2.Value(number_value=2),
},
class TestToProto:
# pytest does not allow absl.testing.parameterized.named_parameters.
@pytest.mark.parametrize(
"obj, expected_proto",
[
(
# "empty_dict",
{},
struct_pb2.Struct(fields={}),
),
),
dict(
testcase_name="empty_proto_message",
obj=struct_pb2.Struct(fields={}),
expected_proto=struct_pb2.Struct(fields={}),
),
dict(
testcase_name="nonempty_proto_message",
obj=struct_pb2.Struct(
fields={
"snake_case": struct_pb2.Value(number_value=1),
"camelCase": struct_pb2.Value(number_value=2),
},
(
# "nonempty_dict",
{"snake_case": 1, "camelCase": 2},
struct_pb2.Struct(
fields={
"snake_case": struct_pb2.Value(number_value=1),
"camelCase": struct_pb2.Value(number_value=2),
},
),
),
expected_proto=struct_pb2.Struct(
fields={
"snake_case": struct_pb2.Value(number_value=1),
"camelCase": struct_pb2.Value(number_value=2),
},
(
# "empty_proto_message",
struct_pb2.Struct(fields={}),
struct_pb2.Struct(fields={}),
),
),
(
# "nonempty_proto_message",
struct_pb2.Struct(
fields={
"snake_case": struct_pb2.Value(number_value=1),
"camelCase": struct_pb2.Value(number_value=2),
},
),
struct_pb2.Struct(
fields={
"snake_case": struct_pb2.Value(number_value=1),
"camelCase": struct_pb2.Value(number_value=2),
},
),
),
],
)
def test_to_proto(self, obj, expected_proto):
result = _utils.to_proto(obj)
self.assertDictEqual(_utils.to_dict(result), _utils.to_dict(expected_proto))
# converting a new object to proto should not modify earlier objects.
new_result = _utils.to_proto({})
self.assertDictEqual(_utils.to_dict(result), _utils.to_dict(expected_proto))
self.assertEmpty(new_result)
assert _utils.to_dict(result) == _utils.to_dict(expected_proto)


class ToParsedJsonTest(parameterized.TestCase):
@parameterized.named_parameters(
dict(
testcase_name="valid_json",
obj=httpbody_pb2.HttpBody(
content_type="application/json", data=b'{"a": 1, "b": "hello"}'
class ToParsedJsonTest:
# pytest does not allow absl.testing.parameterized.named_parameters.
@pytest.mark.parametrize(
"obj, expected",
[
(
# "valid_json",
httpbody_pb2.HttpBody(
content_type="application/json", data=b'{"a": 1, "b": "hello"}'
),
[{"a": 1, "b": "hello"}],
),
expected=[{"a": 1, "b": "hello"}],
),
dict(
testcase_name="invalid_json",
obj=httpbody_pb2.HttpBody(
content_type="application/json", data=b'{"a": 1, "b": "hello"'
(
# "invalid_json",
httpbody_pb2.HttpBody(
content_type="application/json", data=b'{"a": 1, "b": "hello"'
),
['{"a": 1, "b": "hello"'], # returns the unparsed string
),
expected=['{"a": 1, "b": "hello"'], # returns the unparsed string
),
dict(
testcase_name="missing_content_type",
obj=httpbody_pb2.HttpBody(data=b'{"a": 1}'),
expected=[httpbody_pb2.HttpBody(data=b'{"a": 1}')],
),
dict(
testcase_name="missing_data",
obj=httpbody_pb2.HttpBody(content_type="application/json"),
expected=[None],
),
dict(
testcase_name="wrong_content_type",
obj=httpbody_pb2.HttpBody(content_type="text/plain", data=b"hello"),
expected=[httpbody_pb2.HttpBody(content_type="text/plain", data=b"hello")],
),
dict(
testcase_name="empty_data",
obj=httpbody_pb2.HttpBody(content_type="application/json", data=b""),
expected=[None],
),
dict(
testcase_name="unicode_data",
obj=httpbody_pb2.HttpBody(
content_type="application/json", data='{"a": "你好"}'.encode("utf-8")
(
# "missing_content_type",
httpbody_pb2.HttpBody(data=b'{"a": 1}'),
[httpbody_pb2.HttpBody(data=b'{"a": 1}')],
),
expected=[{"a": "你好"}],
),
dict(
testcase_name="nested_json",
obj=httpbody_pb2.HttpBody(
content_type="application/json", data=b'{"a": {"b": 1}}'
(
# "missing_data",
httpbody_pb2.HttpBody(content_type="application/json"),
[None],
),
expected=[{"a": {"b": 1}}],
),
dict(
testcase_name="multiline_json",
obj=httpbody_pb2.HttpBody(
content_type="application/json",
data=b'{"a": {"b": 1}}\n{"a": {"b": 2}}',
(
# "wrong_content_type",
httpbody_pb2.HttpBody(content_type="text/plain", data=b"hello"),
[httpbody_pb2.HttpBody(content_type="text/plain", data=b"hello")],
),
expected=[{"a": {"b": 1}}, {"a": {"b": 2}}],
),
(
# "empty_data",
httpbody_pb2.HttpBody(content_type="application/json", data=b""),
[None],
),
(
# "unicode_data",
httpbody_pb2.HttpBody(
content_type="application/json", data='{"a": "你好"}'.encode("utf-8")
),
[{"a": "你好"}],
),
(
# "nested_json",
httpbody_pb2.HttpBody(
content_type="application/json", data=b'{"a": {"b": 1}}'
),
[{"a": {"b": 1}}],
),
(
# "multiline_json",
httpbody_pb2.HttpBody(
content_type="application/json",
data=b'{"a": {"b": 1}}\n{"a": {"b": 2}}',
),
[{"a": {"b": 1}}, {"a": {"b": 2}}],
),
],
)
def test_to_parsed_json(self, obj, expected):
for got, want in zip(_utils.yield_parsed_json(obj), expected):
self.assertEqual(got, want)
assert got == want


class TestRequirements:
Expand Down
Loading