Ver en Español
You can now call Gemini directly from Google Cloud Pub/Sub... but should you?
Jun 24, 2026
Updated: Jun 25, 2026

You can now call Gemini directly from Google Cloud Pub/Sub... but should you?

Throughout our careers, software developers and architects have always been taught to be careful about where we place business logic. Should we hide it in frontend components? In database triggers? In the middleware? The classic recommendation is always to keep these rules in centralized services so we don't lose track of them.

However, Google Cloud has just shipped a feature that challenges this notion and forces us to rethink our architectures: it's now incredibly simple to call an LLM directly from a topic or subscription in Google Cloud Pub/Sub.

When does it make sense to do this? What does it look like in practice? Today we'll break it down, build a step by step implementation, and debate whether you really should do it.

The power of SMTs (Single Message Transforms) in Pub/Sub

If you've ever dealt with data processing pipelines or cloud events, you know we often need to alter a message in flight. For this, Pub/Sub offers Single Message Transforms (SMTs), which let us manipulate a data stream (for example, to clean up sensitive data or change formats) before it reaches the final consumer.

The big news in the ecosystem is the AI Inference SMT. Using this tool, we can call Vertex AI managed models (like our beloved Gemini), as well as third party models, directly in the messaging layer. This can be applied at the Topic level (where all subscriptions receive the AI transformed message) or in a more controlled way at the level of a specific Subscription.

A real world scenario: the "New Employee" event

Imagine you're building an event driven architecture for a Human Resources system. Every time someone is hired, a "new employee" event is emitted to Pub/Sub.

One of the microservices subscribed to this event is in charge of sending a welcome email. Instead of having that microservice build the text and connect to the AI API, we can configure our Pub/Sub subscription to call an LLM directly. That way, the message that reaches the target microservice already comes enriched with a warm, personalized, pre generated welcome email.

Hands on: calling Gemini from Pub/Sub

Let's see how to set this up directly from the Google Cloud console.

1. Create the Topic and the Subscription

First, we create a standard Topic in Pub/Sub. Although we could apply the AI transform right here so it applies to everything, I prefer to be surgical and apply it only on the subscription. So we create a pull based subscription tied to this topic.

Creating the employee-event-topic Topic in the Google Cloud Pub/Sub console
Form to create a pull subscription associated with the topic in the Google Cloud console

2. Chaining transforms with JavaScript

As a web developer, this is my favorite part. The AI Inference SMT expects us to send it a very specific format, but our original event comes in raw. Fortunately, Pub/Sub lets us "chain" transforms.

First, we add an SMT using a JavaScript User Defined Function (UDF). This function will take the employee data, parse it, and build the exact prompt Gemini needs.

Before you copy the code, one clarification that prevents a silent error: the UDF receives the message body in message.data as an already decoded UTF-8 string, not as base64. That's why we can pass it directly to JSON.parse without a prior decode step. If your original event were not valid JSON, that JSON.parse would fail, so make sure the producer publishes a JSON body.

Here's the code in JavaScript:

function transform(message, metadata) {
  // message.data arrives as an already decoded UTF-8 string (not base64),
  // so we can pass it directly to JSON.parse.
  var incomingData = JSON.parse(message.data);
  var promptText = "Generate a personalized welcome email taking into account the person's name, location and role. " + JSON.stringify(incomingData);

  var aiInferenceObject = {
    model: "google/gemini-2.5-flash",
    messages: [
      { role: "user", content: promptText }
    ]
  };

  return {
    data: JSON.stringify(aiInferenceObject),
    attributes: message.attributes || {}
  };
}
The JavaScript UDF (transform function) in the Pub/Sub New Transform editor

GDE tip: Google Cloud has a very handy button called "Test Transforms" that lets us run this JavaScript block inline, directly in the console, before saving. It makes the development experience much smoother.

"Test Transforms" panel running the JavaScript UDF over a sample message

3. Add the AI Inference SMT

Now we add our second SMT to the chain: the AI Inference SMT. Here we configure that we want to use the managed model google/gemini-2.5-flash through Vertex AI. We select the right service account with the correct permissions, and we configure basic AI parameters like the token limit (max tokens) and the temperature (temperature).

Note on model versions (verify): Gemini point versions rotate frequently. Always use the model identifier that Vertex AI exposes in your project when you configure the SMT; google/gemini-2.5-flash is the value used in this guide, but it's worth confirming it in the console before deploying.

AI Inference SMT form with the google/gemini-2.5-flash model, service account and parameters

When we go back to the Test Transforms window, we can test both SMTs together.

"Test Transforms" panel running the full chain: the UDF and then the AI Inference SMT

It's pure magic. The output of our JavaScript function automatically feeds the call to Gemini, and the final output is our original event transformed into a welcome text ready to be dispatched by the email service.

Final SMT output: the personalized welcome email generated by Gemini

The big question: should you do it?

Integrating LLM capabilities into your cloud architecture has never been this easy, fast, and with so little code. Having your "AI powered" Pub/Sub saves you from having to build, deploy, and maintain SDKs in your microservices just to generate simple text.

But as a software architect, my advice is: use it wisely.

It's tempting to put AI everywhere, but remember our initial principle about the visibility of business logic. It's great for enriching messages, sanitizing complex data, or pre computing auxiliary text (like in the email case). However, if that AI transform becomes the critical core of your business, hiding it in the settings of a Pub/Sub subscription can turn debugging into a headache for your team down the road.

Document your architecture well, keep observability high, and leverage the Google Cloud ecosystem to move faster without sacrificing order.

Suggested exercises

  1. Create a Pub/Sub topic and a pull subscription, and publish a test "new employee" JSON message to verify it reaches the consumer unchanged.
  2. Write your own JavaScript UDF that receives that event, validates that message.data is valid JSON, and builds a different prompt (for example, a welcome message in another language).
  3. Add the AI Inference SMT to the chain with google/gemini-2.5-flash and experiment with different temperature and max tokens values using the "Test Transforms" button.
  4. Compare two designs: the AI logic in the SMT versus the same logic in your microservice code. Note what's harder to debug and observe in each case.

3-point summary

  1. Pub/Sub Single Message Transforms, and specifically the AI Inference SMT, let you call Vertex AI models like Gemini directly in the messaging layer, at the topic or subscription level.
  2. By chaining a JavaScript UDF with the AI Inference SMT you can turn a raw event into a prompt and get a response from the LLM without writing or deploying a single SDK in your microservices.
  3. It's an excellent tool for enriching messages, but use it with judgment: if the AI logic is critical, hiding it in a subscription's settings can hurt observability and debugging.

What do you think? Do you see value in enriching your events in real time with LLMs directly in the middleware, or do you prefer to keep that logic explicit in your server code?

Leave me your comments, and if you found this analysis useful, don't forget to explore more articles on the intersection of Web Technologies and Artificial Intelligence here at sebastian gomez.com. Until next time!

Sebastian Gomez

Sebastian Gomez

Creador de contenido principalmente acerca de tecnología.

Leave a Reply

0 Comments

Advertisements

Related Posts

Categorias