psf/requests · warning · RequestsDependencyWarning
Unable to find acceptable character detection dependency (ch
Error message
Unable to find acceptable character detection dependency (chardet or charset_normalizer).
What it means
A `RequestsDependencyWarning` (not an exception) emitted at import time from `check_compatibility()` in `src/requests/__init__.py:92`. Requests uses chardet or charset_normalizer to guess a response's text encoding when the server doesn't declare one; if neither package is importable (or their versions fell outside the supported ranges checked just above), it warns that automatic character detection is unavailable. Requests still works — `response.text` just falls back to less accurate encoding handling for undeclared charsets.
Source
Thrown at src/requests/__init__.py:92
major, minor, patch = int(major), int(minor), int(patch)
# urllib3 >= 1.21.1
assert major >= 1
if major == 1:
assert minor >= 21
# Check charset_normalizer for compatibility.
if chardet_version:
major, minor, patch = chardet_version.split(".")[:3]
major, minor, patch = int(major), int(minor), int(patch)
# chardet_version >= 3.0.2, < 8.0.0
assert (3, 0, 2) <= (major, minor, patch) < (8, 0, 0)
elif charset_normalizer_version:
major, minor, patch = charset_normalizer_version.split(".")[:3]
major, minor, patch = int(major), int(minor), int(patch)
# charset_normalizer >= 2.0.0 < 4.0.0
assert (2, 0, 0) <= (major, minor, patch) < (4, 0, 0)
else:
warnings.warn(
"Unable to find acceptable character detection dependency "
"(chardet or charset_normalizer).",
RequestsDependencyWarning,
)
def _check_cryptography(cryptography_version: str) -> None:
# cryptography < 1.3.4
try:
cryptography_version_list = list(map(int, cryptography_version.split(".")))
except ValueError:
return
if cryptography_version_list < [1, 3, 4]:
warning = f"Old version of cryptography ({cryptography_version_list}) may cause slowdown."
warnings.warn(warning, RequestsDependencyWarning)
View on GitHub ↗ (pinned to 414f0513c3)
Solutions
- Install the standard dependency: `pip install charset_normalizer` (or `pip install 'requests[use_chardet_on_py3]'` for chardet).
- Reinstall requests with dependencies: `pip install --force-reinstall requests` (drop any `--no-deps` flag in your build).
- Verify the active interpreter matches where you installed: `python -m pip show charset_normalizer`.
- If you always know your responses' encodings, you may suppress the warning and set `response.encoding` explicitly — detection is only needed for undeclared charsets.
Example fix
# before (Dockerfile) RUN pip install --no-deps requests # after RUN pip install requests # pulls charset_normalizer, urllib3, idna, certifi
Defensive patterns
Strategy: fallback
Validate before calling
import importlib.util
if not (importlib.util.find_spec('charset_normalizer') or importlib.util.find_spec('chardet')):
raise RuntimeError('Install charset_normalizer (or chardet): pip install charset_normalizer') Try / catch
try:
import requests
except ImportError as e:
if 'charset' in str(e) or 'chardet' in str(e):
sys.exit('Missing charset detection dep — pip install "requests[use_chardet_on_py3]" or charset_normalizer')
raise Prevention
- Install requests via pip so charset_normalizer comes in as a declared dependency — this error usually means a broken/manual install
- Pin charset_normalizer (or chardet) explicitly in requirements.txt if you vendor or strip dependencies
- Verify the environment at deploy time with pip check / a smoke-test import
- If you never rely on detection, set response.encoding explicitly — but the import-time check still requires the package present
When it happens
Trigger: `import requests` in an environment where neither `charset_normalizer` (>=2,<4) nor `chardet` (>=3.0.2,<8) is installed — the versions are passed into `check_compatibility()` as None when import failed.
Common situations: Minimal/vendored installs, `pip install --no-deps requests`, aggressive dependency pruning in Docker images or lambda bundles, a broken/partial upgrade that uninstalled charset_normalizer, conda/pip mixed environments where the package landed in a different site-packages, or PyInstaller/py2exe bundles that missed the hidden import.
Related errors
- Invalid percent-escape sequence: '{h}'
- Old version of cryptography ({cryptography_version_list}) ma
- Files must be provided.
- Data must not be a string.
- Unsupported event specified, with event name "{event}"
AI-assisted analysis of psf/requests@414f0513c3 (2026-07-31).
Data as JSON: /data/errors/5916d25c56cfa39a.json.
Report an issue: GitHub ↗.