Skip to content

Commit 99fafe9

Browse files
yeesiancopybara-github
authored andcommitted
fix: implementation of proto conversion in reasoning engine utils when message is not specified
PiperOrigin-RevId: 697742665
1 parent 315e765 commit 99fafe9

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

tests/unit/vertex_langchain/test_reasoning_engines.py

+48
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from vertexai.reasoning_engines import _reasoning_engines
3939
from vertexai.reasoning_engines import _utils
4040
from google.protobuf import field_mask_pb2
41+
from google.protobuf import struct_pb2
4142

4243

4344
class CapitalizeEngine:
@@ -1081,3 +1082,50 @@ class TestGenerateSchema(parameterized.TestCase):
10811082
def test_generate_schemas(self, func, required, expected_operation):
10821083
result = _utils.generate_schema(func, required=required)
10831084
self.assertDictEqual(result, expected_operation)
1085+
1086+
1087+
class TestToProto(parameterized.TestCase):
1088+
@parameterized.named_parameters(
1089+
dict(
1090+
testcase_name="empty_dict",
1091+
obj={},
1092+
expected_proto=struct_pb2.Struct(fields={}),
1093+
),
1094+
dict(
1095+
testcase_name="nonempty_dict",
1096+
obj={"a": 1, "b": 2},
1097+
expected_proto=struct_pb2.Struct(
1098+
fields={
1099+
"a": struct_pb2.Value(number_value=1),
1100+
"b": struct_pb2.Value(number_value=2),
1101+
},
1102+
),
1103+
),
1104+
dict(
1105+
testcase_name="empty_proto_message",
1106+
obj=struct_pb2.Struct(fields={}),
1107+
expected_proto=struct_pb2.Struct(fields={}),
1108+
),
1109+
dict(
1110+
testcase_name="nonempty_proto_message",
1111+
obj=struct_pb2.Struct(
1112+
fields={
1113+
"a": struct_pb2.Value(number_value=1),
1114+
"b": struct_pb2.Value(number_value=2),
1115+
},
1116+
),
1117+
expected_proto=struct_pb2.Struct(
1118+
fields={
1119+
"a": struct_pb2.Value(number_value=1),
1120+
"b": struct_pb2.Value(number_value=2),
1121+
},
1122+
),
1123+
),
1124+
)
1125+
def test_to_proto(self, obj, expected_proto):
1126+
result = _utils.to_proto(obj)
1127+
self.assertDictEqual(_utils.to_dict(result), _utils.to_dict(expected_proto))
1128+
# converting a new object to proto should not modify earlier objects.
1129+
new_result = _utils.to_proto({})
1130+
self.assertDictEqual(_utils.to_dict(result), _utils.to_dict(expected_proto))
1131+
self.assertEmpty(new_result)

vertexai/reasoning_engines/_utils.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
def to_proto(
4444
obj: Union[JsonDict, proto.Message],
45-
message: proto.Message = struct_pb2.Struct(),
45+
message: Optional[proto.Message] = None,
4646
) -> proto.Message:
4747
"""Parses a JSON-like object into a message.
4848
@@ -60,7 +60,9 @@ def to_proto(
6060
Returns:
6161
proto.Message: The same message passed as argument.
6262
"""
63-
if isinstance(obj, proto.Message):
63+
if message is None:
64+
message = struct_pb2.Struct()
65+
if isinstance(obj, (proto.Message, struct_pb2.Struct)):
6466
return obj
6567
try:
6668
json_format.ParseDict(obj, message._pb)

0 commit comments

Comments
 (0)