Skip to content

Commit e7769c0

Browse files
authored
Merge pull request #50 from CMCC-Foundation/version-2025.06
datastore hot fix for numpy object that cannot be serialized by fastapi
2 parents 642bf55 + ea08a63 commit e7769c0

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

datastore/datastore/datastore.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77

88
import intake
9+
import numpy as np
910
from dask.delayed import Delayed
1011
from geokube import GeogCS
1112

@@ -206,6 +207,15 @@ def product_metadata(self, dataset_id: str, product_id: str):
206207
product_id
207208
].metadata
208209

210+
def _convert_numpy(self,obj):
211+
if isinstance(obj, dict):
212+
return {k: self._convert_numpy(v) for k, v in obj.items()}
213+
elif isinstance(obj, list):
214+
return [self._convert_numpy(i) for i in obj]
215+
elif isinstance(obj, np.generic):
216+
return obj.item()
217+
return obj
218+
209219
@log_execution_time(_LOG)
210220
def first_eligible_product_details(
211221
self,
@@ -256,6 +266,7 @@ def first_eligible_product_details(
256266
).to_dict()
257267
else:
258268
info["data"] = entry.read_chunked().to_dict()
269+
info = self._convert_numpy(info)
259270
return info
260271
raise UnauthorizedError()
261272

@@ -323,6 +334,7 @@ def product_info(
323334
).to_dict()
324335
else:
325336
info["data"] = entry.read_chunked().to_dict()
337+
info = self._convert_numpy(info)
326338
return info
327339

328340
@log_execution_time(_LOG)

0 commit comments

Comments
 (0)