mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-14 23:21:35 +00:00
fix(aiohttp): sanitize non-ASCII response headers before passing to httpx
Fixes #26548. aiohttp decodes response header values as UTF-8 strings, but httpx.Headers internally calls .encode("ascii") which raises UnicodeEncodeError when headers contain non-ASCII characters (e.g. Chinese text in error messages from some providers). Re-encode non-ASCII header values via UTF-8→latin-1 to preserve the raw bytes in a form httpx can accept.
This commit is contained in:
parent
3d2b8fed32
commit
c1323e20d9
1 changed files with 15 additions and 1 deletions
|
|
@ -339,9 +339,23 @@ class LiteLLMAiohttpTransport(AiohttpTransport):
|
|||
# Re-raise if it's a different RuntimeError
|
||||
raise
|
||||
|
||||
# Sanitize response headers: aiohttp decodes header values as UTF-8
|
||||
# strings, but httpx.Headers internally calls .encode("ascii") which
|
||||
# raises UnicodeEncodeError on non-ASCII values (e.g. Chinese characters).
|
||||
# Re-encode via latin-1 to preserve the raw bytes for httpx.
|
||||
sanitized_headers = []
|
||||
for key, value in response.headers.items():
|
||||
try:
|
||||
value.encode("ascii")
|
||||
sanitized_headers.append((key, value))
|
||||
except UnicodeEncodeError:
|
||||
sanitized_headers.append(
|
||||
(key, value.encode("utf-8").decode("latin-1"))
|
||||
)
|
||||
|
||||
return httpx.Response(
|
||||
status_code=response.status,
|
||||
headers=response.headers,
|
||||
headers=sanitized_headers,
|
||||
stream=AiohttpResponseStream(response),
|
||||
request=request,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue