Jump to solution
Verify

The Fix

pip install requests==2.27.0

Based on closed psf/requests issue #4879 · PR/commit linked

Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.

Jump to Verify Open PR/Commit
@@ -115,3 +115,4 @@ Patches and Suggestions - André Graf (dergraf) - Stephen Zhuang (everbird) +- Martijn Pieters diff --git a/requests/models.py b/requests/models.py index c19d3ccab7..06c8a71a3e 100644
repro.py
import requests jsonstr="{ 'bla' : 'blub'}" resp = requests.Response() resp._content = jsonstr resp.json() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3/dist-packages/requests/models.py", line 880, in json encoding = guess_json_utf(self.content) File "/usr/lib/python3/dist-packages/requests/utils.py", line 798, in guess_json_utf nullcount = sample.count(_null) TypeError: must be str, not bytes
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Upgrade to fixed release\npip install requests==2.27.0\nWhen NOT to use: This fix is not applicable if the content is not properly set to bytes.\n\n

Why This Fix Works in Production

  • Trigger: import requests
  • Mechanism: The _null constant was incorrectly encoded to bytes, causing a TypeError in Python 3
  • Why the fix works: Introduces a JSON-specific encoding detection for responses without specified encoding, addressing issues related to type errors in Python 3. (first fixed release: 2.27.0).
Production impact:
  • If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.

Why This Breaks in Prod

  • Shows up under Python 3.7 in real deployments (not just unit tests).
  • The _null constant was incorrectly encoded to bytes, causing a TypeError in Python 3
  • Surfaces as: import requests

Proof / Evidence

  • GitHub issue: #4879
  • Fix PR: https://github.com/psf/requests/pull/909
  • First fixed release: 2.27.0
  • Reproduced locally: No (not executed)
  • Last verified: 2026-02-09
  • Confidence: 0.85
  • Did this fix it?: Yes (upstream fix exists)
  • Own content ratio: 0.50

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“for anyone else who get this error - just make content bytes”
@Smosker · 2019-11-06 · source
“So it is possible in python. Just nobody bothered. This is what I found in base64: warnings.warn("encodestring() is a deprecated alias since 3.1, " "use…”
@uvwild · 2018-11-26 · source
“Forgive me if I'm misunderstanding but setting Response._content to a value seems like mucking with internals which has no guarantees compared to the "public" interface”
@sethmlarson · 2018-11-26 · source
“@SethMichaelLarson is correct, this is outside of Requests defined usage”
@nateprewitt · 2018-11-26 · source

Failure Signature (Search String)

  • import requests

Error Message

Stack trace
error.txt
Error Message ------------- import requests jsonstr="{ 'bla' : 'blub'}" resp = requests.Response() resp._content = jsonstr resp.json() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3/dist-packages/requests/models.py", line 880, in json encoding = guess_json_utf(self.content) File "/usr/lib/python3/dist-packages/requests/utils.py", line 798, in guess_json_utf nullcount = sample.count(_null) TypeError: must be str, not bytes

Minimal Reproduction

repro.py
import requests jsonstr="{ 'bla' : 'blub'}" resp = requests.Response() resp._content = jsonstr resp.json() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3/dist-packages/requests/models.py", line 880, in json encoding = guess_json_utf(self.content) File "/usr/lib/python3/dist-packages/requests/utils.py", line 798, in guess_json_utf nullcount = sample.count(_null) TypeError: must be str, not bytes

Environment

  • Python: 3.7

What Broke

TypeError occurs when calling json() on a response with improperly set content.

Why It Broke

The _null constant was incorrectly encoded to bytes, causing a TypeError in Python 3

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

pip install requests==2.27.0

When NOT to use: This fix is not applicable if the content is not properly set to bytes.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

Fix reference: https://github.com/psf/requests/pull/909

First fixed release: 2.27.0

Last verified: 2026-02-09. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix is not applicable if the content is not properly set to bytes.

Verify Fix

verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Add a TLS smoke test that performs a real handshake in CI (include CA bundle validation and hostname checks).
  • Alert on handshake failures by error string and endpoint to catch cert/CA changes quickly.

Version Compatibility Table

VersionStatus
2.27.0 Fixed

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.