From 757021ab1985d2b228ab39f4feccc3874a497ac3 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 11:52:37 -0700 Subject: [PATCH] fix(vertex_ai): keep Vertex's error body on batch create errors and require custom_id on Claude lines --- litellm/llms/vertex_ai/batches/handler.py | 8 +++++--- litellm/llms/vertex_ai/files/transformation.py | 4 ++-- tests/unit/llms/vertex_ai/batches/test_handler.py | 2 ++ .../files/test_vertex_ai_files_transformation.py | 12 +++++++----- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/litellm/llms/vertex_ai/batches/handler.py b/litellm/llms/vertex_ai/batches/handler.py index 7cd41b07bbb..d1094044847 100644 --- a/litellm/llms/vertex_ai/batches/handler.py +++ b/litellm/llms/vertex_ai/batches/handler.py @@ -18,6 +18,7 @@ from litellm.llms.custom_httpx.http_handler import ( MaskedHTTPStatusError, get_async_httpx_client, get_httpx_client, + mask_sensitive_info, ) from litellm.llms.vertex_ai.common_utils import VertexAIError, get_vertex_base_url from litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini import VertexLLM @@ -83,10 +84,11 @@ def _vertex_batch_list_payload(response: _VertexBatchListJsonSource) -> dict[str def _vertex_batch_create_error(error: httpx.HTTPStatusError) -> MaskedHTTPStatusError: + body: Final = mask_sensitive_info(error.response.text) return MaskedHTTPStatusError( - httpx.HTTPStatusError( - f"Vertex AI batch create failed: {error.response.text}", request=error.request, response=error.response - ) + httpx.HTTPStatusError(f"Vertex AI batch create failed: {body}", request=error.request, response=error.response), + message=body, + text=body, ) diff --git a/litellm/llms/vertex_ai/files/transformation.py b/litellm/llms/vertex_ai/files/transformation.py index 27308d72eab..8eb0157c4dc 100644 --- a/litellm/llms/vertex_ai/files/transformation.py +++ b/litellm/llms/vertex_ai/files/transformation.py @@ -771,6 +771,8 @@ def _openai_batch_jsonl_entry_to_vertex_anthropic_row( Transforms an OpenAI chat batch entry into the Anthropic Messages row Vertex batches Claude with, `{"custom_id": ..., "request": {"messages": [...], "anthropic_version": "vertex-2023-10-16", "max_tokens": ...}}` """ + if custom_id is None: + raise ValueError("custom_id is required on Claude lines, since Claude output rows are matched back only by it") config: Final = VertexAIAnthropicConfig() bare_model: Final = model.rsplit("/", 1)[-1] params: Final = {**deployment_params, **request_params} @@ -821,8 +823,6 @@ def _openai_batch_jsonl_entry_to_vertex_anthropic_row( ), } ) - if custom_id is None: - return {"request": request} return {"custom_id": str(custom_id), "request": request} diff --git a/tests/unit/llms/vertex_ai/batches/test_handler.py b/tests/unit/llms/vertex_ai/batches/test_handler.py index 9ce7fa2e500..33bca2e4ffe 100644 --- a/tests/unit/llms/vertex_ai/batches/test_handler.py +++ b/tests/unit/llms/vertex_ai/batches/test_handler.py @@ -398,6 +398,7 @@ def test_create_batch_sync_httpstatuserror_carries_vertex_status_body_and_header assert exc_info.value.response.status_code == 429 assert exc_info.value.response.headers["retry-after"] == "30" assert handle_exception_on_proxy(exc_info.value).code == "429" + assert (getattr(exc_info.value, "message", None), getattr(exc_info.value, "text", None)) == ("boom", "boom") def test_create_batch_input_file_id_without_model_raises_400_before_post(): @@ -837,6 +838,7 @@ def test_create_batch_async_httpstatuserror_carries_vertex_status_body_and_heade assert exc_info.value.response.status_code == 429 assert exc_info.value.response.headers["retry-after"] == "30" assert handle_exception_on_proxy(exc_info.value).code == "429" + assert (getattr(exc_info.value, "message", None), getattr(exc_info.value, "text", None)) == ("boom", "boom") def test_async_retrieve_batch_non_200_raises(): diff --git a/tests/unit/llms/vertex_ai/files/test_vertex_ai_files_transformation.py b/tests/unit/llms/vertex_ai/files/test_vertex_ai_files_transformation.py index 6f5f5e3638d..378cf4f6735 100644 --- a/tests/unit/llms/vertex_ai/files/test_vertex_ai_files_transformation.py +++ b/tests/unit/llms/vertex_ai/files/test_vertex_ai_files_transformation.py @@ -2381,13 +2381,15 @@ class TestVertexClaudeBatch: assert bad_row["response"] is None assert bad_row["error"]["code"] == "transformation_error" - def test_claude_row_without_custom_id_has_no_custom_id_field(self, config): - entry: Final = _claude_chat_entry(None, "hi", max_tokens=10) + def test_claude_line_without_custom_id_is_a_400_at_upload(self, config): + from litellm.llms.vertex_ai.common_utils import VertexAIError - (row,) = _uploaded_rows(config, CLAUDE_DEPLOYMENT, [entry]) + with pytest.raises(VertexAIError, match=r"line 2 .*custom_id") as exc_info: + _uploaded_rows( + config, CLAUDE_DEPLOYMENT, [_claude_chat_entry("r1", "hi", max_tokens=10), _claude_chat_entry(None, "hi")] + ) - assert set(row) == {"request"} - assert row["request"]["anthropic_version"] == "vertex-2023-10-16" + assert exc_info.value.status_code == 400 @pytest.mark.asyncio async def test_streamed_claude_output_is_transformed(self, config):