Skip to content

Commit a03e323

Browse files
Rodrigo-Palmantkathole
authored andcommitted
fix: Infer the tensor type from retrieved values in to_tensor
to_tensor decides between a torch.Tensor and a plain list by looking at the first value that is not None, but it searches the list where the missing values have already been replaced by default_value. That default is float('nan') by default, which is not None, so the search always returns the first element. When the first entity row is missing, the type is then read off the default rather than off the feature. A string feature whose first row is absent, the ordinary case of an entity key that is not in the online store, raises 'Failed to convert values for ... to tensor: new(): invalid data type' instead of returning the list the docstring promises. Reordering the entity rows makes the error disappear. Read the first present value from the retrieved list. The fallback stays default_value so that a fully missing numeric feature keeps taking the tensor path, which the added regression test pins. Signed-off-by: Rodrigo-Palma <[email protected]>
1 parent f9dcfb5 commit a03e323

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎sdk/python/feast/online_response.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,11 @@ def to_tensor(
134134
tensor_dict: Dict[str, Union[TorchTensor, List[Any]]] = {}
135135
for key in feature_keys:
136136
raw_values = feature_dict[key]
137+
# Infer the feature type from the retrieved values, not from the
138+
# substituted ones: default_value is a stand-in for a missing entity
139+
# and says nothing about the feature's actual type.
140+
first_valid = next((v for v in raw_values if v is not None), default_value)
137141
values = [v if v is not None else default_value for v in raw_values]
138-
first_valid = next((v for v in values if v is not None), None)
139142
if isinstance(first_valid, (int, float, bool)):
140143
try:
141144
device = "cuda" if torch.cuda.is_available() else "cpu"
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2025 The Feast Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import math
16+
17+
from feast.online_response import OnlineResponse
18+
from feast.protos.feast.serving.ServingService_pb2 import GetOnlineFeaturesResponse
19+
from feast.protos.feast.types.Value_pb2 import Value
20+
21+
22+
def _response(feature_name, values):
23+
resp = GetOnlineFeaturesResponse()
24+
resp.metadata.feature_names.val.extend([feature_name])
25+
vector = resp.results.add()
26+
for v in values:
27+
vector.values.append(v)
28+
return OnlineResponse(resp)
29+
30+
31+
def test_to_tensor_string_feature_with_missing_first_row():
32+
# driver 0 is not in the online store -> null; driver 1 has a name.
33+
response = _response("name", [Value(), Value(string_val="John")])
34+
35+
assert response.to_dict() == {"name": [None, "John"]}
36+
37+
tensors = response.to_tensor()
38+
39+
assert isinstance(tensors["name"], list)
40+
assert math.isnan(tensors["name"][0])
41+
assert tensors["name"][1] == "John"
42+
43+
44+
def test_to_tensor_numeric_feature_all_missing_still_tensor():
45+
response = _response("trips", [Value(), Value()])
46+
47+
tensors = response.to_tensor()
48+
49+
# numeric-looking default keeps the tensor path (regression guard)
50+
assert hasattr(tensors["trips"], "shape")
51+
assert tensors["trips"].shape[0] == 2

0 commit comments

Comments
 (0)