Skip to content

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]

fix(bedrock): treat ConflictException on stop as idempotent cancel

mateo-berri1b13957776ad
diff --git a/litellm/llms/bedrock/batches/handler.py b/litellm/llms/bedrock/batches/handler.py
index c9a4216c787..6efdd17f98d 100644
--- a/litellm/llms/bedrock/batches/handler.py
+++ b/litellm/llms/bedrock/batches/handler.py
@@ -153,7 +153,7 @@ class BedrockBatchesHandler:
try:
client.stop_model_invocation_job(jobIdentifier=batch_id)
except ClientError as e:
- if e.response.get("Error", {}).get("Code") != "ValidationException":
+ if e.response.get("Error", {}).get("Code") not in ("ValidationException", "ConflictException"):
raise
current_batch: Final = job_status()
if current_batch.status not in _CANCEL_IDEMPOTENT_STATUSES:
diff --git a/tests/test_litellm/llms/bedrock/batches/test_handler.py b/tests/test_litellm/llms/bedrock/batches/test_handler.py
index 500e738e338..1436ad2f383 100644
--- a/tests/test_litellm/llms/bedrock/batches/test_handler.py
+++ b/tests/test_litellm/llms/bedrock/batches/test_handler.py
@@ -365,6 +365,35 @@ def test_cancel_batch_tolerates_already_terminal_job(patched_boto3):
assert batch.status == "cancelled"
+def test_cancel_batch_tolerates_conflict_on_already_stopped_job(patched_boto3):
+ from botocore.exceptions import ClientError
+
+ fake_client, _ = patched_boto3
+ fake_client.stop_model_invocation_job.side_effect = ClientError(
+ {"Error": {"Code": "ConflictException", "Message": "Job cannot be stopped in its current state"}},
+ "StopModelInvocationJob",
+ )
+ fake_client.get_model_invocation_job.return_value = _fake_boto3_response(status="Stopped")
+
+ batch = BedrockBatchesHandler.cancel_batch(batch_id=JOB_ARN)
+
+ assert batch.status == "cancelled"
+
+
+def test_cancel_batch_reraises_conflict_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": "ConflictException", "Message": "Operation conflicts with current job 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_validation_error_when_job_not_terminal(patched_boto3):
from botocore.exceptions import ClientError