Skip to content

Conversation

@stdrc
Copy link

@stdrc stdrc commented Oct 11, 2025

  • 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 stdrc requested a review from a team as a code owner October 11, 2025 12:39
@karpetrosyan
Copy link
Collaborator

Hey! It looks like a duplicate of this issue.

Do you see the same error when you run this code, and does passing encoding='utf-8' to httpx.Headers resolve it?

# /// script # requires-python = ">=3.9" # dependencies = [ # "httpx", # ] # /// import os import httpx api_key = os.getenv("OPENAI_API_KEY") url = "https://api.openai.com/v1/chat/completions" headers = httpx.Headers({"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}, encoding=None) data = { "model": "gpt-4", "messages": [{"role": "user", "content": "Hello!"}], "max_tokens": 100, } with httpx.Client() as client: response = client.post(url, headers=headers, json=data) response.raise_for_status() result = response.json() print(result["choices"][0]["message"]["content"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants