chardet 7: a 0BSD encoding detector that replaced its own engine
Python character encoding detector
At a glance
- What is it?
- chardet 7 is a ground-up rewrite of the Python character encoding detector, keeping the same detect(), detect_all(), UniversalDetector and chardetect CLI surface while changing the licence to 0BSD. The README's own numbers put it at 99.7% accuracy across 3,138 test files, but the interesting part is what the rewrite does to candidate ranking.
- Who is it for?
- Adopt chardet 7 if you already call detect(), detect_all() or UniversalDetector and want 0BSD terms plus language and MIME type fields in the result dict; the README states the public API is unchanged, so the migration is mostly a version bump. Do not adopt it as a security control, and do not reach for it when you control both ends of the pipe, because the correct move there is to write UTF-8 and skip detection entirely.
- Can I use it commercially?
- Yes. 0BSD is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 4 days ago.
- What is it written in?
- Mainly Python, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 24, 2026, and from our analysis. They are not legal advice.
Star history
- chardet/chardet2,687 ★ · +14
| Repository | 2026-09-29 | 2026-11-24 |
|---|---|---|
| chardet/chardet | 2.7k | 2.7k |
14 Stars in the last 56 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
The problem chardet solves, and who actually has it
Byte streams do not carry their encoding. A file that starts 0xE6 0x97 0xA5 is three UTF-8 bytes for a Japanese character, or two characters in a legacy single-byte code page, or garbage. Something has to guess before you can call .decode(). chardet is that guess, exposed as a Python function.
The people who need it are the ones parsing files they did not create: log ingestion, CSV importers, mail archives, scraped HTML, subtitle files, old database dumps. If you control both the writer and the reader, you do not need chardet at all, and using it would be a design smell. The README frames the package as a universal detector with 99 encodings, which is the right scope for a tool whose input is arbitrary bytes from elsewhere.
chardet 7 is a rewrite rather than an increment. The README is explicit that it keeps the same package name and the same public API, calls itself a drop-in replacement for chardet 5.x and 6.x, and changes the licence from LGPL to 0BSD. That last point matters more than the accuracy table for a lot of teams: LGPL in a Python dependency raises questions that 0BSD does not.
Inside the 13-stage pipeline: BOM, magic numbers, byte validity, bigrams
The README describes the detector as a 13-stage pipeline and names four of the stages: BOM detection, magic number identification, structural probing, and byte validity filtering, backed by bigram statistical models. That ordering is the design. Cheap, decisive checks run first. A byte order mark settles the answer outright. A magic number signature identifies a binary format and returns a MIME type instead of pretending the file is text. Only when those fail does the expensive statistical work start.
The public API exposes the result as a dict. For a plain ASCII sentence the README shows encoding, confidence, language and mime_type all populated. That is a wider contract than chardet 6 had: language detection and MIME type detection are both new in 7, and the README says language is returned with every result.
The part worth reading twice is detect_all(). It returns ranked candidates rather than a single verdict, and the README's French example is the honest illustration of why that exists: a windows-1252 sentence comes back as Windows-1252, iso8859-15, ISO-8859-1 and MacRoman, all at roughly 0.31 confidence. Those four encodings genuinely agree on that byte sequence. A single-answer API has to pick one and be wrong some of the time; detect_all() lets the caller decide, for instance by trying each candidate and keeping the one that decodes without errors.
The README also states that detect() and detect_all() are thread-safe and that the detector scales on free-threaded Python. Treat that as a claim in the documentation rather than something verified here.
Installing chardet and reading your first detection result
The README gives one install command and no build flags. It runs on Python 3.10 or newer, and pyproject.toml lists Python 3.10 through 3.14 plus PyPy. There are no runtime dependencies.
pip install chardetThe README notes that the package also ships optional compiled builds, using mypyc plus a Cython scoring kernel, described as a 4.8x additional speedup on CPython. The plain pip install is the pure-Python path; nothing in the README says the compiled wheels are the default, so assume the interpreted version unless you have checked the wheel you received.
A first real call is short. The README's quick start passes bytes to detect() and prints the result:
import chardet
chardet.detect(b"Python is a great programming language for beginners and experts alike.")You get back a dict with the encoding name, a confidence between 0 and 1, the detected language, and a MIME type. The README's own example for a UTF-8 sentence with accented characters returns utf-8 at 0.84 confidence, not 1.0. Plan for that: a threshold check like confidence > 0.9 will reject results the detector considers good.
For files too large to read into memory, the README points at UniversalDetector, which accepts data incrementally and exposes a done flag so you can stop early:
from chardet import UniversalDetector
detector = UniversalDetector()
with open("unknown.txt", "rb") as f:
for line in f:
detector.feed(line)
if detector.done:
break
result = detector.close()
print(result)The README states that large inputs do not need a byte cap and gives detect(data, max_bytes=len(data)) on a 272 MiB file as finishing in roughly 0.13 to 0.23 seconds, with the UTF-8 verdict validated over every byte examined rather than sampled. Those figures come from the project's performance documentation, not from this article.
There is also a CLI, registered as chardetect in pyproject.toml under [project.scripts]. The README shows chardetect somefile.txt printing the filename, encoding and confidence, --minimal for the encoding alone, -l to include the language name, -i utf-8,windows-1252 to restrict the candidate set, and piping from stdin with cat somefile.txt | chardetect.
Encoding era filtering and include/exclude lists are the real tuning knobs
The default behaviour of detect_all() is to consider every encoding era. For a Russian windows-1251 sentence the README shows four candidates across eras: Windows-1251 at 0.46, MacCyrillic at 0.42, KZ1048 at 0.2 and ptcp154 at 0.2. Two of those are plausible, and the gap between the top two is small.
Passing encoding_era=EncodingEra.MODERN_WEB collapses that to a single confident result, Windows-1251 at 0.46. This is the most useful parameter in the library for real pipelines, because most modern data is web data and the legacy candidates are noise. It is also a correctness risk in the other direction: if your corpus really does contain DOS or EBCDIC text, filtering to modern web encodings will silently remove the right answer from the candidate set. The README lists EBCDIC, Mac, DOS and Baltic/Central European families among the 99 supported encodings, so the detector can handle them, but only if you let it.
The include_encodings and exclude_encodings parameters do the same job by name. The README shows include_encodings=["utf-8", "windows-1252"] and exclude_encodings=["cp037", "cp500"] as examples. Excluding EBCDIC code pages is a reasonable default for anything that is not a mainframe feed. Neither parameter existed in chardet 6 according to the README's comparison table, so code migrating from 6 has no equivalent to port.
Where chardet gets it wrong, and when it is the wrong tool
Short inputs are the failure mode. The README's own French example returns four encodings at confidence 0.31 to 0.30 on a full sentence. Confidence is not a probability that the answer is right; it is a relative score, and on a 20-byte string there is not enough signal for any bigram model to separate windows-1252 from ISO-8859-1, because the two agree on almost every byte in that range. If your inputs are short, chardet will return a low-confidence guess and you need a fallback policy, not a threshold tweak.
Second, chardet is not a security control. One of the search questions people ask is whether chardet is safe. The README does not make any claim about detecting malicious content, and it should not be read as one. An encoding detector tells you how to decode bytes; it says nothing about whether those bytes are hostile. Decoding untrusted input is where you apply your own limits.
Third, the language and MIME type fields invite over-reading. Language detection is reported at 91.8% accuracy across 49 languages by the README, which means roughly one in twelve results is wrong. Using it to route documents by language is a different risk profile from using it to label output. MIME type detection covers 40+ binary formats via magic number signatures plus text/html, text/xml and text/x-python; a file with no recognisable signature is not classified by this, and files with a valid signature can still be mislabelled if the signature is ambiguous.
Finally, if you already know the encoding, do not call chardet. A CSV export from your own system, a config file you wrote, a JSON API response: all of these have a known encoding. Detection on known input adds a failure mode for nothing.
chardet versus charset-normalizer: different bets on the same problem
The obvious alternative is charset-normalizer, which the README benchmarks against directly at version 3.5.1. Both are pure-Python-adjacent encoding detectors with similar encoding coverage (99 each, per the README's table), and both are permissively licensed, MIT for charset-normalizer and 0BSD for chardet 7.
The difference the README highlights is architectural: chardet 7 supports streaming detection and charset-normalizer does not, according to that table. If you process network streams or files larger than memory, that is a concrete divider, and it is not a matter of degree. chardet 7 also claims encoding era filtering and MIME type detection, neither of which the table attributes to charset-normalizer.
The README reports chardet 7 at 99.7% accuracy against 86.6% for charset-normalizer 3.5.1 on the same 3,138 files, and 1.2x faster overall with 2.0x at the median. Those are the project's own measurements on its own test set, and the test set is the thing to interrogate: a corpus assembled by the chardet authors is not a neutral referee. The honest position is that chardet 7 looks stronger on the project's published numbers, and that you should re-run both against your own files before switching, because encoding distributions are extremely corpus-specific. A pipeline dominated by legacy Japanese mail will not behave like a pipeline dominated by modern web text.
One more difference is worth naming: chardet 7 keeps the same API as chardet 5.x and 6.x, so an existing chardet caller can move without touching call sites. Moving to charset-normalizer means rewriting them.
Licence, maintenance and what an upgrade actually costs
chardet 7 is 0BSD. The README states plainly that previous versions were LGPL. 0BSD is a permissive licence with no attribution requirement, which removes the compliance questions that LGPL dependencies raise in some organisations. That is a factual difference in licence text, not legal advice; check with whoever handles licensing where you work.
Maintenance looks current on the evidence available. The repository is not archived, and the last push was on 2026-08-30. The most recent release listed is 7.6.0 on 2026-08-14, with 7.5.0 and 7.5.1 on the same day. Note that the README's comparison table refers to 7.6.1.dev, a development version, so the accuracy and speed figures in that table describe code ahead of the 7.6.0 release.
Upgrade cost depends on which version you are on. From chardet 5.x or 6.x, the README says the API is unchanged: detect(), detect_all(), UniversalDetector and the chardetect CLI all work as before. The risk is not the call signatures, it is the return values. chardet 7 adds language and mime_type keys, and its confidence numbers come from a completely different scoring implementation, so any code that compares confidence against a fixed threshold needs re-checking against real files. Code that reads only result["encoding"] is the safest kind of caller.
There is also a documented history here that a reader should know about. The README links to a post titled "Read more details about the rewrite process" on the maintainer's blog, and one of the related search phrases is "Chardet controversy". The README does not describe that controversy, and this article cannot either, beyond noting that the project itself points readers to an explanation of how the rewrite happened.
Editorial conclusion
Adopt chardet 7 if you already call detect(), detect_all() or UniversalDetector and want 0BSD terms plus language and MIME type fields in the result dict; the README states the public API is unchanged, so the migration is mostly a version bump. Do not adopt it as a security control, and do not reach for it when you control both ends of the pipe, because the correct move there is to write UTF-8 and skip detection entirely. Before upgrading, verify two things against your own corpus: that the confidence values your code compares against still land where you expect, and that the encoding_era and include_encodings parameters do what you need, since the README says chardet 6 had neither. The licence change from LGPL to 0BSD is the reason many teams will look at this at all.
Frequently asked questions
What is chardet in Python and how is it used?
It is a character encoding detector: you pass it bytes and it returns a dict with the guessed encoding, a confidence value, the language and a MIME type. The README shows the basic call as chardet.detect(raw_bytes), with detect_all() available when you want the full ranked candidate list instead of one answer.
How do I install chardet?
The README gives a single command, pip install chardet. It requires Python 3.10 or newer and has zero runtime dependencies; the README also notes optional compiled builds using mypyc and a Cython scoring kernel.
Is chardet safe to use on untrusted input?
The README makes no security claim and does not describe any content inspection beyond magic number signatures for MIME type detection. It tells you how to decode bytes, not whether those bytes are safe, so any limits on untrusted input are yours to enforce.
How does chardet compare with charset-normalizer?
The README's comparison table reports chardet 7 at 99.7% accuracy versus 86.6% for charset-normalizer 3.5.1 on the same 3,138 files, and attributes streaming detection, encoding era filtering and MIME type detection to chardet 7 only. Those are the project's own measurements on its own test set.
What is the purpose of character encoding?
The README does not explain encoding theory; it treats the encoding as the unknown that has to be recovered from raw bytes before they can be decoded. chardet's job is to name that encoding, and it reports 99 supported encodings in its comparison table.
What does the chardet library return?
A dict. The README's examples show the keys encoding, confidence, language and mime_type, and detect_all() returns a ranked list of such results. The README states that language detection is returned with every result in chardet 7.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/chardet-chardet)Star history chart for your README
[](https://hysenlabs.com/projects/chardet-chardet)Was this useful?