Jump to Content

Easily integrate machine learning models into applications with Vertex AI integration for Cloud Spanner

November 18, 2022
Joe Yong

Senior Product Manager

Adrian Wisniewski

Staff Software Engineer

Try Google Cloud

Start building on Google Cloud with $300 in free credits and 20+ always free products.

Free trial

Cloud Spanner is a fully managed relational database that provides industry-leading  consistency and availability at any scale. Organizations of all sizes in industries like financial services, retail, and games rely on Spanner to run their mission critical applications, but efficiently running line-of-business applications is often not enough. They want to react to business or customer events faster and in a scalable way by leveraging machine learning (ML) models instead of relying on manual actions. Tasks like fraud detection, algorithmic trading, and bot detection are time sensitive and can have unpredictable loads. Spanner integration with Vertex AI, Google Cloud’s ML platform, lets users leverage ML models in Vertex AI using a simple SQL query in Spanner. Built-in integration with Vertex AI, available now in preview, unifies operational databases and AI, making it simpler to consume published ML models and to build AI-powered applications faster.  

Application developers often have limited bandwidth to integrate ML services with an application, as doing so requires a learning curve and long-term maintenance overhead. These developers have to create a separate module within their application or a separate application/service entirely to interact with ML services like Vertex AI. 

Conceptually, the module would send data to Vertex AI’s prediction model and pass the results to the application that will make a decision based on the results. Such an implementation leads to expansion of the application, added latency to decision making, and more moving parts to maintain. With Spanner Vertex AI integration, in contrast, developers can easily access models built by data scientists and apply them to database transactions using familiar SQL.

This is similar to how BigQuery ML natively supports ML to make it easier for users to leverage ML models within applications, but with an important distinction: Spanner’s Vertex AI integration allows predictions to be made on live transactions, while BigQuery ML analyzes transactions that have already completed on data stored in BigQuery. Spanner’s Vertex AI integration is important for use cases such as fraud detection or detection of toxic players, where you want to make the best decision possible during the transaction or event instead of detecting a poor decision later. This direct integration with Spanner lowers the entry barrier for adding ML to applications, provides lower latency and shorter times transaction locks are held, making it less susceptible to lock contention, an important benefit for busy transactional systems.

Getting started with Vertex AI integration

In this release of Vertex AI integration, we’ve made it easy to consume any existing model already published in Vertex AI. If you are unfamiliar with Vertex AI, check out this quick start video and documentation on training a tabular data model. Let’s walk through an example of how a games company uses Vertex AI integration with Spanner to protect its users and  company from toxic players. 

Use case example: toxic player detection

Consider Cymbal Entertainment, a hypothetical games and social media company, that leverages Spanner to store information about all user activities, including chats, and comments in their forums. They want their systems to automatically trigger an investigation into potential user misconduct, especially toxic chat/comment behavior, as soon as that toxic behavior occurs. In cases like in-game chat/messaging, they want to detect harmful content as it is submitted by players. They have to evaluate the level of toxicity of the reported user’s comments and to make a decision on whether to flag the comment, suspend the user’s account, or apply any other reasonable measures.


This has to be done almost instantly or it may result in altercations or worse, players may abandon the platform completely. Cymbal Entertainment prides itself for its zero tolerance policy for online bullying and its millions of diverse users bear testimony to its success. Toxic users can damage their hard earned reputation and affect their business overnight. The size of Cymbal’s community and 24x7 activity level of its users makes any solution that relies on humans to prevent such events infeasible.

Preparing the model for Spanner

To automate the process of chat or comment toxicity evaluation, Cymbal Entertainment uses ML. They train a model following Vertex AI Text Classification guide using either data existing in their system or a publicly available dataset like Toxicity Comment Classification from Kaggle. Once the model is trained and deployed to a Vertex AI endpoint, it can be registered in Spanner:


The ToxicityClassifier model takes the comment text as input and outputs two arrays: display names and confidences. The former contains all the labels seen in the training dataset. The latter indicates the confidence score for the positionally corresponding label. In the following example, the result will indicate a toxic comment:


Inline toxicity prediction

While inserting comments into the Comments table, they calculate the set of toxicity ratings using the ToxicityClassifier model. According to their company policy, comments that contain identity attacks, severe toxicity, or threats are not allowed on the platform and need to be banned automatically by setting the is_banned flag of the comment record to true. Other comments that contain high ratings of toxicity can be marked as suspicious and re-directed for further investigation. The whole described operation can be executed using the following SQL statement:


Apart from the simplicity and better overall query performance with this approach, the data, decision and reasoning are stored in the database all in the same step. Accomplishing the same using external functions will require multiple steps and database transactions. 

As you can see (and try it for yourself), all the work needed to enrich Cymbal Entertainment’s application with ML capabilities is done by adding a few SQL commands to their existing code. The subscriptions and payments processing team can also add ML capabilities for payment fraud detection in the same way and just as easily.

Availability and pricing

Vertex AI integration with Spanner is currently in preview and available in all regions Spanner is available. There is no additional charge from Spanner for using this feature. Customers will need access to Vertex AI models and are subject to Vertex AI pricing based on the usage type.

Get started today

It has never been easier to enhance your Spanner applications with ML capabilities so try it out today. 

Use the sample in this blog or check out  documentation for more information.

  • If you are new to Spanner, try Spanner at no charge with a 90-day free trial instance.

  • Learn more about how Spanner provides unlimited scale and up to 99.999% availability.

Posted in