Skip to content

Commit

Permalink
Fix buggy response handling in vertexai_client (#2614)
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanmai authored May 7, 2024
1 parent 4d87f3f commit 767a9e7
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/helm/clients/vertexai_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def do_it() -> Dict[str, Any]:

# Depending on the version of the Vertex AI library and the type of prompt blocking,
# prompt blocking can show up in many ways, so this defensively handles most of these ways
if response.prompt_feedback.block_reason:
if response.prompt_feedback and response.prompt_feedback.block_reason:
raise VertexAIContentBlockedError(
f"Prompt blocked with reason: {response.prompt_feedback.block_reason}"
)
Expand All @@ -209,8 +209,10 @@ def do_it() -> Dict[str, Any]:
# content blocking can show up in many ways, so this defensively handles most of these ways
if candidate.finish_reason in VertexAIChatClient.CONTENT_BLOCKED_FINISH_REASONS:
raise VertexAIContentBlockedError(f"Content blocked with reason: {candidate.finish_reason}")
if not candidate.content:
raise VertexAIContentBlockedError(f"No content in candidate: {candidate}")
if not candidate.content.parts:
raise VertexAIContentBlockedError(f"No parts in candidate: {candidate}")
raise VertexAIContentBlockedError(f"No content parts in candidate: {candidate}")
predictions.append({"text": candidate.content.text})
# TODO: Extract more information from the response
return {"predictions": predictions}
Expand Down Expand Up @@ -330,7 +332,7 @@ def do_it() -> Dict[str, Any]:
)
# Depending on the version of the Vertex AI library and the type of prompt blocking,
# prompt blocking can show up in many ways, so this defensively handles most of these ways
if response.prompt_feedback.block_reason:
if response.prompt_feedback and response.prompt_feedback.block_reason:
raise VertexAIContentBlockedError(
f"Prompt blocked with reason: {response.prompt_feedback.block_reason}"
)
Expand All @@ -345,8 +347,10 @@ def do_it() -> Dict[str, Any]:
# content blocking can show up in many ways, so this defensively handles most of these ways
if candidate.finish_reason in VertexAIChatClient.CONTENT_BLOCKED_FINISH_REASONS:
raise VertexAIContentBlockedError(f"Content blocked with reason: {candidate.finish_reason}")
if not candidate.content:
raise VertexAIContentBlockedError(f"No content in candidate: {candidate}")
if not candidate.content.parts:
raise VertexAIContentBlockedError(f"No parts in candidate: {candidate}")
raise VertexAIContentBlockedError(f"No content parts in candidate: {candidate}")
return {"predictions": [{"text": candidate.text}]}

raw_cache_key = {"model_name": model_name, "prompt": prompt_key, **parameters}
Expand Down

0 comments on commit 767a9e7

Please sign in to comment.