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(vertex-ai): support batch cancel via Vertex API
Sameer Kankute0bc609affdc0
diff --git a/litellm/batches/main.py b/litellm/batches/main.pyindex e176dc42921..36093d071ba 100644--- a/litellm/batches/main.py+++ b/litellm/batches/main.py@@ -884,7 +884,7 @@ def list_batches(async def acancel_batch(batch_id: str,model: Optional[str] = None,- custom_llm_provider: Literal["openai", "azure"] = "openai",+ custom_llm_provider: Literal["openai", "azure", "vertex_ai"] = "openai",metadata: Optional[Dict[str, str]] = None,extra_headers: Optional[Dict[str, str]] = None,extra_body: Optional[Dict[str, str]] = None,@@ -930,7 +930,7 @@ async def acancel_batch(def cancel_batch(batch_id: str,model: Optional[str] = None,- custom_llm_provider: Union[Literal["openai", "azure"], str] = "openai",+ custom_llm_provider: Union[Literal["openai", "azure", "vertex_ai"], str] = "openai",metadata: Optional[Dict[str, str]] = None,extra_headers: Optional[Dict[str, str]] = None,extra_body: Optional[Dict[str, str]] = None,@@ -1047,9 +1047,35 @@ def cancel_batch(cancel_batch_data=_cancel_batch_request,litellm_params=litellm_params,)+ elif custom_llm_provider == "vertex_ai":+ api_base = optional_params.api_base or ""+ vertex_ai_project = (+ optional_params.vertex_project+ or litellm.vertex_project+ or get_secret_str("VERTEXAI_PROJECT")+ )+ vertex_ai_location = (+ optional_params.vertex_location+ or litellm.vertex_location+ or get_secret_str("VERTEXAI_LOCATION")+ )+ vertex_credentials = optional_params.vertex_credentials or get_secret_str(+ "VERTEXAI_CREDENTIALS"+ )++ response = vertex_ai_batches_instance.cancel_batch(+ _is_async=_is_async,+ batch_id=batch_id,+ api_base=api_base,+ vertex_project=vertex_ai_project,+ vertex_location=vertex_ai_location,+ vertex_credentials=vertex_credentials,+ timeout=timeout,+ max_retries=optional_params.max_retries,+ )else:raise litellm.exceptions.BadRequestError(- message="LiteLLM doesn't support {} for 'cancel_batch'. Only 'openai' and 'azure' are supported.".format(+ message="LiteLLM doesn't support {} for 'cancel_batch'. Only 'openai', 'azure', and 'vertex_ai' are supported.".format(custom_llm_provider),model="n/a",diff --git a/litellm/llms/vertex_ai/batches/handler.py b/litellm/llms/vertex_ai/batches/handler.pyindex f0b181c9a61..a24bfd89f07 100644--- a/litellm/llms/vertex_ai/batches/handler.py+++ b/litellm/llms/vertex_ai/batches/handler.py@@ -376,3 +376,116 @@ class VertexAIBatchPrediction(VertexLLM):response=_json_response)return vertex_batch_response++ def cancel_batch(+ self,+ _is_async: bool,+ batch_id: str,+ api_base: Optional[str],+ vertex_credentials: Optional[VERTEX_CREDENTIALS_TYPES],+ vertex_project: Optional[str],+ vertex_location: Optional[str],+ timeout: Union[float, httpx.Timeout],+ max_retries: Optional[int],+ ) -> Union[LiteLLMBatch, Coroutine[Any, Any, LiteLLMBatch]]:+ sync_handler = _get_httpx_client()++ access_token, project_id = self._ensure_access_token(+ credentials=vertex_credentials,+ project_id=vertex_project,+ custom_llm_provider="vertex_ai",+ )++ default_api_base = self.create_vertex_batch_url(+ vertex_location=vertex_location or "us-central1",+ vertex_project=vertex_project or project_id,+ )++ default_api_base = f"{default_api_base}/{batch_id}:cancel"++ if len(default_api_base.split(":")) > 1:+ endpoint = default_api_base.split(":")[-1]+ else:+ endpoint = ""++ _, api_base = self._check_custom_proxy(+ api_base=api_base,+ custom_llm_provider="vertex_ai",+ gemini_api_key=None,+ endpoint=endpoint,+ stream=None,+ auth_header=None,+ url=default_api_base,+ model=None,+ vertex_project=vertex_project or project_id,+ vertex_location=vertex_location or "us-central1",+ vertex_api_version="v1",+ )++ headers = {+ "Content-Type": "application/json; charset=utf-8",+ "Authorization": f"Bearer {access_token}",+ }++ if _is_async is True:+ return self._async_cancel_batch(+ api_base=api_base,+ retrieve_api_base=api_base.rsplit(":cancel", 1)[0],+ headers=headers,+ )++ response = sync_handler.post(+ url=api_base,+ headers=headers,+ data=json.dumps({}),+ )++ if response.status_code != 200:+ raise Exception(f"Error: {response.status_code} {response.text}")++ retrieve_response = sync_handler.get(+ url=api_base.rsplit(":cancel", 1)[0],+ headers=headers,+ )+ if retrieve_response.status_code != 200:+ raise Exception(+ f"Error: {retrieve_response.status_code} {retrieve_response.text}"+ )++ _json_response = retrieve_response.json()+ vertex_batch_response = VertexAIBatchTransformation.transform_vertex_ai_batch_response_to_openai_batch_response(+ response=_json_response+ )+ return vertex_batch_response++ async def _async_cancel_batch(+ self,+ api_base: str,+ retrieve_api_base: str,+ headers: Dict[str, str],+ ) -> LiteLLMBatch:+ client = get_async_httpx_client(+ llm_provider=litellm.LlmProviders.VERTEX_AI,+ )+ response = await client.post(+ url=api_base,+ headers=headers,+ data=json.dumps({}),+ )+ if response.status_code != 200:+ raise Exception(f"Error: {response.status_code} {response.text}")++ retrieve_response = await client.get(+ url=retrieve_api_base,+ headers=headers,+ )+ if retrieve_response.status_code != 200:+ raise Exception(+ f"Error: {retrieve_response.status_code} {retrieve_response.text}"+ )++ _json_response = retrieve_response.json()+ vertex_batch_response = VertexAIBatchTransformation.transform_vertex_ai_batch_response_to_openai_batch_response(+ response=_json_response+ )+ return vertex_batch_responsediff --git a/litellm/proxy/batches_endpoints/endpoints.py b/litellm/proxy/batches_endpoints/endpoints.pyindex 740e63b7f17..06254b57fc5 100644--- a/litellm/proxy/batches_endpoints/endpoints.py+++ b/litellm/proxy/batches_endpoints/endpoints.py@@ -5,7 +5,7 @@######################################################################import asyncio-from typing import Dict, Optional, cast+from typing import Any, Dict, Optional, castfrom fastapi import APIRouter, Depends, HTTPException, Path, Request, Response@@ -654,7 +654,7 @@ async def list_batches(managed_files_obj, "list_user_batches"):verbose_proxy_logger.debug("Using managed objects table for batch listing")- response = await managed_files_obj.list_user_batches(+ response = await cast(Any, managed_files_obj).list_user_batches(user_api_key_dict=user_api_key_dict,limit=limit,after=after,@@ -685,8 +685,9 @@ async def list_batches(# Encode batch IDs in the list response so clients can use# them for retrieve/cancel/file downloads through the proxy.- if response and hasattr(response, "data") and response.data:- for batch in response.data:+ response_data = getattr(response, "data", None)+ if response_data:+ for batch in response_data:encode_batch_response_ids(batch, model=model_param)verbose_proxy_logger.debug(f"Listed batches using model: {model_param}")@@ -896,7 +897,10 @@ async def cancel_batch(# SCENARIO 3: Fallback to custom_llm_provider (uses env variables)else:custom_llm_provider = (- provider or data.pop("custom_llm_provider", None) or "openai"+ provider+ or get_custom_llm_provider_from_request_headers(request=request)+ or data.pop("custom_llm_provider", None)+ or "openai")# Extract batch_id from data to avoid "multiple values for keyword argument" error# data was cast from CancelBatchRequest which already contains batch_iddiff --git a/tests/test_litellm/llms/vertex_ai/test_vertex_ai_batch_transformation.py b/tests/test_litellm/llms/vertex_ai/test_vertex_ai_batch_transformation.pyindex 1aab74ddc26..d27cd8ba8a7 100644--- a/tests/test_litellm/llms/vertex_ai/test_vertex_ai_batch_transformation.py+++ b/tests/test_litellm/llms/vertex_ai/test_vertex_ai_batch_transformation.py@@ -1,3 +1,9 @@+from unittest.mock import MagicMock, patch++import pytest++import litellm+from litellm.llms.vertex_ai.batches.handler import VertexAIBatchPredictionfrom litellm.llms.vertex_ai.batches.transformation import VertexAIBatchTransformation@@ -36,3 +42,75 @@ def test_output_file_id_falls_back_to_output_uri_prefix_with_predictions_jsonl()output_file_id== "gs://test-bucket/litellm-vertex-files/publishers/google/models/gemini-2.5-pro/prediction-model-456/predictions.jsonl")+++async def test_vertex_ai_cancel_batch():+ """Test that vertex_ai cancel_batch calls the correct API endpoint"""+ handler = VertexAIBatchPrediction(gcs_bucket_name="test-bucket")++ mock_response = MagicMock()+ mock_response.status_code = 200+ mock_response.json.return_value = {+ "name": "projects/test-project/locations/us-central1/batchPredictionJobs/123456",+ "state": "JOB_STATE_CANCELLING",+ "createTime": "2024-03-17T10:00:00.000000Z",+ "inputConfig": {+ "gcsSource": {+ "uris": ["gs://test-bucket/input.jsonl"]+ }+ },+ "outputConfig": {+ "gcsDestination": {+ "outputUriPrefix": "gs://test-bucket/output"+ }+ }+ }++ with patch("litellm.llms.vertex_ai.batches.handler._get_httpx_client") as mock_client:+ mock_client.return_value.post.return_value = mock_response+ mock_client.return_value.get.return_value = mock_response++ with patch.object(handler, "_ensure_access_token") as mock_auth:+ mock_auth.return_value = ("fake-token", "test-project")++ response = handler.cancel_batch(+ _is_async=False,+ batch_id="123456",+ api_base=None,+ vertex_credentials=None,+ vertex_project="test-project",+ vertex_location="us-central1",+ timeout=600.0,+ max_retries=None,+ )++ assert response.id == "123456"+ assert response.status == "cancelling"++ mock_client.return_value.post.assert_called_once()+ mock_client.return_value.get.assert_called_once()+ call_args = mock_client.return_value.post.call_args+ assert ":cancel" in call_args.kwargs["url"]+++async def test_litellm_cancel_batch_vertex_ai():+ """Test that litellm.cancel_batch works with vertex_ai provider"""+ mock_response = MagicMock()+ mock_response.id = "batch_123"+ mock_response.status = "cancelling"++ with patch.object(litellm.batches.main, "vertex_ai_batches_instance") as mock_instance:+ mock_instance.cancel_batch.return_value = mock_response++ response = litellm.cancel_batch(+ batch_id="batch_123",+ custom_llm_provider="vertex_ai",+ vertex_project="test-project",+ vertex_location="us-central1",+ )++ assert mock_instance.cancel_batch.called+ assert response.id == "batch_123"+ assert response.status == "cancelling"