Jump to solution
Verify

The Fix

Upgrade to version 0.23.1 or later.

Based on closed encode/httpx issue #2069 · 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
@@ -122,8 +122,14 @@ def __init__(self, name: str, value: FileTypes) -> None: headers["Content-Type"] = content_type - if isinstance(fileobj, (str, io.StringIO)): - raise TypeError(f"Expected bytes or bytes-like object got: {type(fileobj)}") + if "b" not in getattr(fileobj, "mode", "b"):
repro.py
import requests import httpx files = {'upload-file': (None, 'text content', 'text/plain')} httpx.post("https://httpbin.org/post", files=files) # error requests.post("https://httpbin.org/post", files=files) # works files = {'upload-file': (None, b'text content', 'text/plain')} httpx.post("https://httpbin.org/post", files=files) # works requests.post("https://httpbin.org/post", files=files) # works
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.23.1 or later.\nWhen NOT to use: This fix is not applicable if you need to enforce binary-only uploads.\n\n

Why This Fix Works in Production

  • Trigger: TypeError: Expected bytes or bytes-like object got: <class 'str'>
  • Mechanism: The library only accepted binary content for multipart uploads, causing TypeErrors with string inputs
  • Why the fix works: Allows 'str' content for multipart uploads, addressing the issue where only binary content was accepted. (first fixed release: 0.23.1).
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

  • The library only accepted binary content for multipart uploads, causing TypeErrors with string inputs
  • Surfaces as: TypeError: Expected bytes or bytes-like object got: <class 'str'>

Proof / Evidence

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

Discussion

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

“If we decide to keep the behavior -- should we also try to sniff "have we got str content?" early on enough so that we…”
@florimondmanca · 2022-02-26 · source
“FWIW I am hopeful that we can allow non-files for two reasons: 1”
@jamesboehmer · 2022-03-03 · source
“Thanks for raising this @imsgj - you're absolutely correct”
@lovelydinosaur · 2022-02-09 · source
“> FWIW I am hopeful that we can allow non-files for two reasons: > > 1”
@ImBatou · 2022-06-20 · source

Failure Signature (Search String)

  • TypeError: Expected bytes or bytes-like object got: <class 'str'>

Error Message

Stack trace
error.txt
Error Message ------------- TypeError: Expected bytes or bytes-like object got: <class 'str'>

Minimal Reproduction

repro.py
import requests import httpx files = {'upload-file': (None, 'text content', 'text/plain')} httpx.post("https://httpbin.org/post", files=files) # error requests.post("https://httpbin.org/post", files=files) # works files = {'upload-file': (None, b'text content', 'text/plain')} httpx.post("https://httpbin.org/post", files=files) # works requests.post("https://httpbin.org/post", files=files) # works

What Broke

Users experienced TypeErrors when attempting to upload string content as files.

Why It Broke

The library only accepted binary content for multipart uploads, causing TypeErrors with string inputs

Fix Options (Details)

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

Upgrade to version 0.23.1 or later.

When NOT to use: This fix is not applicable if you need to enforce binary-only uploads.

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/2400

First fixed release: 0.23.1

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 is not applicable if you need to enforce binary-only uploads.

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.

Version Compatibility Table

VersionStatus
0.23.1 Fixed

Related Issues

No related fixes found.

Sources

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