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%.
@@ -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
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
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
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).
- 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”
“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…”
“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”
“@SethMichaelLarson is correct, this is outside of Requests defined usage”
Failure Signature (Search String)
- import requests
Error Message
Stack trace
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
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
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.
When NOT to Use This Fix
- This fix is not applicable if the content is not properly set to bytes.
Verify Fix
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
| Version | Status |
|---|---|
| 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.