public static FinishReason getFinishReason(GenerateContentResponse response) {
    if (response.getCandidatesCount() != 1) {
      throw new IllegalArgumentException(
          String.format(
              "This response should have exactly 1 candidate, but it has %s.",
              response.getCandidatesCount()));
    }
    return response.getCandidates(0).getFinishReason();
  }
Gets the finish reason in a GenerateContentResponse.

@param response a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance
@return the {@link com.google.cloud.vertexai.api.FinishReason} in the response
@throws IllegalArgumentException if the response has 0 or more than 1 candidates
java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ResponseHandler.java