mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-22 10:40:46 +00:00
fix(chatgpt): retain SDK login headers during provider resolution
This commit is contained in:
parent
b86f8fd091
commit
c21c1fb09c
4 changed files with 79 additions and 16 deletions
|
|
@ -678,7 +678,10 @@ async def acompletion(
|
|||
if custom_llm_provider is None:
|
||||
_supplemental: Final = cast( # cast-ok: kwargs values are request-scoped objects
|
||||
"dict[str, object]",
|
||||
{k: kwargs[k] for k in OPTIONAL_KWARGS_KEYS | {"provider_specific_header"} if k in kwargs},
|
||||
{
|
||||
**{k: kwargs[k] for k in OPTIONAL_KWARGS_KEYS | {"provider_specific_header"} if k in kwargs},
|
||||
"extra_headers": extra_headers,
|
||||
},
|
||||
)
|
||||
_, custom_llm_provider, _, _ = get_llm_provider(
|
||||
model=model,
|
||||
|
|
@ -5680,7 +5683,8 @@ def completion(
|
|||
model = deployment_id
|
||||
custom_llm_provider = "azure"
|
||||
_supplemental_provider_params: Final = {
|
||||
k: kwargs[k] for k in OPTIONAL_KWARGS_KEYS | {"provider_specific_header"} if k in kwargs
|
||||
**{k: kwargs[k] for k in OPTIONAL_KWARGS_KEYS | {"provider_specific_header"} if k in kwargs},
|
||||
"extra_headers": extra_headers,
|
||||
}
|
||||
model, custom_llm_provider, dynamic_api_key, api_base = get_llm_provider(
|
||||
model=model,
|
||||
|
|
|
|||
|
|
@ -911,7 +911,8 @@ async def aresponses(
|
|||
model=model,
|
||||
api_base=local_vars.get("base_url", None),
|
||||
litellm_params=GenericLiteLLMParams(
|
||||
**cast("dict[str, object]", kwargs) # cast-ok: kwargs is the untyped request dict
|
||||
extra_headers=extra_headers,
|
||||
**cast("dict[str, object]", kwargs), # cast-ok: kwargs is the untyped request dict
|
||||
),
|
||||
)
|
||||
# Update local_vars with detected provider (fixes #19782)
|
||||
|
|
@ -974,7 +975,7 @@ async def aresponses(
|
|||
original_model=original_model,
|
||||
swapped_model=model,
|
||||
custom_llm_provider=custom_llm_provider,
|
||||
kwargs=kwargs,
|
||||
kwargs={**kwargs, "extra_headers": extra_headers},
|
||||
prompt_id=prompt_id,
|
||||
)
|
||||
kwargs.pop("prompt_id", None)
|
||||
|
|
@ -1153,7 +1154,7 @@ def _apply_prompt_management_to_responses_call(
|
|||
original_model=original_model,
|
||||
swapped_model=model,
|
||||
custom_llm_provider=custom_llm_provider,
|
||||
kwargs=kwargs,
|
||||
kwargs={**kwargs, "extra_headers": local_vars.get("extra_headers")},
|
||||
prompt_id=prompt_id,
|
||||
)
|
||||
)
|
||||
|
|
@ -1509,7 +1510,8 @@ def responses(
|
|||
model=model,
|
||||
api_base=local_vars.get("base_url", None),
|
||||
litellm_params=GenericLiteLLMParams(
|
||||
**cast("dict[str, object]", kwargs) # cast-ok: kwargs is the untyped request dict
|
||||
extra_headers=extra_headers,
|
||||
**cast("dict[str, object]", kwargs), # cast-ok: kwargs is the untyped request dict
|
||||
),
|
||||
)
|
||||
local_vars["custom_llm_provider"] = custom_llm_provider
|
||||
|
|
@ -1527,7 +1529,7 @@ def responses(
|
|||
|
||||
from litellm.litellm_core_utils.core_helpers import attribute_or_none
|
||||
|
||||
litellm_params: Final = GenericLiteLLMParams(**kwargs)
|
||||
litellm_params: Final = GenericLiteLLMParams(extra_headers=extra_headers, **kwargs)
|
||||
provider_headers: Final = ResponsesAPIRequestUtils.merge_provider_specific_headers(
|
||||
extra_headers=extra_headers,
|
||||
provider_specific_header=attribute_or_none(litellm_params, "provider_specific_header"),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
from collections.abc import Iterator
|
||||
from pathlib import Path
|
||||
from typing import Final
|
||||
|
||||
import respx
|
||||
|
||||
import asyncio, httpx, importlib, os
|
||||
import pytest
|
||||
|
||||
|
|
@ -719,14 +723,14 @@ def test_opencode_affinity_resolution_preserves_existing_transport(prefix: str)
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def chatgpt_login_tripwire(monkeypatch: pytest.MonkeyPatch, tmp_path) -> None:
|
||||
from litellm.llms.chatgpt.authenticator import Authenticator
|
||||
|
||||
def _tripwire(self: Authenticator) -> str:
|
||||
raise AssertionError("metadata lookup ran the ChatGPT OAuth device flow")
|
||||
|
||||
monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(tmp_path / "missing-login"))
|
||||
monkeypatch.setattr(Authenticator, "get_access_token", _tripwire)
|
||||
def chatgpt_login_tripwire(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Iterator[None]:
|
||||
blocked: Final = tmp_path / "blocked"
|
||||
blocked.write_text("ordinary file")
|
||||
monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(blocked / "unused"))
|
||||
monkeypatch.delenv("CHATGPT_AUTH_FILE", raising=False)
|
||||
with respx.mock as transport:
|
||||
yield
|
||||
assert len(transport.calls) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
|
|
|||
|
|
@ -171,7 +171,9 @@ class TestChatGPTMultiAccountChat:
|
|||
|
||||
def _subscription_response(request: httpx.Request) -> httpx.Response:
|
||||
account: Final = request.headers["ChatGPT-Account-Id"]
|
||||
assert request.headers["Authorization"] == f"Bearer token-{account.removeprefix('acct-')}"
|
||||
assert request.headers["Authorization"] in (
|
||||
f"Bearer token-{account.removeprefix('acct-')}", f"Bearer {_client_token(account)}"
|
||||
)
|
||||
assert "chatgpt_auth_file" not in request.content.decode()
|
||||
message: Final = {
|
||||
"id": "msg_test",
|
||||
|
|
@ -498,3 +500,54 @@ async def test_cross_provider_fallback_does_not_receive_the_chatgpt_login(
|
|||
assert response.output[0].content[0].text == "fallback"
|
||||
assert fallback.calls.last.request.headers["Authorization"] == "Bearer sk-example-fallback"
|
||||
assert "ChatGPT-Account-Id" not in fallback.calls.last.request.headers
|
||||
|
||||
|
||||
@pytest.mark.parametrize("endpoint", ["chat", "responses"])
|
||||
@pytest.mark.parametrize("async_call", [False, True])
|
||||
@pytest.mark.parametrize("with_prompt", [False, True])
|
||||
@pytest.mark.asyncio
|
||||
async def test_sdk_extra_headers_use_client_login_without_gateway_auth(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, endpoint: str, async_call: bool, with_prompt: bool
|
||||
) -> None:
|
||||
blocked: Final = tmp_path / "blocked"
|
||||
blocked.write_text("ordinary file")
|
||||
monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(blocked / "unused"))
|
||||
monkeypatch.delenv("CHATGPT_AUTH_FILE", raising=False)
|
||||
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
||||
headers: Final = {"Authorization": f"Bearer {_client_token('acct-client-a')}"}
|
||||
prompt_params: Final = {"prompt_id": "sdk-client"} if with_prompt else {}
|
||||
if with_prompt:
|
||||
monkeypatch.setattr(
|
||||
litellm,
|
||||
"callbacks",
|
||||
[
|
||||
DotpromptManager(
|
||||
prompt_id="sdk-client",
|
||||
prompt_data={"content": "Hello", "metadata": {"model": "chatgpt/gpt-6-luna"}},
|
||||
)
|
||||
],
|
||||
)
|
||||
with respx.mock as transport:
|
||||
transport.post("https://chatgpt.com/backend-api/codex/responses").mock(side_effect=_subscription_response)
|
||||
if endpoint == "chat":
|
||||
params: Final = {
|
||||
"model": "chatgpt/gpt-6.1-sol",
|
||||
"messages": [{"role": "user", "content": "Hello"}],
|
||||
"extra_headers": headers,
|
||||
**prompt_params,
|
||||
}
|
||||
result: Final = await litellm.acompletion(**params) if async_call else litellm.completion(**params)
|
||||
assert result.choices[0].message.content == "acct-client-a"
|
||||
else:
|
||||
response: Final = (
|
||||
await litellm.aresponses(
|
||||
model="chatgpt/gpt-6.1-sol", input="Hello", extra_headers=headers, **prompt_params
|
||||
)
|
||||
if async_call
|
||||
else litellm.responses(
|
||||
model="chatgpt/gpt-6.1-sol", input="Hello", extra_headers=headers, **prompt_params
|
||||
)
|
||||
)
|
||||
assert response.output[0].content[0].text == "acct-client-a"
|
||||
assert transport.calls.last.request.headers["Authorization"] == headers["Authorization"]
|
||||
assert len(transport.calls) == 1
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue