Skip to main content

bytesense

Detect the encoding. Validate every byte. Keep your data intact.

PyPI CI MIT license

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.

Every returned encoding strictly decodes 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.

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

Upgrading from 0.x

  • Language reporting is opt-in: include_language=True.
  • confidence_interval remains available and is None; 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. steps and chunk_size remain accepted compatibility arguments; use sample_size to 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.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bytesense 1.0.0
File Size Uploaded
bytesense-1.0.0.tar.gz 310.8 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for bytesense 1.0.0
File
bytesense-1.0.0-py3-none-any.whl Python 3 none any Details
bytesense-1.0.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
bytesense-1.0.0-cp39-abi3-manylinux_2_28_x86_64.whl CPython 3.9 abi3 Linux glibc 2.28+ x86-64 Details
bytesense-1.0.0-cp39-abi3-manylinux_2_28_aarch64.whl CPython 3.9 abi3 Linux glibc 2.28+ ARM64 Details
bytesense-1.0.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
bytesense-1.0.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.0.0.tar.gz

Download URL bytesense-1.0.0.tar.gz
Size 310.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ac13ae61759db678d3abde23cab7eb2f004f11c132514c8f462732c1c1dcf269
BLAKE2b-256 checksum
How to use checksums
36ee7bb7481ba92368a2c7d1d142196736281523e28252f7efaeffe9daeb93c9
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 10, 2026.

Transparency log

Release files / bytesense-1.0.0-py3-none-any.whl

Download URL bytesense-1.0.0-py3-none-any.whl
Size 272.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
80fe891936ee8def3114caa0278fb053111cc1329c6e53e53dd59eaa9914e8d9
BLAKE2b-256 checksum
How to use checksums
c2904079197b5bc5c579c0b7e2942ab9fb77ba3ff3e6f9bb25f24e531b6f4a9a
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 10, 2026.

Transparency log

Release files / bytesense-1.0.0-cp39-abi3-win_amd64.whl

Download URL bytesense-1.0.0-cp39-abi3-win_amd64.whl
Size 384.5 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
c73e1289696ea1be1c7949e13310b689fd74fe1a23ed0ed3241c76b76c807a9f
BLAKE2b-256 checksum
How to use checksums
c642789a3428a2da349c26254f407049f04d55aa02eb7389488b92afef1bef45
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 10, 2026.

Transparency log

Release files / bytesense-1.0.0-cp39-abi3-manylinux_2_28_x86_64.whl

Download URL bytesense-1.0.0-cp39-abi3-manylinux_2_28_x86_64.whl
Size 481.0 kB
Tags CPython 3.9 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
40c1fd9f23b9e63a32523bca282a0592ee54f3e5b1d1f5d25850dbbe688aad7d
BLAKE2b-256 checksum
How to use checksums
bb6d8b674bf212828d16506c2243a46b0b906df9a401f39099f4e7a2adab6aa1
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 10, 2026.

Transparency log

Release files / bytesense-1.0.0-cp39-abi3-manylinux_2_28_aarch64.whl

Download URL bytesense-1.0.0-cp39-abi3-manylinux_2_28_aarch64.whl
Size 473.1 kB
Tags CPython 3.9 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
b30edfe4cacb646931b165caff4b7b8969f43c1343ce5f41da8c324e745a11bc
BLAKE2b-256 checksum
How to use checksums
1aa4e122a22eea00e45ff063e8409688b9dba5d73ed5fbf8bd7ead2a7db5bb7a
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 10, 2026.

Transparency log

Release files / bytesense-1.0.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL bytesense-1.0.0-cp39-abi3-macosx_11_0_arm64.whl
Size 458.5 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
301ca8b52a08f3b5a412f61dc70822fde858f3d617d1b4e36a1c2bbc68ca227c
BLAKE2b-256 checksum
How to use checksums
df01481b8fb70eaaac0b4fa1d529eaa27c19ff1e3965f03d1e1158be14a1d194
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 10, 2026.

Transparency log

Release files / bytesense-1.0.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL bytesense-1.0.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 470.9 kB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
a74c47cc7ba882ff26f7465a0fa9e1c932ae75cc5a7f4b0a5b071ea2466d041f
BLAKE2b-256 checksum
How to use checksums
a355e637d5bbab0dc4ec0390118cccb4d96fe59cdd2d7ea832e29cb936fa8f7e
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 10, 2026.

Transparency log

Release history Release notifications | RSS feed

1.2.0

7 release files

1.1.0

7 release files

This release

1.0.0 This release

7 release files

0.1.2

4 release files

0.1.1

4 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page