-
Notifications
You must be signed in to change notification settings - Fork 392
Description
Description:
The Vertex AI API is returning FinishReason
enum values that are not defined in the google-cloud-aiplatform
SDK. When this occurs, the proto-plus
library issues a UserWarning
and passes the raw integer value to the application instead of an enum object.
This causes a contract violation for downstream libraries, such as langchain-google-vertexai
, which expect an enum object with a .name
attribute. The application then crashes with an AttributeError: 'int' object has no attribute 'name'
.
This was originally reported in langchain-google
issue #1111.
To Reproduce
The issue is intermittent and occurs when the Vertex AI API returns a new or undocumented FinishReason
. The traceback demonstrates the failure path:
- The API returns a
Candidate
object wherefinish_reason
is an integer (e.g.,15
) not present in the client-side enum definition. proto-plus
warns:UserWarning: Unrecognized FinishReason enum value: 15
.- Code that attempts to access
candidate.finish_reason.name
fails.
Expected behavior
The google.cloud.aiplatform_v1beta1.types.content.Candidate.FinishReason
enum should be updated to include all possible values returned by the API. The client library should always provide an enum object, not a raw integer, so that consumers can reliably access attributes like .name
.
Workaround
Downstream consumers are forced to implement defensive code to handle this issue, as shown in the provided snippet. The logic checks if the finish_reason
attribute has a .name
attribute before accessing it.
Brief code change from the patch:
finish_reason = getattr(candidate, "finish_reason", None)
finish_reason_name = None
if finish_reason is not None:
if hasattr(finish_reason, "name"):
finish_reason_name = finish_reason.name
else:
# This path handles the unexpected integer value.
logging.warning(
"Unrecognized FinishReason enum value: %s. Setting to 'STOP'.", finish_reason
)
finish_reason_name = gapic_content_types.Candidate.FinishReason.STOP.name
While this workaround prevents crashes, the root cause is the out-of-sync enum definition in google-cloud-aiplatform
.
Environment details
google-cloud-aiplatform
: 1.111.0proto-plus
: 1.26.1protobuf
: 5.29.5