diff --git a/replicate/helpers.py b/replicate/helpers.py index 62c7d67..17579ee 100644 --- a/replicate/helpers.py +++ b/replicate/helpers.py @@ -46,6 +46,8 @@ def encode_json( return base64_encode_file(obj) return client.files.create(obj).urls["get"] if HAS_NUMPY: + if isinstance(obj, np.bool_): # type: ignore + return bool(obj) if isinstance(obj, np.integer): # type: ignore return int(obj) if isinstance(obj, np.floating): # type: ignore @@ -83,6 +85,8 @@ async def async_encode_json( return base64_encode_file(obj) return (await client.files.async_create(obj)).urls["get"] if HAS_NUMPY: + if isinstance(obj, np.bool_): # type: ignore + return bool(obj) if isinstance(obj, np.integer): # type: ignore return int(obj) if isinstance(obj, np.floating): # type: ignore diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 0c41cab..6924d78 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1,8 +1,10 @@ import io +import json import pytest -from replicate.helpers import base64_encode_file +from replicate.client import Client +from replicate.helpers import async_encode_json, base64_encode_file, encode_json @pytest.mark.parametrize( @@ -33,3 +35,17 @@ def test_base64_encode_file(content, filename, expected): # Call the function and check the result result = base64_encode_file(file) assert result == expected + + +@pytest.mark.parametrize("value", [True, False]) +async def test_numpy_boolean_prediction_inputs(value): + np = pytest.importorskip("numpy") + inputs = {"enabled": np.bool_(value), "flags": [np.bool_(value)]} + client = Client() + for payload in ( + encode_json(inputs, client), + await async_encode_json(inputs, client), + ): + decoded = json.loads(json.dumps(payload)) + assert decoded["enabled"] is value + assert decoded["flags"][0] is value