fix(vertex_httpx.py): re-raise vertex content policy violation error

Fixes https://github.com/BerriAI/litellm/issues/4270
This commit is contained in:
Krrish Dholakia 2024-06-18 19:00:35 -07:00
parent aef5cf3f22
commit f41c443abb
7 changed files with 309 additions and 37 deletions

View file

@ -6235,7 +6235,11 @@ def exception_type(
llm_provider="sagemaker",
response=original_exception.response,
)
elif custom_llm_provider == "vertex_ai":
elif (
custom_llm_provider == "vertex_ai"
or custom_llm_provider == "vertex_ai_beta"
or custom_llm_provider == "gemini"
):
if (
"Vertex AI API has not been used in project" in error_str
or "Unable to find your project" in error_str
@ -6254,6 +6258,13 @@ def exception_type(
),
litellm_debug_info=extra_information,
)
if "400 Request payload size exceeds" in error_str:
exception_mapping_worked = True
raise ContextWindowExceededError(
message=f"VertexException - {error_str}",
model=model,
llm_provider=custom_llm_provider,
)
elif (
"None Unknown Error." in error_str
or "Content has no parts." in error_str
@ -6287,13 +6298,13 @@ def exception_type(
)
elif "The response was blocked." in error_str:
exception_mapping_worked = True
raise UnprocessableEntityError(
message=f"VertexAIException UnprocessableEntityError - {error_str}",
raise ContentPolicyViolationError(
message=f"VertexAIException ContentPolicyViolationError - {error_str}",
model=model,
llm_provider="vertex_ai",
litellm_debug_info=extra_information,
response=httpx.Response(
status_code=422,
status_code=400,
request=httpx.Request(
method="POST",
url=" https://cloud.google.com/vertex-ai/",
@ -6345,6 +6356,27 @@ def exception_type(
),
),
)
if original_exception.status_code == 401:
exception_mapping_worked = True
raise AuthenticationError(
message=f"VertexAIException - {original_exception.message}",
llm_provider=custom_llm_provider,
model=model,
)
if original_exception.status_code == 404:
exception_mapping_worked = True
raise NotFoundError(
message=f"VertexAIException - {original_exception.message}",
llm_provider=custom_llm_provider,
model=model,
)
if original_exception.status_code == 408:
exception_mapping_worked = True
raise Timeout(
message=f"VertexAIException - {original_exception.message}",
llm_provider=custom_llm_provider,
model=model,
)
if original_exception.status_code == 429:
exception_mapping_worked = True
@ -6374,6 +6406,13 @@ def exception_type(
request=httpx.Request(method="completion", url="https://github.com/BerriAI/litellm"), # type: ignore
),
)
if original_exception.status_code == 503:
exception_mapping_worked = True
raise ServiceUnavailableError(
message=f"VertexAIException - {original_exception.message}",
llm_provider=custom_llm_provider,
model=model,
)
elif custom_llm_provider == "palm" or custom_llm_provider == "gemini":
if "503 Getting metadata" in error_str:
# auth errors look like this