Skip to content

Commit 211ecb8

Browse files
authored
fix: Encode non-finite floats in feature server JSON responses (feast-dev#6930)
* fix: Encode non-finite floats in feature server JSON responses A stored NaN or +/-inf float made /get-online-features (and the other endpoints that use convert_response_to_dict) fail with HTTP 500, because JSONResponse serializes with allow_nan=False. Encode non-finite values in float/double scalars, lists and sets as the protobuf JSON strings "NaN", "Infinity" and "-Infinity". Signed-off-by: Edson <[email protected]> * perf: Materialize float collections once before the non-finite check Copy the repeated field into a list once and run the finite check and the conversion on that list, instead of iterating the protobuf container for the check and again for the conversion. Signed-off-by: Edson <[email protected]> * test: Restore CRLF line endings in test_feature_server.py The earlier commit converted the file from CRLF to LF, which made the whole file show up as changed. Only the line endings change here. Signed-off-by: Edson <[email protected]> --------- Signed-off-by: Edson <[email protected]>
1 parent 8cfe891 commit 211ecb8

3 files changed

Lines changed: 141 additions & 2 deletions

File tree

‎sdk/python/feast/feature_server_utils.py‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
import base64
88
import logging
9+
import math
910
from datetime import datetime, timezone
10-
from typing import Any, Dict, Optional
11+
from typing import Any, Dict, Optional, Union
1112

1213
from feast.protos.feast.serving.ServingService_pb2 import GetOnlineFeaturesResponse
1314
from feast.protos.feast.types.Value_pb2 import Value
@@ -23,18 +24,29 @@
2324
4: "OUTSIDE_MAX_AGE",
2425
}
2526

27+
_FLOAT_SCALAR_FIELDS = ("double_val", "float_val")
28+
_FLOAT_COLLECTION_FIELDS = (
29+
"double_list_val",
30+
"float_list_val",
31+
"double_set_val",
32+
"float_set_val",
33+
)
34+
2635

2736
def convert_response_to_dict(response: GetOnlineFeaturesResponse) -> Dict[str, Any]:
2837
"""Convert GetOnlineFeaturesResponse to a JSON-serializable dict.
2938
3039
Matches the structure produced by MessageToDict(proto, preserving_proto_field_name=True)
31-
with proto_json.patch() applied, with one intentional difference:
40+
with proto_json.patch() applied, with two intentional differences:
3241
3342
- double_val fields are returned as Python float objects (json.dumps uses Python 3.1+
3443
shortest round-trip form, ~15-17 sig digits) rather than 18 fixed significant digits
3544
(float_precision=18). Values are numerically identical; only the JSON string length
3645
may differ. This is safe for all ML feature types and avoids unnecessary precision
3746
overhead.
47+
- Non-finite float values (NaN, +/-inf) are returned as the strings "NaN",
48+
"Infinity" and "-Infinity" (the protobuf JSON mapping), because they are not
49+
valid JSON numbers and would make JSONResponse fail the whole response.
3850
"""
3951
result: Dict[str, Any] = {
4052
"results": [
@@ -77,6 +89,15 @@ def _value_to_native(v: Value) -> Optional[Any]:
7789
which = v.WhichOneof("val")
7890
if which is None or which == "null_val":
7991
return None
92+
# NaN/Infinity are not valid JSON numbers; JSONResponse (allow_nan=False)
93+
# would fail the whole response, so encode them as protobuf JSON does.
94+
elif which in _FLOAT_SCALAR_FIELDS:
95+
return _float_to_native(getattr(v, which))
96+
elif which in _FLOAT_COLLECTION_FIELDS:
97+
vals = list(getattr(v, which).val)
98+
if all(map(math.isfinite, vals)):
99+
return vals
100+
return [_float_to_native(f) for f in vals]
80101
# bytes must be base64-encoded for JSON serialization
81102
elif which == "bytes_val":
82103
return base64.b64encode(v.bytes_val).decode("ascii")
@@ -114,6 +135,19 @@ def _value_to_native(v: Value) -> Optional[Any]:
114135
return getattr(v, which)
115136

116137

138+
def _float_to_native(f: float) -> Union[float, str]:
139+
"""Return a finite float unchanged, or its protobuf JSON string form.
140+
141+
The protobuf JSON mapping encodes non-finite floats as the strings
142+
"NaN", "Infinity" and "-Infinity", which keeps the response valid JSON.
143+
"""
144+
if math.isfinite(f):
145+
return f
146+
if math.isnan(f):
147+
return "NaN"
148+
return "Infinity" if f > 0 else "-Infinity"
149+
150+
117151
def _zoned_timestamp_to_str(zoned) -> Optional[str]:
118152
"""Convert a ZonedTimestamp proto to an ISO 8601 string in its stored zone.
119153

‎sdk/python/tests/unit/test_feature_server.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,3 +782,56 @@ def test_get_online_features_returns_feature_view_version_metadata(test_client):
782782
assert [m["name"] for m in metadata["feature_view_metadata"]] == [
783783
"pushed_driver_locations"
784784
]
785+
786+
787+
@pytest.fixture
788+
def store_and_client():
789+
runner = CliRunner()
790+
with runner.local_repo(
791+
get_example_repo("example_feature_repo_1.py"), "file"
792+
) as store:
793+
# raise_server_exceptions=False so the app's own exception handler
794+
# answers the request, exactly as a real client would observe it.
795+
yield store, TestClient(get_app(store), raise_server_exceptions=False)
796+
797+
798+
@pytest.mark.parametrize("non_finite", [float("inf"), float("-inf")])
799+
def test_get_online_features_serves_non_finite_float(store_and_client, non_finite):
800+
"""A stored +/-inf float value must not turn the whole REST response into a 500.
801+
802+
``write_to_online_store`` accepts the value and the SDK path returns it,
803+
so the REST feature server must answer the same request with HTTP 200.
804+
"""
805+
import pandas as pd
806+
807+
store, client = store_and_client
808+
now = _utc_now()
809+
store.write_to_online_store(
810+
"pushed_driver_locations",
811+
pd.DataFrame(
812+
{
813+
"driver_id": [123],
814+
"driver_lat": [non_finite],
815+
"driver_long": ["42.0"],
816+
"event_timestamp": [now],
817+
"created_timestamp": [now],
818+
}
819+
),
820+
)
821+
822+
# Precondition: the Python SDK serves the value without complaint.
823+
sdk_resp = store.get_online_features(
824+
features=["pushed_driver_locations:driver_lat"],
825+
entity_rows=[{"driver_id": 123}],
826+
).to_dict()
827+
assert sdk_resp["driver_lat"] == [non_finite]
828+
829+
response = client.post("/get-online-features", json=get_online_features_body())
830+
831+
assert response.status_code == 200, response.text
832+
body = response.json()
833+
ix = body["metadata"]["feature_names"].index("driver_lat")
834+
assert body["results"][ix]["statuses"] == ["PRESENT"]
835+
assert body["results"][ix]["values"] == [
836+
"Infinity" if non_finite > 0 else "-Infinity"
837+
]

‎sdk/python/tests/unit/test_feature_server_utils.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
BoolList,
4747
BytesList,
4848
DoubleList,
49+
DoubleSet,
4950
FloatList,
51+
FloatSet,
5052
Int32List,
5153
Int64List,
5254
Int64Set,
@@ -118,6 +120,36 @@ def test_float_list_val(self):
118120
result = _value_to_native(v)
119121
assert result == [1.5, 2.5]
120122

123+
@pytest.mark.parametrize("field", ["double_val", "float_val"])
124+
@pytest.mark.parametrize(
125+
"value, expected",
126+
[
127+
(float("inf"), "Infinity"),
128+
(float("-inf"), "-Infinity"),
129+
(float("nan"), "NaN"),
130+
],
131+
)
132+
def test_non_finite_scalar_val(self, field, value, expected):
133+
v = Value(**{field: value})
134+
result = _value_to_native(v)
135+
assert result == expected
136+
137+
@pytest.mark.parametrize(
138+
"field, container",
139+
[
140+
("double_list_val", DoubleList),
141+
("float_list_val", FloatList),
142+
("double_set_val", DoubleSet),
143+
("float_set_val", FloatSet),
144+
],
145+
)
146+
def test_non_finite_float_collection_val(self, field, container):
147+
v = Value(
148+
**{field: container(val=[1.5, float("nan"), float("inf"), float("-inf")])}
149+
)
150+
result = _value_to_native(v)
151+
assert result == [1.5, "NaN", "Infinity", "-Infinity"]
152+
121153
def test_int64_list_val(self):
122154
v = Value(int64_list_val=Int64List(val=[100, 200, 300]))
123155
result = _value_to_native(v)
@@ -572,6 +604,26 @@ def test_complex_types_are_json_serializable(self):
572604
serialized = json.dumps(result)
573605
assert serialized # non-empty
574606

607+
def test_non_finite_floats_are_json_serializable(self):
608+
"""NaN/inf must not break strict JSON encoding (JSONResponse uses allow_nan=False)."""
609+
response = GetOnlineFeaturesResponse()
610+
fv = response.results.add()
611+
fv.values.append(Value(double_val=float("inf")))
612+
fv.values.append(Value(float_val=float("-inf")))
613+
fv.values.append(Value(double_list_val=DoubleList(val=[1.0, float("nan")])))
614+
fv.statuses.extend(
615+
[FieldStatus.PRESENT, FieldStatus.PRESENT, FieldStatus.PRESENT]
616+
)
617+
618+
result = convert_response_to_dict(response)
619+
# must not raise
620+
serialized = json.dumps(result, allow_nan=False)
621+
assert json.loads(serialized)["results"][0]["values"] == [
622+
"Infinity",
623+
"-Infinity",
624+
[1.0, "NaN"],
625+
]
626+
575627

576628
class TestProtobufCompatibility:
577629
"""Regression tests for protobuf >= 7.34.0 compatibility (issue #6435).

0 commit comments

Comments
 (0)