openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

fix: encode httpx headers with utf-8

Open stdrc opened this issue 3 months ago • 1 comments

  • [x] I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

When custom headers contain CJK or other non-ascii characters, httpx.Headers(headers_dict) without explicit utf-8 encoding will raise:

  File "...", line 85, in generate
    response = await self._client.chat.completions.create(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<6 lines>...
    )
    ^
  File ".venv/lib/python3.13/site-packages/openai/resources/chat/completions/completions.py", line 2603, in create
    return await self._post(
           ^^^^^^^^^^^^^^^^^
    ...<48 lines>...
    )
    ^
  File ".venv/lib/python3.13/site-packages/openai/_base_client.py", line 1794, in post
    return await self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.13/site-packages/openai/_base_client.py", line 1515, in request
    request = self._build_request(options, retries_taken=retries_taken)
  File ".venv/lib/python3.13/site-packages/openai/_base_client.py", line 495, in _build_request
    headers = self._build_headers(options, retries_taken=retries_taken)
  File ".venv/lib/python3.13/site-packages/openai/_base_client.py", line 439, in _build_headers
    headers = httpx.Headers(headers_dict)
  File ".venv/lib/python3.13/site-packages/httpx/_models.py", line 156, in __init__
    bytes_value = _normalize_header_value(v, encoding)
  File ".venv/lib/python3.13/site-packages/httpx/_models.py", line 82, in _normalize_header_value
    return value.encode(encoding or "ascii")
           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'ascii' codec can't encode characters in position 58-59: ordinal not in range(128)

Additional context & links

stdrc avatar Oct 11 '25 12:10 stdrc