py3langid icon indicating copy to clipboard operation
py3langid copied to clipboard

fix error with float32 error with json.dumps in server mode

Open Aprilistic opened this issue 6 months ago • 1 comments

Hi there!

I found that when I use langid in server mode langid -s, and send a request curl -d "q=This is a test" localhost:9008/detect I get an error with json.dumps because of its compatibility with float32.

This error also happens with the original langid code but the repo hasn't been updated for a while.

So, I added a custom encoder to prevent the error, which works well now. You can find how I changed it in this commit log. https://github.com/adbar/py3langid/compare/master...Aprilistic:py3langid:master

Error log File "/opt/homebrew/Caskroom/miniforge/base/envs/test/lib/python3.12/site-packages/py3langid/langid.py", line 352, in application return [json.dumps(response)] ^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Caskroom/miniforge/base/envs/test/lib/python3.12/json/__init__.py", line 231, in dumps return _default_encoder.encode(obj) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Caskroom/miniforge/base/envs/test/lib/python3.12/json/encoder.py", line 200, in encode chunks = self.iterencode(o, _one_shot=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Caskroom/miniforge/base/envs/test/lib/python3.12/json/encoder.py", line 258, in iterencode return _iterencode(o, 0) ^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Caskroom/miniforge/base/envs/test/lib/python3.12/json/encoder.py", line 180, in default raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type float32 is not JSON serializable

Aprilistic avatar Aug 16 '24 14:08 Aprilistic