Skip to content

Generated Python code protobuf version mismatch #39912

Open
@stinovlas

Description

@stinovlas

What version of gRPC and what language are you using?

grpcio 1.73.0 Python package

What operating system (Linux, Windows,...) and version?

Linux 6.15.2

What runtime / compiler are you using (e.g. python version or version of gcc)

Python 3.13.3

What did you do?

  1. create example.proto file:
    syntax = "proto3";
    message MyMessage {}
  2. create virtualenv and install packages
    python -m venv .venv
    source .venv/bin/activate
    pip install 'grpcio-tools==1.73.0' 'protobuf==6.30.0'
  3. generate pb2 file python -m grpc_tools.protoc -I. --python_out . example.proto
  4. try importing the generated module python -m example_pb2

What did you expect to see?

I'd expect the import to pass.

What did you see instead?

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/jmusilek/git/mwe/example_pb2.py", line 12, in <module>
    _runtime_version.ValidateProtobufRuntimeVersion(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        _runtime_version.Domain.PUBLIC,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
        'example.proto'
        ^^^^^^^^^^^^^^^
    )
    ^
  File "/home/jmusilek/git/mwe/.direnv/python-3.13.3/lib/python3.13/site-packages/google/protobuf/runtime_version.py", line 113, in ValidateProtobufRuntimeVersion
    _ReportVersionError(
    ~~~~~~~~~~~~~~~~~~~^
        'Detected incompatible Protobuf Gencode/Runtime versions when loading'
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        f' {location}: gencode {gen_version} runtime {version}. Runtime version'
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        f' cannot be older than the linked gencode version. {error_prompt}'
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/jmusilek/git/mwe/.direnv/python-3.13.3/lib/python3.13/site-packages/google/protobuf/runtime_version.py", line 50, in _ReportVersionError
    raise VersionError(msg)
google.protobuf.runtime_version.VersionError: Detected incompatible Protobuf Gencode/Runtime versions when loading example.proto: gencode 6.31.0 runtime 6.30.0. Runtime version cannot be older than the linked gencode version. See Protobuf version guarantees at https://protobuf.dev/support/cross-version-runtime-guarantee.

Anything else we should know about your project / environment?

It turns out that grpcio-tools bundle their own version of protobuf and this version is higher than the minimal protobuf requirement in install_requires of grpcio-tools. This leads to an unexpected situation – the generated code has a minimum protobuf version requirement of 6.31.0, even when I have protobuf 6.30.0 installed. There is no indication from the grpcio-tools that I won't be able to import the generated modules with the currently installed version of protobuf.

I don't understand what is install_requires version of protobuf in grpcio-tools supposed to represent. It's definitely not the requirement for runtime protobuf version generated by grpcio-tools which is what I'd expect.

I did read the discussion in #37609, but I don't think the problem has been resolved at all. If you don't use monorepo and have all your gRPC APIs in separate Python packages, this is a huge pain, that should be resolved.

One solution I see is to always declare the bundled protobuf version as a minimal runtime version for grpcio-tools. This is a bare minimum and not really great by itself, because grpcio-tools is just a build dependency and there's no reason why it should be installed in the runtime environment. But still, it would at least give us some idea about the actual minimal required protobuf version of the generated code.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions