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(bedrock): reraise cancel validation errors for non-terminal jobs, allow bedrock in acancel_batch typing
mateo-berri4114f907ea14
diff --git a/litellm/batches/main.py b/litellm/batches/main.pyindex 756e8a6804c..a5df9b78601 100644--- a/litellm/batches/main.py+++ b/litellm/batches/main.py@@ -826,7 +826,7 @@ def list_batches(async def acancel_batch(batch_id: str,model: str | None = None,- custom_llm_provider: Literal["openai", "azure", "vertex_ai"] = "openai",+ custom_llm_provider: Literal["openai", "azure", "vertex_ai", "bedrock"] = "openai",metadata: dict[str, str] | None = None,extra_headers: dict[str, str] | None = None,extra_body: dict[str, str] | None = None,diff --git a/litellm/llms/bedrock/batches/handler.py b/litellm/llms/bedrock/batches/handler.pyindex 757552cec7e..c9a4216c787 100644--- a/litellm/llms/bedrock/batches/handler.py+++ b/litellm/llms/bedrock/batches/handler.py@@ -25,6 +25,8 @@ _BEDROCK_MIJ_STATUS_TO_OPENAI: Final = {"Expired": "expired",}+_CANCEL_IDEMPOTENT_STATUSES: Final = frozenset({"cancelling", "cancelled", "completed", "failed", "expired"})+def _extract_region_from_bedrock_arn(arn: str) -> str | None:"""ARN shape: ``arn:aws:bedrock:<region>:<account>:<type>/<id>``"""@@ -132,31 +134,33 @@ class BedrockBatchesHandler:aws_session_token=creds.token,)+ def job_status() -> "LiteLLMBatch":+ return BedrockBatchesHandler._handle_model_invocation_job_status(+ batch_id=batch_id,+ aws_region_name=region,+ logging_obj=logging_obj,+ aws_access_key_id=aws_access_key_id,+ aws_secret_access_key=aws_secret_access_key,+ aws_session_token=aws_session_token,+ aws_session_name=aws_session_name,+ aws_profile_name=aws_profile_name,+ aws_role_name=aws_role_name,+ aws_web_identity_token=aws_web_identity_token,+ aws_sts_endpoint=aws_sts_endpoint,+ aws_external_id=aws_external_id,+ )+try:client.stop_model_invocation_job(jobIdentifier=batch_id)except ClientError as e:- error_code: Final = e.response.get("Error", {}).get("Code")- error_msg: Final = e.response.get("Error", {}).get("Message", "").lower()- already_terminal: Final = error_code == "ValidationException" and any(- term in error_msg for term in ("stop", "terminal", "completed", "already")- )- if not already_terminal:+ if e.response.get("Error", {}).get("Code") != "ValidationException":raise+ current_batch: Final = job_status()+ if current_batch.status not in _CANCEL_IDEMPOTENT_STATUSES:+ raise+ return current_batch- return BedrockBatchesHandler._handle_model_invocation_job_status(- batch_id=batch_id,- aws_region_name=region,- logging_obj=logging_obj,- aws_access_key_id=aws_access_key_id,- aws_secret_access_key=aws_secret_access_key,- aws_session_token=aws_session_token,- aws_session_name=aws_session_name,- aws_profile_name=aws_profile_name,- aws_role_name=aws_role_name,- aws_web_identity_token=aws_web_identity_token,- aws_sts_endpoint=aws_sts_endpoint,- aws_external_id=aws_external_id,- )+ return job_status()@staticmethoddef _handle_async_invoke_status(batch_id: str, aws_region_name: str, logging_obj=None, **kwargs) -> "LiteLLMBatch":diff --git a/tests/test_litellm/llms/bedrock/batches/test_handler.py b/tests/test_litellm/llms/bedrock/batches/test_handler.pyindex a54a0efae55..500e738e338 100644--- a/tests/test_litellm/llms/bedrock/batches/test_handler.py+++ b/tests/test_litellm/llms/bedrock/batches/test_handler.py@@ -365,6 +365,20 @@ def test_cancel_batch_tolerates_already_terminal_job(patched_boto3):assert batch.status == "cancelled"+def test_cancel_batch_reraises_validation_error_when_job_not_terminal(patched_boto3):+ from botocore.exceptions import ClientError++ fake_client, _ = patched_boto3+ fake_client.stop_model_invocation_job.side_effect = ClientError(+ {"Error": {"Code": "ValidationException", "Message": "Cannot stop job in current state"}},+ "StopModelInvocationJob",+ )+ fake_client.get_model_invocation_job.return_value = _fake_boto3_response(status="InProgress")++ with pytest.raises(ClientError):+ BedrockBatchesHandler.cancel_batch(batch_id=JOB_ARN)++def test_cancel_batch_reraises_other_client_errors(patched_boto3):from botocore.exceptions import ClientError