Skip to content

FinishReason enum in google.cloud.aiplatform_v1beta1 is out of sync with API, causing AttributeError #5805

@nkilleen-work

Description

@nkilleen-work

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:

  1. The API returns a Candidate object where finish_reason is an integer (e.g., 15) not present in the client-side enum definition.
  2. proto-plus warns: UserWarning: Unrecognized FinishReason enum value: 15.
  3. 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.0
  • proto-plus: 1.26.1
  • protobuf: 5.29.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: vertex-aiIssues related to the googleapis/python-aiplatform API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions