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
Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_vertex_batch_create_error_propagation
mateo-berri2b58053561e7
diff --cc litellm/llms/vertex_ai/batches/handler.pyindex f0fd5480c75,29ebd5c2a75..8e36c2a0faa--- a/litellm/llms/vertex_ai/batches/handler.py+++ b/litellm/llms/vertex_ai/batches/handler.py@@@ -98,11 -99,9 +99,11 @@@ class VertexAIBatchPrediction(VertexLLM)if response.status_code != 200:- raise Exception(f"Error: {response.status_code} {response.text}")+ raise VertexAIError(+ status_code=response.status_code, message=f"Error: {response.status_code} {response.text}"+ )- _json_response = response.json()+ _json_response: Final = response.json()vertex_batch_response = VertexAIBatchTransformation.transform_vertex_ai_batch_response_to_openai_batch_response(response=_json_response)@@@ -132,11 -131,9 +133,11 @@@)raiseif response.status_code != 200:- raise Exception(f"Error: {response.status_code} {response.text}")+ raise VertexAIError(+ status_code=response.status_code, message=f"Error: {response.status_code} {response.text}"+ )- _json_response = response.json()+ _json_response: Final = response.json()vertex_batch_response = VertexAIBatchTransformation.transform_vertex_ai_batch_response_to_openai_batch_response(response=_json_response)@@@ -246,11 -243,9 +247,11 @@@)if response.status_code != 200:- raise Exception(f"Error: {response.status_code} {response.text}")+ raise VertexAIError(+ status_code=response.status_code, message=f"Error: {response.status_code} {response.text}"+ )- _json_response = response.json()+ _json_response: Final = response.json()vertex_batch_response = VertexAIBatchTransformation.transform_vertex_ai_batch_response_to_openai_batch_response(response=_json_response)@@@ -298,11 -293,9 +299,11 @@@headers=headers,)if response.status_code != 200:- raise Exception(f"Error: {response.status_code} {response.text}")+ raise VertexAIError(+ status_code=response.status_code, message=f"Error: {response.status_code} {response.text}"+ )- _json_response = response.json()+ _json_response: Final = response.json()vertex_batch_response = VertexAIBatchTransformation.transform_vertex_ai_batch_response_to_openai_batch_response(response=_json_response)@@@ -373,12 -366,10 +374,12 @@@)if response.status_code != 200:- raise Exception(f"Error: {response.status_code} {response.text}")+ raise VertexAIError(+ status_code=response.status_code, message=f"Error: {response.status_code} {response.text}"+ )- _json_response = response.json()- vertex_batch_response = (+ _json_response: Final = response.json()+ vertex_batch_response: Final = (VertexAIBatchTransformation.transform_vertex_ai_batch_list_response_to_openai_list_response(response=_json_response)@@@ -400,12 -391,10 +401,12 @@@params=params,)if response.status_code != 200:- raise Exception(f"Error: {response.status_code} {response.text}")+ raise VertexAIError(+ status_code=response.status_code, message=f"Error: {response.status_code} {response.text}"+ )- _json_response = response.json()- vertex_batch_response = (+ _json_response: Final = response.json()+ vertex_batch_response: Final = (VertexAIBatchTransformation.transform_vertex_ai_batch_list_response_to_openai_list_response(response=_json_response)@@@ -487,12 -476,10 +488,12 @@@raiseif response.status_code != 200:- raise Exception(f"Error: {response.status_code} {response.text}")+ raise VertexAIError(+ status_code=response.status_code, message=f"Error: {response.status_code} {response.text}"+ )# HTTPHandler.get() does not accept a timeout parameter- retrieve_response = sync_handler.get(+ retrieve_response: Final = sync_handler.get(url=retrieve_api_base,headers=headers,)@@@ -502,12 -489,9 +503,12 @@@retrieve_response.status_code,retrieve_response.text[:1000],)- raise Exception(f"Error: {retrieve_response.status_code} {retrieve_response.text}")+ raise VertexAIError(+ status_code=retrieve_response.status_code,+ message=f"Error: {retrieve_response.status_code} {retrieve_response.text}",+ )- _json_response = retrieve_response.json()+ _json_response: Final = retrieve_response.json()vertex_batch_response = VertexAIBatchTransformation.transform_vertex_ai_batch_response_to_openai_batch_response(response=_json_response)@@@ -538,12 -522,10 +539,12 @@@)raiseif response.status_code != 200:- raise Exception(f"Error: {response.status_code} {response.text}")+ raise VertexAIError(+ status_code=response.status_code, message=f"Error: {response.status_code} {response.text}"+ )# AsyncHTTPHandler.get() does not accept a timeout parameter- retrieve_response = await client.get(+ retrieve_response: Final = await client.get(url=retrieve_api_base,headers=headers,)@@@ -553,12 -535,9 +554,12 @@@retrieve_response.status_code,retrieve_response.text[:1000],)- raise Exception(f"Error: {retrieve_response.status_code} {retrieve_response.text}")+ raise VertexAIError(+ status_code=retrieve_response.status_code,+ message=f"Error: {retrieve_response.status_code} {retrieve_response.text}",+ )- _json_response = retrieve_response.json()+ _json_response: Final = retrieve_response.json()vertex_batch_response = VertexAIBatchTransformation.transform_vertex_ai_batch_response_to_openai_batch_response(response=_json_response)diff --cc litellm/llms/vertex_ai/batches/transformation.pyindex e4299bcf2a0,66951715488..f284b47292b--- a/litellm/llms/vertex_ai/batches/transformation.py+++ b/litellm/llms/vertex_ai/batches/transformation.py@@@ -1,5 -1,4 +1,5 @@@- from typing import Any, Dict, Optional+ from typing import Any, Final+from urllib.parse import unquotefrom litellm._uuid import uuidfrom litellm.llms.vertex_ai.common_utils import (@@@ -201,42 -199,18 +201,42 @@@ class VertexAIBatchTransformationgcs_file_uri format: gs://litellm-testing-bucket/litellm-vertex-files/publishers/google/models/gemini-1.5-flash-001/e9412502-2c91-42a6-8e61-f5c294cc0fc8returns: "publishers/google/models/gemini-1.5-flash-001"++ Raises a 400 `VertexAIError` when the uri carries no parseable model path."""- model = cls._parse_model_from_gcs_file(gcs_file_uri)- from urllib.parse import unquote++ model: Final = cls._parse_model_from_gcs_file(gcs_file_uri)+ if model is None:+ raise VertexAIError(+ status_code=400,+ message=(+ "Vertex AI batch creation requires the model to be part of `input_file_id`, but "+ f"'{gcs_file_uri}' contains no 'publishers/<publisher>/models/<model>' path segment. "+ "Either upload the input file through LiteLLM (POST /v1/files with "+ "custom_llm_provider=vertex_ai), which encodes the model into the returned file id, or "+ "pass a uri of the form "+ "gs://<bucket>/<prefix>/publishers/<publisher>/models/<model>/<file>"+ ),+ )+ return model- decoded_uri: Final = unquote(gcs_file_uri)+ @classmethod+ def _parse_model_from_gcs_file(cls, gcs_file_uri: str) -> str | None:+ """+ Returns the `publishers/<publisher>/models/<model>` path from a gcs uri, or None if the uri+ does not contain one.+ """+ _, separator, model_path = unquote(gcs_file_uri).partition("publishers/")+ if not separator:+ return None- parts = model_path.split("/")- model_path: Final = decoded_uri.split("publishers/")[1]+ parts: Final = model_path.split("/")- model: Final = f"publishers/{'/'.join(parts[:3])}"- return model+ if len(parts) < 3 or parts[1] != "models" or not parts[2]:+ return None++ return f"publishers/{'/'.join(parts[:3])}"@classmethod- def is_unmanaged_gcs_batch_input_file_id(cls, input_file_id: Optional[str]) -> bool:+ def is_unmanaged_gcs_batch_input_file_id(cls, input_file_id: str | None) -> bool:"""Returns True if `input_file_id` is a raw gs:// Vertex batch input file (i.e. not aLiteLLM-managed unified file id) with a `publishers/` model path that