Support env var vertex credentials for passthrough + ignore space id on watsonx deployment (throws Json validation errors) (#11527)

* fix(router.py): support vertex credentials set in env var for passthrough

Closes https://github.com/BerriAI/litellm/issues/11245

* fix(watsonx/common_utils.py): do not pass space_id on watsonx deployment requests - raises Json validation error

Fixes https://github.com/BerriAI/litellm/issues/10941

* test: update unit test
This commit is contained in:
Krish Dholakia 2025-06-07 20:31:05 -07:00 • committed by GitHub
parent 7acaa08ad4
commit 2654d3b0b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 14 additions and 18 deletions

View file

@ -38,7 +38,11 @@ def generate_iam_token(api_key=None, **params) -> str:
headers = {}
headers["Content-Type"] = "application/x-www-form-urlencoded"
if api_key is None:
api_key = get_secret_str("WX_API_KEY") or get_secret_str("WATSONX_API_KEY") or get_secret_str("WATSONX_APIKEY")
api_key = (
get_secret_str("WX_API_KEY")
or get_secret_str("WATSONX_API_KEY")
or get_secret_str("WATSONX_APIKEY")
)
if api_key is None:
raise ValueError("API key is required")
headers["Accept"] = "application/json"
@ -280,13 +284,9 @@ class IBMWatsonXMixin:
def _prepare_payload(self, model: str, api_params: WatsonXAPIParams) -> dict:
payload: dict = {}
if model.startswith("deployment/"):
if api_params["space_id"] is None:
raise WatsonXAIError(
status_code=401,
message="Error: space_id is required for models called using the 'deployment/' endpoint. Pass in the space_id as a parameter or set it in the WX_SPACE_ID environment variable.",
)
payload["space_id"] = api_params["space_id"]
return payload
return (
{}
) # Deployment models do not support 'space_id' or 'project_id' in their payload
payload["model_id"] = model
payload["project_id"] = api_params["project_id"]
return payload

View file

@ -110,7 +110,7 @@ class PassthroughEndpointRouter:
self,
project_id: str,
location: str,
vertex_credentials: VERTEX_CREDENTIALS_TYPES,
vertex_credentials: Optional[VERTEX_CREDENTIALS_TYPES],
):
"""
Add the vertex credentials for the given project-id, location

View file

@ -4740,13 +4740,9 @@ class Router:
or deployment.litellm_params.vertex_credentials
)
if (
vertex_project is None
or vertex_location is None
or vertex_credentials is None
):
if vertex_project is None or vertex_location is None:
raise ValueError(
"vertex_project, vertex_location, and vertex_credentials must be set in litellm_params for pass-through endpoints"
"vertex_project, and vertex_location must be set in litellm_params for pass-through endpoints."
)
passthrough_endpoint_router.add_vertex_credentials(
project_id=vertex_project,

View file

@ -206,7 +206,7 @@ def test_watsonx_deployment_space_id(monkeypatch, watsonx_chat_completion_call,
assert mock_post.call_count == 1
json_data = json.loads(mock_post.call_args.kwargs["data"])
assert my_fake_space_id == json_data["space_id"]
assert my_fake_space_id not in json_data
def test_watsonx_deployment_space_id_embedding(monkeypatch, watsonx_embedding_call):
@ -217,4 +217,4 @@ def test_watsonx_deployment_space_id_embedding(monkeypatch, watsonx_embedding_ca
assert mock_post.call_count == 1
json_data = json.loads(mock_post.call_args.kwargs["data"])
assert my_fake_space_id == json_data["space_id"]
assert my_fake_space_id not in json_data

View file

@ -92,7 +92,7 @@ def test_initialize_deployment_for_pass_through_missing_params():
# Test that initialization raises ValueError
with pytest.raises(
ValueError,
match="vertex_project, vertex_location, and vertex_credentials must be set",
match="vertex_project, and vertex_location must be set in litellm_params for pass-through endpoints",
):
router._initialize_deployment_for_pass_through(
deployment=deployment,