From 2723c69bfeafc9cf80a2a2df1bdd5edc23f863c9 Mon Sep 17 00:00:00 2001 From: Vasilisa Parshikova Date: Thu, 5 Mar 2026 19:48:20 +0400 Subject: [PATCH] (sap) fix after bot review --- litellm/llms/sap/credentials.py | 8 +++--- .../llms/sap/test_sap_fetch_creds.py | 25 ++++++++----------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/litellm/llms/sap/credentials.py b/litellm/llms/sap/credentials.py index 56777a6ff19..55c50a033f0 100644 --- a/litellm/llms/sap/credentials.py +++ b/litellm/llms/sap/credentials.py @@ -9,9 +9,9 @@ import os import tempfile import httpx -from litellm import sap_service_key from litellm.llms.custom_httpx.http_handler import _get_httpx_client, HTTPHandler from litellm._logging import verbose_logger +import litellm AUTH_ENDPOINT_SUFFIX = "/oauth/token" @@ -175,7 +175,7 @@ def extract_credentials(source: Source, exclude: Optional[List[str]] = None) -> if cv.name in exclude: continue value = source.get(cv) - if value: + if value is not None: credentials[cv.name] = cv.transform_fn(value) if cv.transform_fn else value return credentials @@ -216,7 +216,7 @@ def fetch_credentials(service_key: Optional[Union[str, dict]] = None, profile: O """ config = init_conf(profile) - service_key = service_key or sap_service_key or _load_json_env(SERVICE_KEY_ENV_VAR) + service_key = service_key or litellm.sap_service_key or _load_json_env(SERVICE_KEY_ENV_VAR) vcap_service = _get_vcap_service(VCAP_AICORE_SERVICE_NAME) sources = [ @@ -271,7 +271,7 @@ def validate_credentials( ] if sum(bool(m) for m in modes) != 1: raise ValueError( - "SAP AI Core credentials are incomplete." + "SAP AI Core credentials are incomplete. " "Invalid credentials: provide exactly one of client_secret, " "(cert_str & key_str), or (cert_file_path & key_file_path)." ) diff --git a/tests/test_litellm/llms/sap/test_sap_fetch_creds.py b/tests/test_litellm/llms/sap/test_sap_fetch_creds.py index 6877c2ba698..572eecdb5d8 100644 --- a/tests/test_litellm/llms/sap/test_sap_fetch_creds.py +++ b/tests/test_litellm/llms/sap/test_sap_fetch_creds.py @@ -31,35 +31,32 @@ mock_sap_vcap_service_key_dict = { } }] } +def _prep_env(monkeypatch): + for var in ("AICORE_CLIENT_ID", "AICORE_CLIENT_SECRET", "AICORE_AUTH_URL", + "AICORE_BASE_URL", "AICORE_CERT_URL", "AICORE_SERVICE_KEY", "VCAP_SERVICES"): + monkeypatch.delenv(var, raising=False) + monkeypatch.setenv("AICORE_HOME", 'notexist') def test_sap_fetch_creds_from_env_service_key(monkeypatch): - monkeypatch.setattr(sap_credentials, "sap_service_key", None) - monkeypatch.setenv("AICORE_HOME", 'notexist') + _prep_env(monkeypatch) monkeypatch.setenv("AICORE_SERVICE_KEY", json.dumps(mock_sap_service_key_dict)) creds = sap_credentials.fetch_credentials() assert creds == expected_creds def test_sap_fetch_creds_from_arg_service_key(monkeypatch): - monkeypatch.setattr(sap_credentials, "sap_service_key", None) - monkeypatch.delenv("AICORE_SERVICE_KEY", raising=False) - monkeypatch.setenv("AICORE_HOME", 'notexist') + _prep_env(monkeypatch) creds = sap_credentials.fetch_credentials(service_key=json.dumps(mock_sap_service_key_dict)) assert creds == expected_creds def test_fetch_creds_from_env_vcap_service(monkeypatch): - monkeypatch.setattr(sap_credentials, "sap_service_key", None) - monkeypatch.delenv("AICORE_SERVICE_KEY", raising=False) - monkeypatch.setenv("AICORE_HOME", 'notexist') + _prep_env(monkeypatch) monkeypatch.setenv("VCAP_SERVICES", json.dumps(mock_sap_vcap_service_key_dict)) creds = sap_credentials.fetch_credentials() assert creds['client_id'] == "vcap-clientid" assert creds['client_secret'] == "vcap-clientsecret" def test_fetch_creds_from_env(monkeypatch): - monkeypatch.setattr(sap_credentials, "sap_service_key", None) - monkeypatch.delenv("AICORE_SERVICE_KEY", raising=False) - monkeypatch.delenv("VCAP_SERVICES", raising=False) - monkeypatch.setenv("AICORE_HOME", 'notexist') + _prep_env(monkeypatch) monkeypatch.setenv("AICORE_CLIENT_ID", "env-client-id") monkeypatch.setenv("AICORE_CLIENT_SECRET", "env-client-secret") monkeypatch.setenv("AICORE_AUTH_URL", "env-auth-url") @@ -75,9 +72,7 @@ def test_fetch_creds_from_env(monkeypatch): assert creds['resource_group'] == "env-resource-group" def test_creds_priority_order(monkeypatch): - monkeypatch.setattr(sap_credentials, "sap_service_key", None) - monkeypatch.delenv("AICORE_SERVICE_KEY", raising=False) - monkeypatch.setenv("AICORE_HOME", 'notexist') + _prep_env(monkeypatch) monkeypatch.setenv("AICORE_CLIENT_ID", "env-client-id") monkeypatch.setenv("AICORE_CLIENT_SECRET", "env-client-secret") monkeypatch.setenv("AICORE_AUTH_URL", "env-auth-url")