Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
AI and LLM integration

Generated _pb2.py files fail to import against an older protobuf

Your error

ImportError: cannot import name 'runtime_version' from 'google.protobuf'1 more line it can show

Fix it fast

Most common cause, 1 of 2

The runtime is too old to have the runtime_version module at all

The clean fix is a runtime that is new enough.

Fix
# option A: newer stack
pip install -U "tensorflow==2.21.0" "protobuf>=6.31.1"

# option B: keep protobuf 4.x, rebuild the gencode with protoc 25.9
curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v25.9/protoc-25.9-linux-x86_64.zip
unzip -o protoc-25.9-linux-x86_64.zip -d "$HOME/protoc25"
"$HOME/protoc25/bin/protoc" object_detection/protos/*.proto --python_out=.
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ImportError: cannot import name 'runtime_version' from 'google.protobuf' means your _pb2.py files came from protoc 27 or newer, while the installed protobuf package predates that module (4.x or early 5.x). Newer generated code on an older runtime is never supported. Upgrade protobuf to at least the generated version, or regenerate the files with a protoc that matches your runtime.

  • Applies to protobuf (Python) 7.36.2, protoc 36.2 and 25.9, TensorFlow 2.21.0 (needs protobuf >= 6.31.1)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the installed runtime, the protoc on your PATH, and the gencode version written inside one failing _pb2.py file. A runtime below 5.27 explains the ImportError (cause 1). A runtime of 5.27 or later that is still below the gencode numbers explains the VersionError (cause 2).

Run this first
python -c "import google.protobuf as p; print(p.__version__)"
protoc --version
grep -m1 -A6 "ValidateProtobufRuntimeVersion" object_detection/protos/string_int_label_map_pb2.py

Then find the line you see in this list and open its fix:

  1. 1The runtime is too old to have the runtime_version module at allpython prints protobuf 4.x or 3.x, and the _pb2.py file starts with from google.protobuf import runtime_version as _runtime_version
  2. 2The runtime has runtime_version but is older than the gencodeThe traceback ends in VersionError and names both numbers, for example gencode 6.31.1 runtime 5.29.3

What does the error look like?

  • ImportError: cannot import name 'runtime_version' from 'google.protobuf'
  • google.protobuf.runtime_version.VersionError: Detected incompatible Protobuf Gencode/Runtime versions when loading object_detection/protos/string_int_label_map.proto: gencode 6.31.1 runtime 5.29.3. Runtime version cannot be older than the linked gencode version.

Cause 1 of 2

The runtime is too old to have the runtime_version module at all

You seepython prints protobuf 4.x or 3.x, and the _pb2.py file starts with from google.protobuf import runtime_version as _runtime_version

Check

The ImportError for runtime_version means a protobuf runtime older than 5.27 is loading code from protoc 27 or later. From release 27.0 the Python generator writes that import plus a ValidateProtobufRuntimeVersion call into every _pb2.py, and the module only exists in runtimes from the same release on. TensorFlow 2.15 to 2.17 cap protobuf below 5, so compiling protos with a current protoc next to them produces exactly this.

Check
pip show protobuf tensorflow | grep -E "^(Name|Version)"

Fix

The clean fix is a runtime that is new enough. TensorFlow 2.21 already requires protobuf 6.31.1 or later, so upgrading TensorFlow and protobuf together removes the cap. If you must keep an old TensorFlow, regenerate the files with protoc 25.x, the last compiler line for protobuf Python 4.x.

Fix
# option A: newer stack
pip install -U "tensorflow==2.21.0" "protobuf>=6.31.1"

# option B: keep protobuf 4.x, rebuild the gencode with protoc 25.9
curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v25.9/protoc-25.9-linux-x86_64.zip
unzip -o protoc-25.9-linux-x86_64.zip -d "$HOME/protoc25"
"$HOME/protoc25/bin/protoc" object_detection/protos/*.proto --python_out=.

Verify

Importing the generated module succeeds without any traceback.

Verify
python -c "from object_detection.protos import string_int_label_map_pb2; print('ok')"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

The runtime has runtime_version but is older than the gencode

You seeThe traceback ends in VersionError and names both numbers, for example gencode 6.31.1 runtime 5.29.3

Check

Here the module exists, so the import works, and the check inside it refuses the pairing. Protobuf's cross-version guarantee says generated code must never run on a runtime that predates the protoc that produced it. runtime_version.py raises VersionError whenever the runtime version, compared as major, then minor, then patch, comes out lower than the gencode version.

Check
python -c "import google.protobuf as p; print(p.__version__)"

Fix

Install a runtime at least as new as the gencode version from the message, as long as your other packages allow it. If a pin blocks that, regenerate with a protoc whose Python version is not newer than your runtime; protoc 26 to 29 map to Python 5.x, 30 to 33 to 6.x, and 34 onward to 7.x.

Fix
pip install -U "protobuf>=6.31.1"
pip check

Verify

pip check reports no broken requirements and the failing import now loads.

Verify
python -c "from object_detection.protos import string_int_label_map_pb2"

Confirmed by protobuf.dev

Did this fix it?

How do I stop it coming back?

  • Pin the protoc or grpcio-tools version in the repo next to the protobuf pin, and generate code in CI with exactly that compiler.
  • Never compile protos with whatever protoc the OS package manager installed; record protoc --version in the build log.
  • Commit the _pb2.py files, or rebuild them in the same Docker image that runs the code, so runtime and gencode cannot drift apart.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.