fix: only include Authorization header when OPENAI_API_KEY is set (#33488)

Signed-off-by: zack041 <zackyu041@gmail.com>
This commit is contained in:
Zack Yu
2026-01-31 23:35:09 -08:00
committed by GitHub
parent 302ecf64ff
commit 754a8ca942

View File

@@ -138,6 +138,16 @@ def _update_headers_common(
headers["x-request-id"] = request_func_input.request_id
def _get_headers(content_type: str | None = None) -> dict[str, str]:
headers = {}
if content_type:
headers["Content-Type"] = content_type
api_key = os.environ.get("OPENAI_API_KEY")
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
return headers
async def async_request_openai_completions(
request_func_input: RequestFuncInput,
session: aiohttp.ClientSession,
@@ -170,9 +180,7 @@ async def async_request_openai_completions(
}
_update_payload_common(payload, request_func_input)
headers = {
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
}
headers = _get_headers()
_update_headers_common(headers, request_func_input)
output = RequestFuncOutput()
@@ -301,10 +309,7 @@ async def async_request_openai_chat_completions(
}
_update_payload_common(payload, request_func_input)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
}
headers = _get_headers("application/json")
_update_headers_common(headers, request_func_input)
output = RequestFuncOutput()
@@ -396,9 +401,7 @@ async def async_request_openai_audio(
}
_update_payload_common(payload, request_func_input)
headers = {
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
}
headers = _get_headers()
_update_headers_common(headers, request_func_input)
# Send audio file
@@ -537,10 +540,7 @@ async def async_request_openai_embeddings(
}
_update_payload_common(payload, request_func_input)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
}
headers = _get_headers("application/json")
_update_headers_common(headers, request_func_input)
return await _run_pooling_request(
@@ -576,10 +576,7 @@ async def async_request_vllm_rerank(
"truncate_prompt_tokens": -1,
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
}
headers = _get_headers("application/json")
_update_headers_common(headers, request_func_input)
return await _run_pooling_request(
@@ -615,10 +612,7 @@ async def async_request_openai_embeddings_chat(
}
_update_payload_common(payload, request_func_input)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
}
headers = _get_headers("application/json")
_update_headers_common(headers, request_func_input)
return await _run_pooling_request(
@@ -721,10 +715,7 @@ async def async_request_infinity_embeddings(
_update_payload_common(payload, request_func_input)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
}
headers = _get_headers("application/json")
_update_headers_common(headers, request_func_input)
return await _run_pooling_request(