Generated Python SDKs are now available in the BSR

Nov 29, 2023/3 min read

Authored by Stefan VanBuren

Today we’re announcing the release of Python support for generated SDKs in the Buf Schema Registry (BSR)! Python engineers no longer need to manage a Protobuf toolchain or generate code locally—they can now download pre-packaged generated code for their Protobuf schemas from the BSR using package management tools like pip, Poetry, Conda, and others that support PEP 503 - Simple Repository API. These packages are automatically made available when schema changes are pushed to the BSR.

Python package support complements our existing support for generated SDKs in GoJavaScript/TypeScript, Java/Kotlin, and Swift, enabling clients across many platforms to consume generated packages from Protobuf schemas using their native dependency managers.

We've updated each BSR module’s Generated SDKs tab with instructions on how to get started with Python generated SDKs using pip. You can also find instructions and more details in our documentation.

Using generated SDKs with pip

Let’s create a small project using pip to pull a package from the Buf Python Repository and then use it to interact with the Connect Demo API.

First, ensure that python3 is on your $PATH. Then, create a new directory for the example:

$ mkdir buf-python-example
$ cd buf-python-example

Next, create and activate a virtual environment to install your dependencies in:

$ python3 -m venv venv
$ source ./venv/bin/activate

After that, install the dependency while specifying the Buf Python Repository as an extra index:

$ python3 -m pip install connectrpc-eliza-grpc-python --extra-index-url https://buf.build/gen/python

Then, paste the following code into a file called main.py:

import grpc

from connectrpc.eliza.v1.eliza_pb2_grpc import ElizaServiceStub
from connectrpc.eliza.v1.eliza_pb2 import SayRequest

def main():
    channel_credentials = grpc.ssl_channel_credentials()
    channel = grpc.secure_channel("demo.connectrpc.com", channel_credentials)
    stub = ElizaServiceStub(channel)

    say_response = stub.Say(SayRequest(sentence="Hello there!"))
    print(say_response.sentence)

if __name__ == "__main__":
    main()

From here, we can execute main.py to interact with the Connect Demo API:

$ python3 -m main
Hello...I'm glad you could drop by today.

Type checking

The above example installs just the basics we need to run a Python script, but we’ll also want to be able to use tools like mypy or pyright to type check the code. For that, we can use type-stub packages that are generated by plugins like pyi and mypy-protobuf.

Let’s install mypy first:

$ python3 -m pip install mypy

Next, we’ll install our type-stub package:

$ python3 -m pip install connectrpc-eliza-protocolbuffers-pyi --extra-index-url https://buf.build/gen/python

Now we can type check our script with mypy:

$ python3 -m mypy main.py
Output
Success: no issues found in 1 source file

Where to go from here

Head over to our documentation to get started with generated SDKs for Python (or any of our other supported languages). As always, we’d love to hear from you—feedback and questions are welcome on the Buf Slack!

Get started for free with the Buf Schema Registry