#3400·httpx

Mention header name error message for invalid header encodings

Author: RobertCraigieCreated Nov 12, 2024Updated Sep 26, 2026

Discussed in https://github.com/encode/httpx/discussions/3399

Originally posted by RobertCraigie November 12, 2024 This openai-python user ran into a confusing error when passing a non-ascii header value, would it be possible to mention the header name in the error message?

Minimal repro

python
import httpx

httpx.Headers({"auth": "здравейздравейздравейздравей"})
Traceback (most recent call last):
  File "script.py", line 3, in <module>
    httpx.Headers({"auth": "здравейздравейздравейздравей"})
  File ".venv/lib/python3.9/site-packages/httpx/_models.py", line 74, in __init__
    self._list = [
  File ".venv/lib/python3.9/site-packages/httpx/_models.py", line 78, in <listcomp>
    normalize_header_value(v, encoding),
  File ".venv/lib/python3.9/site-packages/httpx/_utils.py", line 53, in normalize_header_value
    return value.encode(encoding or "ascii")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-27: ordinal not in range(128)
```</div>