From 2b7f569134e38b0d22508a20accdc9bc500125d7 Mon Sep 17 00:00:00 2001 From: Twiink Date: Fri, 27 Nov 2026 13:36:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(inputs):=20=E5=B0=86=E5=B8=83=E5=B0=94?= =?UTF-8?q?=E6=A0=87=E9=87=8F=E7=BC=96=E7=A0=81=E4=B8=BA=E9=A2=84=E6=B5=8B?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E7=9A=84=E5=B8=83=E5=B0=94=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Twiink --- replicate/helpers.py | 4 ++++ tests/test_helpers.py | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/replicate/helpers.py b/replicate/helpers.py index 62c7d67a..17579ee2 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 0c41cab7..6924d78d 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