Skip to content

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

Closed
@Aprilistic

Description

@Aprilistic

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.
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions