Jump to solution
Verify

The Fix

Upgrade to version 0.18.0 or later.

Based on closed encode/httpx issue #1482 · PR/commit linked

Production note: Watch p95/p99 latency and retry volume; timeouts can turn into retry storms and duplicate side-effects.

Jump to Verify Open PR/Commit
@@ -40,11 +40,7 @@ def render_headers(self) -> bytes: def render_data(self) -> bytes: if not hasattr(self, "_data"): - self._data = ( - self.value - if isinstance(self.value, bytes)
repro.py
import httpx response = httpx.post( "https://httpbin.org/post", files={ 'upload-file': ('example.txt', '\u00E9', 'text/plain; charset=utf-8') } ) response.raise_for_status() print(response.read())
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\nUpgrade to version 0.18.0 or later.\nWhen NOT to use: This fix should not be used if the application relies on the original Content-Length behavior.\n\n

Why This Fix Works in Production

  • Trigger: response = httpx.post(
  • Mechanism: Content-Length is calculated using Unicode length instead of byte length for non-ascii characters
  • Why the fix works: Fixes the Content-Length calculation for unicode file contents in multipart requests, addressing the issue where an exception was raised for non-ascii characters. (first fixed release: 0.18.0).
Production impact:
  • If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).

Why This Breaks in Prod

  • Shows up under Python 3.9 in real deployments (not just unit tests).
  • Content-Length is calculated using Unicode length instead of byte length for non-ascii characters
  • Surfaces as: Traceback (most recent call last):

Proof / Evidence

  • GitHub issue: #1482
  • Fix PR: https://github.com/encode/httpx/pull/1537
  • First fixed release: 0.18.0
  • Reproduced locally: No (not executed)
  • Last verified: 2026-02-08
  • Confidence: 0.85
  • Did this fix it?: Yes (upstream fix exists)
  • Own content ratio: 0.44

Verified Execution

We executed the runnable minimal repro in a temporary environment and captured exit codes + logs.

  • Status: PASS
  • Ran: 2026-02-11T16:52:29Z
  • Package: httpx
  • Fixed: 0.18.0
  • Mode: fixed_only
  • Outcome: ok
Logs
affected (exit=None)
fixed (exit=0)
b'{\n "args": {}, \n "data": "", \n "files": {\n "upload-file": "\\u00e9"\n }, \n "form": {}, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Content-Length": "197", \n "Content-Type": "multipart/form-data; boundary=8e29a4c1fcee5377fe1b2ecfbd64760a", \n "Host": "httpbin.org", \n "User-Agent": "python-httpx/0.18.0", \n "X-Amzn-Trace-Id": "Root=1-698cb453-60705b9b741ee1964ad3415c"\n }, \n "json": null, \n "origin": "34.38.131.152", \n "url": "https://httpbin.org/post"\n}\n'

Discussion

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

“Looks like we calculate Content-Length using Unicode length: https://github.com/encode/httpx/blob/0f280af8b170ed5cc48c12a894f71a8b5762f748/httpx/_multipart.py#L90-L91 but sends with bytes: https://github.com/encode/httpx/blob/0f280af8b170ed5cc48c12a894f71a8b57”
@j178 · 2021-02-24 · source

Failure Signature (Search String)

  • response = httpx.post(

Error Message

Stack trace
error.txt
Error Message ------------- Traceback (most recent call last): File "/***redacted***/httpx_bug.py", line 5, in <module> response = httpx.post( File "/***redacted***/.venv/lib/python3.9/site-packages/httpx/_api.py", line 296, in post return request( File "/***redacted***/.venv/lib/python3.9/site-packages/httpx/_api.py", line 93, in request return client.request( File "/***redacted***/.venv/lib/python3.9/site-packages/httpx/_client.py", line 733, in request return self.send( File "/***redacted***/.venv/lib/python3.9/site-packages/httpx/_client.py", line 767, in send response = self._send_handling_auth( File "/***redacted***/.venv/lib/python3.9/site-packages/httpx/_client.py", line 805, in _send_handling_auth response = self._send_handling_redirects( File "/***redacted***/.venv/lib/python3.9/site-packages/httpx/_client.py", line 837, in _send_handling_redirects response = self._send_single_request(request, timeout) File "/***redacted***/.venv/lib/python3.9/site-packages/httpx/_client.py", line 861, in _send_single_request (status_code, headers, stream, ext) = transport.request( File "/***redacted***/.venv/lib/python3.9/site-packages/httpcore/_sync/connection_pool.py", line 218, in request response = connection.request( File "/***redacted***/.venv/lib/python3.9/site-packages/httpcore/_sync/connection.py", line 106, in request return self.connection.r ... (truncated) ...

Minimal Reproduction

repro.py
import httpx response = httpx.post( "https://httpbin.org/post", files={ 'upload-file': ('example.txt', '\u00E9', 'text/plain; charset=utf-8') } ) response.raise_for_status() print(response.read())

Environment

  • Python: 3.9

What Broke

An exception is raised when uploading files with non-ascii characters, causing failed requests.

Why It Broke

Content-Length is calculated using Unicode length instead of byte length for non-ascii characters

Fix Options (Details)

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

Upgrade to version 0.18.0 or later.

When NOT to use: This fix should not be used if the application relies on the original Content-Length behavior.

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

Fix reference: https://github.com/encode/httpx/pull/1537

First fixed release: 0.18.0

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

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix should not be used if the application relies on the original Content-Length behavior.

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 CI check that diffs key outputs after upgrades (OpenAPI schema snapshots, JSON payload shapes, CLI output).
  • Upgrade behind a canary and run integration tests against the canary before 100% rollout.
  • 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
0.18.0 Fixed

Related Issues

No related fixes found.

Sources

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