Skip to content

[BUG] Generated buf.validate message are not part of the pypi package #339

@lukasbindreiter

Description

@lukasbindreiter

Description

The current version of the protovalidate python package on pypi is not importable due to a missing buf.validate module.

>>> import protovalidate
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    import protovalidate
  File "/Users/lukasbindreiter/protovalidate-test/.venv/lib/python3.13/site-packages/protovalidate/__init__.py", line 15, in <module>
    from protovalidate import config, validator
  File "/Users/lukasbindreiter/protovalidate-test/.venv/lib/python3.13/site-packages/protovalidate/validator.py", line 19, in <module>
    from buf.validate import validate_pb2  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'buf'

Steps to Reproduce

It's reproducable in a fresh python venv:

  1. uv init
  2. uv add protovalidate
  3. uv run python
  4. >>> import protovalidate
  5. raises ModuleNotFoundError: No module named 'buf'

Expected Behavior

When installing protovalidate with pip/uv it should also be importable and useable without additional workarounds of adding other dependencies externally.

Actual Behavior

The import fails, failing to resolve buf.validate

Environment

  • Operating System: macOS
  • Protovalidate Version: 0.13.0

Possible Solution

I think the best possible solution would be to just bundle the contents of the gen folder in the generated python package (by specifying it as source in pyproject.toml), to make sure that pip install protovalidate also creates the buf/validate namespace package in lib/python3.X/site-packages correctly.

Alternatively, protovalidate could depend on another python package that just provides buf.validate as module. However it is important that that module is in the official pypi registry, and not on https://buf.build/gen/python (reasoning see below).

Additional Context

We could work around this by using the generated SDK feature from buf, e.g.

python3 -m pip install bufbuild-protovalidate-grpc-python==1.73.1.1.20250717185734+6c6e0d3c608e --extra-index-url https://buf.build/gen/python

However, we are developing a library, not an application, so we cannot do that, because we cannot add bufbuild-protovalidate-grpc-python as dependency to our library, otherwise it would no longer be pip installable.

Alternatively, we could set include_imports=True when generating our stubs.

This will generate the required files in a package that looks like our_namespace.buf.validate.

With that everything does work - with one huge caveat however:
We are now incompatible with every other library that also uses protovalidate and works around this issue in the same way.

Because as soon as some code attempts to import the generated stub from multiple python modules, e.g.

import our_namespace.buf.validate
import other_library.buf.validate
import buf.validate  # or even an application using the generated SDK featrue

this will error during import, because of a name conflict in the global protobuf descriptor pool.

Therefore I think it is best to just bundle buf.validate directly in the protovalidate pypi package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions