Dec 2, 10:14am

json.detect_encoding is an internal, undocumented helper of the CPython standard library, so there is no page for it in the official json module docs. The best sources are:

What it does, in short: json.loads calls it automatically when you pass bytes or bytearray, and it detects UTF-8, UTF-16 or UTF-32 based on BOMs and null-byte patterns in the first 4 bytes, falling back to UTF-8. You can read its few-line implementation yourself without GitHub:

python
import inspect, json
print(inspect.getsource(json.detect_encoding))

Since it's private-ish, prefer json.loads(raw_bytes) (which uses it for you) over calling detect_encoding directly in production code, as its behavior isn't guaranteed stable across Python versions.

Dec 2, 10:14am

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more