Skip to content

faststream-gcp-pubsub#

A Google Cloud Pub/Sub broker for FastStream, maintained by the community.

service.py
from faststream import FastStream
from pydantic import BaseModel

from faststream_gcp_pubsub import PubSub, PubSubBroker


class Order(BaseModel):
    id: int
    amount: float


broker = PubSubBroker(project_id="my-project")
app = FastStream(broker)

shipments = broker.publisher("shipments")


@broker.subscriber(PubSub("orders-billing"), topic="orders")
@shipments
async def bill(order: Order) -> dict[str, int]:
    # the return value is published to the `shipments` topic
    return {"order_id": order.id}


@app.after_startup
async def place_order() -> None:
    await broker.publish(Order(id=1, amount=9.5), "orders")
faststream run service:app

The handler reads the orders-billing subscription of the orders topic, decodes the JSON body into Order, acknowledges the message once the handler returns, and publishes its result to shipments.

Install#

pip install faststream-gcp-pubsub
uv add faststream-gcp-pubsub

The package needs Python 3.10 or newer and FastStream 0.7. The extras are otel (OpenTelemetry), prometheus and cli (the faststream command):

pip install "faststream-gcp-pubsub[cli,otel,prometheus]"

What you get#

  • Subscribers read a subscription through a streaming pull and keep each message leased until the handler settles it.
  • Publishers send one message or a batch, with attributes, ordering keys and message ids.
  • Topics, subscriptions, dead-letter and retry policies are created at startup when you declare them.
  • Acknowledgement follows the Pub/Sub model: ack, nack for immediate redelivery, and reject as an acknowledgement.
  • Flow control bounds the messages and bytes held at once; acks travel in batches.
  • TestPubSubBroker runs the service in process, with routing and validation like the real service.
  • The Pub/Sub emulator, Application Default Credentials, service account keys and regional endpoints are supported.
  • FastAPI, OpenTelemetry, Prometheus and AsyncAPI integrations are included.

Where to go next#