TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
The NaN (Not a Number) value is used in Numpy and other scientific libraries to describe an invalid or missing value (e.g. a division by zero). In some scenarios, it may be desirable to let your models receive and / or output NaN values (e.g. these can be useful sometimes with GBTs, like XGBoost models). This is why MLServer supports encoding NaN values on your request / response payloads under some conditions.
In order to send / receive NaN values, you must ensure that:
- You are using the `REST` interface. - The input / output entry containing NaN values uses either the `FP16`, `FP32` or `FP64` datatypes. - You are either using the [Pandas codec](#pandas-dataframe) or the [Numpy codec](#numpy-array).
Assuming those conditions are satisfied, any `null` value within your tensor payload will be converted to NaN.
For example, if you take the following Numpy array:
import numpy as np import pandas as pd from mlserver.codecs import PandasCodec from mlserver.codecs.numpy import to_datatype from mlserver.codecs.pandas import _process_bytes from mlserver.codecs.utils import inject_batch_dimension from mlserver.types import InferenceRequest, Parameters, RequestInput, ResponseOutput, Datatype
def convert_nan(val): try: if np.isnan(val): return None except Exception: return val return val