The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(proxy): coerce non-str x-litellm-* header values to avoid httpx TypeError (#27458) (#27504)
Tai An80445299b884
diff --git a/litellm/proxy/litellm_pre_call_utils.py b/litellm/proxy/litellm_pre_call_utils.pyindex 9d7d6f476ac..b97e7c5e693 100644--- a/litellm/proxy/litellm_pre_call_utils.py+++ b/litellm/proxy/litellm_pre_call_utils.py@@ -1,5 +1,6 @@import asyncioimport copy+import jsonimport reimport timefrom collections import OrderedDict@@ -794,8 +795,17 @@ class LiteLLMProxyRequestSetup:))for k, v in litellm_logging_metadata_headers.items():- if v is not None:+ if v is None:+ continue+ # httpx requires header values to be str or bytes; coerce numbers/bools+ # to str and JSON-encode dict/list (e.g. user_api_key_spend is float,+ # user_api_key_auth_metadata is dict). See #27458.+ if isinstance(v, (dict, list)):+ returned_headers["x-litellm-{}".format(k)] = json.dumps(v)+ elif isinstance(v, (str, bytes)):returned_headers["x-litellm-{}".format(k)] = v+ else:+ returned_headers["x-litellm-{}".format(k)] = str(v)return returned_headersdiff --git a/tests/proxy_unit_tests/test_proxy_utils.py b/tests/proxy_unit_tests/test_proxy_utils.pyindex 70232f25c37..68aff36038b 100644--- a/tests/proxy_unit_tests/test_proxy_utils.py+++ b/tests/proxy_unit_tests/test_proxy_utils.py@@ -587,12 +587,21 @@ def test_foward_litellm_user_info_to_backend_llm_call():user_api_key_dict=user_api_key_dict,)+ # All header values must be str/bytes so httpx won't reject them when the+ # downstream client builds the request (regression: #27458).+ for k, v in data.items():+ assert isinstance(v, (str, bytes)), (+ f"header {k!r} has non-str value {v!r} ({type(v).__name__}); "+ "httpx will raise 'Header value must be str or bytes' when the LLM "+ "request is built."+ )+expected_data = {"x-litellm-user_api_key_user_id": "test_user_id","x-litellm-user_api_key_org_id": "test_org_id","x-litellm-user_api_key_hash": "test_api_key",- "x-litellm-user_api_key_spend": 0.0,- "x-litellm-user_api_key_auth_metadata": {},+ "x-litellm-user_api_key_spend": "0.0",+ "x-litellm-user_api_key_auth_metadata": "{}",}assert json.dumps(data, sort_keys=True) == json.dumps(expected_data, sort_keys=True)