Replies: 1 comment 1 reply
-
The issue you're encountering relates to how BentoML handles numpy string arrays and their dtypes, particularly when using Unicode strings (dtype Numpy string types ( Solution Steps:
Example Fix:Here’s how you can modify your import bentoml
from bentoml.io import NumpyNdarray, JSON
import numpy as np
svc = bentoml.Service("example_bento_service")
@svc.api(input=JSON(), output=NumpyNdarray())
def predict(input_array: dict[int, str]) -> np.ndarray:
# Set a fixed width for the Unicode strings
data = [["zhang", "san"], ["li", "si"]]
res = np.array(data, dtype="<U5") # Specify character width of 5
return res Alternatively, if you want to stick to variable-length strings, you can serialize the Numpy array to a simpler format like JSON: @svc.api(input=JSON(), output=JSON())
def predict(input_array: dict[int, str]) -> dict:
# Use a list of lists to return data as JSON
data = [["zhang", "san"], ["li", "si"]]
return {"result": data} In this case, you would change the output descriptor from Additional Note:Ensure your input/output formats match between the API definition ( |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The validation of the string type in
bentoml._internal.io_descriptors.numpy
within the provided_to_proto_impl
function fails. The mapping relationship provided byFIELDPB_TO_NPDTYPE_NAME_MAP
is"string_value" -> "<U"
, but doesn't the dtype in numpy include character width? Therefore, in this piece of code:an exception is always thrown. My usage is as follows:
the version is
bentoml==1.3.3
and my test starting server command:Beta Was this translation helpful? Give feedback.
All reactions