fix(vertex_ai): keep Vertex's error body on batch create errors and require custom_id on Claude lines

This commit is contained in:
mateo-berri 2026-10-29 11:52:37 -07:00
parent da05e19a53
commit 757021ab19
4 changed files with 16 additions and 10 deletions

View file

@ -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,
)

View file

@ -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}

View file

@ -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():

View file

@ -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):