Detect the encoding. Validate every byte. Keep your data intact.
bytesense is a Python encoding detector for file imports, multilingual text pipelines and legacy data. It combines fast Unicode checks, compact character-pair statistics and optional Rust acceleration—with zero runtime dependencies.
Final detection results strictly validate the selected encoding against the complete supplied input. Linguistic scoring uses a bounded sample; stream inputs spill to a temporary file after a configurable memory limit. Results tell you what was examined, what was validated, and whether the input ended.
from bytesense import from_bytes
data = "İstanbul'da çalışan mühendisler için doğru metin çözümleme. ".encode("cp1254")
result = from_bytes(data)
if result.encoding:
text = data.decode(result.encoding) # strict decoding; no replacement characters
print(result.encoding, result.bytes_validated, result.complete)
Install
pip install bytesense
Python 3.9+. Platform wheels include Rust acceleration; the portable wheel works without a compiler. Source installations automatically use Rust when a toolchain is available. To explicitly choose a source build:
BYTESENSE_BUILD_RUST=0 pip install --no-binary=bytesense bytesense # pure Python
BYTESENSE_BUILD_RUST=1 pip install --no-binary=bytesense bytesense # require Rust
The old [fast] extra is a compatibility alias. It does not install an accelerator separately. BYTESENSE_PURE_PYTHON=1 selects the Python implementation at runtime.
Built for dependable imports
- Complete validation: a valid prefix cannot hide an undecodable suffix. Unknown and binary inputs have explicit outcomes.
- Bounded streams: feed small chunks or large ones; finalization accounts for every accepted byte. Preview stability never silently ends a stream.
- Useful controls: codec aliases, inclusion/exclusion filters, encoding hints, optional language estimates and a minimum evidence score.
- Transparent results:
why, alternatives, sample/validation counts and completion status. Confidence is a heuristic score; no fabricated statistical intervals. - Portable acceleration: native character-pair scoring and byte histograms, with matching Python behavior and typed public APIs.
- Data repair tools: opt-in mojibake repair and byte-preserving mixed-document segmentation, with strict decoding.
Decodability alone cannot prove the original encoding. Several legacy codecs can decode identical bytes into different, plausible text. Use a known encoding or an explicit hint when you have one, and evaluate representative data before switching detectors.
Files, streams and existing integrations
from bytesense import detect, detect_stream, from_path
result = from_path("export.csv", include_language=True)
print(result.to_dict())
with open("archive.txt", "rb") as source:
result = detect_stream(iter(lambda: source.read(64 * 1024), b""))
assert result.complete
# Familiar dictionary interface for code that uses chardet.detect().
metadata = detect(b"hello world")
detect_stream() consumes to EOF by default. max_bytes=... or early_stop=True is an explicit sampling choice and returns complete=False when it stops early. Streaming uses temporary disk space beyond memory_limit (1 MiB by default); close an unfinished StreamDetector or use it as a context manager.
bytesense report.csv
bytesense --language --verbose report.csv
cat report.csv | bytesense --minimal -
The CLI exits 0 when every input has an encoding, 1 for read failures or unknown/binary inputs, and 2 for invalid arguments. JSON goes to stdout; errors go to stderr.
Used by
ingest-sentry uses bytesense for encoding detection and full-snapshot validation before CSV/TSV imports. It adds import contracts, safe UTF-8 normalization, string-preserving pandas/Polars adapters, and a GitHub Action. Built by the same maintainer; an independent project, not affiliated with Sentry.
Measured accuracy and performance
The v1 engine replaces the previous collection of special-case ranking rules with a small, reproducibly generated statistical model. The evaluation separates development documents from held-out documents, grouping identical decoded text across encodings. Accuracy means exact Unicode equality, not just a compatible codec name.
| Detector | Exact Unicode matches | Accuracy |
|---|---|---|
| bytesense 1.2.0 | 1907 / 2078 | 91.77% |
| bytesense 1.1.0 | 1907 / 2078 | 91.77% |
| bytesense 1.0.0 | 1906 / 2078 | 91.72% |
| bytesense 0.1.2 | 883 / 2078 | 42.49% |
| chardet 7.6.0 | 2060 / 2078 | 99.13% |
| charset-normalizer 3.5.1 | 1679 / 2078 | 80.80% |
| chardetng-py 0.3.5 | 776 / 2078 | 37.34% |
See benchmarks and methodology for measured comparisons with chardet, charset-normalizer and chardetng, including the cases where another detector wins. Timing reports distinguish default behavior from an additional full-decode validation step.
New in 1.2: from_bytes() validates UTF-8 using native SIMD without allocating decoded Unicode; its pure-Python path uses 64 KiB blocks above 64 KiB. On the measured 1 MiB UTF-8 workload, native from_bytes() median latency decreased from 1.564 ms to 0.294 ms (5.3×) versus 1.1, with every byte still validated. Peak temporary Python allocations decreased from about 6 MiB to 6 KiB; this is not total process memory.
The existing 2,078-file and 432-case transfer results are unchanged. This release improves UTF-8 validation cost, not general encoding accuracy; chardet remains more accurate on both corpora. See the benchmark methodology for paired measurements, competitors, backend scope and reproduction commands.
Upgrading from 0.x
- Language reporting is opt-in:
include_language=True. confidence_intervalremains available and isNone; scores are not calibrated probabilities.- An empty
cp_isolation=[]allows no codecs. Filters apply to every detection path. - BOM-bearing UTF-16/32 selects
utf_16/utf_32, which consume the signature when decoding. - No unvalidated UTF-8 fallback; stream finalization and repair decoding reject silent data loss.
StreamDetector.feed()after finalization raises.reset()starts a new stream.- Python 3.8 is no longer supported.
stepsandchunk_sizeremain accepted compatibility arguments; usesample_sizeto control statistical work.
Quick start · API · Changes · Contributing
Created by Oğuzhan Kır. MIT-licensed code. The aggregate model's data provenance and reproduction steps are documented with the benchmarks; upstream test documents remain copyrighted by their respective publishers.
Metadata
Release files for bytesense 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| bytesense-1.2.0.tar.gz | 321.5 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| bytesense-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
| bytesense-1.2.0-cp39-abi3-win_amd64.whl | CPython 3.9 | abi3 | Windows x86-64 | Details |
| bytesense-1.2.0-cp39-abi3-manylinux_2_28_x86_64.whl | CPython 3.9 | abi3 | Linux glibc 2.28+ x86-64 | Details |
| bytesense-1.2.0-cp39-abi3-manylinux_2_28_aarch64.whl | CPython 3.9 | abi3 | Linux glibc 2.28+ ARM64 | Details |
| bytesense-1.2.0-cp39-abi3-macosx_11_0_arm64.whl | CPython 3.9 | abi3 | macOS 11.0+ ARM64 | Details |
| bytesense-1.2.0-cp39-abi3-macosx_10_12_x86_64.whl | CPython 3.9 | abi3 | macOS 10.12+ x86-64 | Details |
Total release size: 2.9 MB
Release files / bytesense-1.2.0.tar.gz
| Download URL | bytesense-1.2.0.tar.gz |
|---|---|
| Size | 321.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4946f92ae72a257a04d860fa5ba7a24e3bf70498f024f0e2b13fb1206f31f441
|
|
BLAKE2b-256 checksum How to use checksums |
77a3b632f4d65184523aef6260e89975ca0b748eff198c9327fb8593fbf2e983
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.
Transparency logRelease files / bytesense-1.2.0-py3-none-any.whl
| Download URL | bytesense-1.2.0-py3-none-any.whl |
|---|---|
| Size | 275.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
52bfac15736c003d45957fe0caa7c2c17676baa66ba12dbf4a71226810332ba9
|
|
BLAKE2b-256 checksum How to use checksums |
238102199efeb8f72e7cb43e37d16ebdaf38a6d198042dd17f9777c4a70744c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.
Transparency logRelease files / bytesense-1.2.0-cp39-abi3-win_amd64.whl
| Download URL | bytesense-1.2.0-cp39-abi3-win_amd64.whl |
|---|---|
| Size | 391.0 kB |
| Tags | CPython 3.9 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
230f2862a1327e5586fa80ffae0677f25033946a5df14f1e3f6a233767e927a2
|
|
BLAKE2b-256 checksum How to use checksums |
913c1671204cb0d9814fde74242e416781cda620925c3a10643659e9559ee471
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.
Transparency logRelease files / bytesense-1.2.0-cp39-abi3-manylinux_2_28_x86_64.whl
| Download URL | bytesense-1.2.0-cp39-abi3-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 487.6 kB |
| Tags | CPython 3.9 Linux glibc 2.28+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
929b2f808ef82bc67571a3640344697eafc925483ff59bec7097b3d502b8b48e
|
|
BLAKE2b-256 checksum How to use checksums |
d96478ac3d737f279441e84264c1e02c4c021186f70a042a43bfeb087f1840a7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.
Transparency logRelease files / bytesense-1.2.0-cp39-abi3-manylinux_2_28_aarch64.whl
| Download URL | bytesense-1.2.0-cp39-abi3-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 476.2 kB |
| Tags | CPython 3.9 Linux glibc 2.28+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
223aea69d61602ba1bb8879b9768b078a70a47d3e4ff6f1346cd891ce7abb164
|
|
BLAKE2b-256 checksum How to use checksums |
ee025d23babff8971b67dcf48623d45a1b6d4cf71d2d493c3a94107c854b2276
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.
Transparency logRelease files / bytesense-1.2.0-cp39-abi3-macosx_11_0_arm64.whl
| Download URL | bytesense-1.2.0-cp39-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 460.9 kB |
| Tags | CPython 3.9 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
f9eda69f5d295902a555e59ea83ad0e6ffb9687f28a42f733a00d0bdb6d7a9bc
|
|
BLAKE2b-256 checksum How to use checksums |
bcdf19d2626482aa0ba9f4cab0ef43e65e765e82533ed22939ecd8f58e48551f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.
Transparency logRelease files / bytesense-1.2.0-cp39-abi3-macosx_10_12_x86_64.whl
| Download URL | bytesense-1.2.0-cp39-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 476.2 kB |
| Tags | CPython 3.9 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
3188b7ccbcdaad8531ccc301986fcd6e2115b3e18d0e5d87926952eb7c9fbd2c
|
|
BLAKE2b-256 checksum How to use checksums |
cf1a14e650e3a0a929fde8a9f81a34640aaccfdca4495f19528e2916ce9e05b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.
Transparency log