The Fix
pip install requests==2.27.0
Based on closed psf/requests issue #5504 · 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%.
@@ -273,7 +273,9 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
containing the exact bytes that will be sent to the server.
- Generated from either a :class:`Request <Request>` object or manually.
+ Instances are generated from a :class:`Request <Request>` object, and
+ should not be instantiated manually; doing so may produce undesirable
import requests
if __name__ == "__main__":
req = requests.PreparedRequest()
req.prepare_body(data="hello world!", files=None)
print(req.body)
Re-run: python3 main.py
Option A — Upgrade to fixed release\npip install requests==2.27.0\nWhen NOT to use: This fix is not applicable if manual instantiation of PreparedRequest is intended.\n\n
Why This Fix Works in Production
- Trigger: $ python3 main.py
- Mechanism: The method prepare_body fails when headers are None, leading to a TypeError
- Why the fix works: Clarifies that PreparedRequests should not be manually created to avoid undesirable effects. (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.6 in real deployments (not just unit tests).
- The method prepare_body fails when headers are None, leading to a TypeError
- Surfaces as: $ python3 main.py
Proof / Evidence
- GitHub issue: #5504
- Fix PR: https://github.com/psf/requests/pull/5505
- 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.48
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Right, you're expected to create a Request and then call prepare on that to get your own PreparedRequest”
“> You really shouldn't be creating PreparedRequests Gotcha. The API docs for it say manual creation is allowed, so if that's not the case, the…”
“That's a fair point. I'm sure Nate or Seth would merge a PR if you removed that language”
“Yep, I think that's completely reasonable. Partial initialization has a number of failure points so we shouldn't be recommending it.”
Failure Signature (Search String)
- $ python3 main.py
Error Message
Stack trace
Error Message
-------------
$ python3 main.py
Traceback (most recent call last):
File "main.py", line 5, in <module>
req.prepare_body(data="hello world!", files=None)
File "/Users/nkrichevsky/.local/lib/python3.6/site-packages/requests/models.py", line 514, in prepare_body
self.prepare_content_length(body)
File "/Users/nkrichevsky/.local/lib/python3.6/site-packages/requests/models.py", line 529, in prepare_content_length
self.headers['Content-Length'] = builtin_str(length)
TypeError: 'NoneType' object does not support item assignment
Minimal Reproduction
import requests
if __name__ == "__main__":
req = requests.PreparedRequest()
req.prepare_body(data="hello world!", files=None)
print(req.body)
Environment
- Python: 3.6
What Broke
Attempting to prepare a request body without headers results in a TypeError and prevents request execution.
Why It Broke
The method prepare_body fails when headers are None, leading to a TypeError
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/5505
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 manual instantiation of PreparedRequest is intended.
Verify Fix
Re-run: python3 main.py
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.