#numpy.int64 causes msgspec error

1 messages · Page 1 of 1 (latest)

heavy aurora
#

When my pandas dataframes end up with numpy.int64 integers, it causes this error:

/msgspec_hooks.py", line 164, in encode_json
    raise SerializationException(str(msgspec_error)) from msgspec_error
litestar.exceptions.base_exceptions.SerializationException: Unsupported type: <class 'numpy.int64'>

I saw a related issue was fixed in 2023:
https://github.com/litestar-org/litestar/issues/1544

I'm not sure if the numpy.int64 is expected to work, so just wanted to ask here first, happy to help turn this into an issue if needed.

lime daggerBOT
#
Notes for numpy.int64 causes msgspec error
At your assistance

@heavy aurora

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

idle boneBOT
# heavy aurora When my pandas dataframes end up with numpy.int64 integers, it causes this error...

Summary

when I try in v2.0.0alpha4

@get("/")
async def hello_world() -> dict:
    bad_dict = {"value": 1000000000000000000000000000000000000000000000000}
    return bad_dict

OverflowError: can't serialize ints < -2**63 or > 2**64 - 1

I managed to find a temporary workaround

from fastapi.encoders import jsonable_encoder
...
return jsonable_encoder(bad_dict, custom_encoder={int: lambda x: str(x)})

I think it should be really prominent in the docs (parts like "Migrating to Litestar") that you can't trust arbitrary python dicts and you can't just
return httpx.get(..).json()
in your endpoints because Litestar uses untraditional serializer for json

Labels

Bug :bug:, Upstream