The simplest API that expects an image and a strin...
# ask-for-help
t
The simplest API that expects an image and a string fails when provided a string with a number in it. Sample API:
Copy code
@bentoml.service
class Service:
    @bentoml.api
    def classify(self, input_image: PILImage.Image, uid: str) -> Any:
        print(uid)
Call it in the swagger docs with any number and it fails. Call it with curl with the uid being an explicit string and it still fails.
Copy code
curl -X 'POST' \
  '<http://localhost:3000/classify>' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'input_image=@sample_image.png;type=image/png' \
  -F 'uid="1231231231232131313212312312321123123213312";type=application/json'
How do I fix that so it sees it as an actual string in all cases, no typecasting cutting off decimals etc?
j
what is the returned error?
t
Copy code
File "/home/tokereines/unumed/xray/.venv/lib/python3.10/site-packages/_bentoml_impl/server/app.py", line 642, in api_endpoint_wrapper
    resp = await self.api_endpoint(name, request)
  File "/home/tokereines/unumed/xray/.venv/lib/python3.10/site-packages/_bentoml_impl/server/app.py", line 706, in api_endpoint
    input_data = await method.input_spec.from_http_request(request, serde)
  File "/home/tokereines/unumed/xray/.venv/lib/python3.10/site-packages/_bentoml_sdk/io_models.py", line 213, in from_http_request
    return await serde.parse_request(request, t.cast(t.Type[IODescriptor], cls))
  File "/home/tokereines/unumed/xray/.venv/lib/python3.10/site-packages/_bentoml_impl/serde.py", line 227, in parse_request
    return cls.model_validate(data)
  File "/home/tokereines/unumed/xray/.venv/lib/python3.10/site-packages/pydantic/main.py", line 596, in model_validate
    return cls.__pydantic_validator__.validate_python(
pydantic_core._pydantic_core.ValidationError: 1 validation error for Input
uid
  Input should be a valid string [type=string_type, input_value=1231231231232131313212312312321123123213312, input_type=int]
    For further information visit <https://errors.pydantic.dev/2.9/v/string_type>
Any idea?
j
let me discuss this with the team
👍 1
the issue lies in curl.. not on bentoml.
Copy code
curl -X 'POST' \
  '<http://localhost:3000/classify>' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'input_image=@sample_image.png;type=image/png' \
  -F 'uid="\"123456\"";type=application/json'
this works
t
So the swagger docs is also broken 😕 Sucks that one has to add escape characters even with python requests it seems.
But ty for resolving
f
what is the curl command if you wrap the string with double quotes in the input box