{"id":7517,"library":"protobuf-to-pydantic","title":"Protobuf to Pydantic","description":"protobuf-to-pydantic is a Python library that generates `pydantic.BaseModel` classes with parameter verification functions directly from Protobuf files (proto3 syntax). It supports both runtime object generation and plugin-based code generation, enabling robust data validation for Protobuf messages within Python applications. The library is actively maintained with frequent minor releases, currently at version 0.3.3.1.","status":"active","version":"0.3.3.1","language":"en","source_language":"en","source_url":"https://github.com/so1n/protobuf_to_pydantic","tags":["protobuf","pydantic","code-generation","validation","schema","data-modeling"],"install":[{"cmd":"pip install protobuf-to-pydantic","lang":"bash","label":"Core Installation"},{"cmd":"pip install protobuf-to-pydantic[mypy-protobuf]","lang":"bash","label":"Full Functionality (with MyPy Protobuf integration)"}],"dependencies":[{"reason":"Core functionality relies on Pydantic for model generation and validation.","package":"pydantic"},{"reason":"Required for parsing Protobuf message definitions and handling generated `_pb2.py` files.","package":"protobuf"},{"reason":"Required for compiling .proto files and using the `protoc` plugin mode.","package":"grpcio-tools","optional":true},{"reason":"Optional dependency for enhanced type checking and full functionality via `mypy-protobuf` integration.","package":"mypy-protobuf","optional":true}],"imports":[{"symbol":"msg_to_pydantic_model","correct":"from protobuf_to_pydantic import msg_to_pydantic_model"},{"note":"The import path for custom templates changed in `v0.3.0`. `DescTemplate` was removed. [cite: GitHub v0.3.0 release]","wrong":"from protobuf_to_pydantic.desc_template import DescTemplate","symbol":"Template","correct":"from protobuf_to_pydantic.template import Template"}],"quickstart":{"code":"import sys\nfrom typing import Type\nfrom pydantic import BaseModel, Field\nfrom google.protobuf.message import Message\nfrom google.protobuf import descriptor_pool, descriptor_pb2\nfrom protobuf_to_pydantic import msg_to_pydantic_model\n\n# Simulate a generated protobuf message `demo_pb2.UserMessage`\n# In a real scenario, this would come from `import demo_pb2`\n\npool = descriptor_pool.Default()\ndescriptor = descriptor_pb2.DescriptorProto()\ndescriptor.name = 'UserMessage'\ndescriptor.field.add(name='uid', number=1, type=descriptor_pb2.FieldDescriptorProto.TYPE_STRING)\ndescriptor.field.add(name='age', number=2, type=descriptor_pb2.FieldDescriptorProto.TYPE_INT32)\ndescriptor.field.add(name='user_name', number=3, type=descriptor_pb2.FieldDescriptorProto.TYPE_STRING)\n\npool.Add(descriptor)\n\n# Create a mock Message class based on the descriptor\nclass UserMessage(Message):\n    def __init__(self, uid='', age=0, user_name='', **kwargs):\n        super().__init__(**kwargs)\n        self.uid = uid\n        self.age = age\n        self.user_name = user_name\n    DESCRIPTOR = pool.FindMessageTypeByName('UserMessage')\n\n# Convert the Protobuf Message object to a Pydantic BaseModel\nUserModel: Type[BaseModel] = msg_to_pydantic_model(UserMessage)\n\n# Example usage of the generated Pydantic model\nuser_data = UserModel(uid='123', age=30, user_name='John Doe')\nprint(user_data.model_dump_json(indent=2))\n\n# Access field info\n# print({k: v.field_info for k, v in UserModel.__fields__.items()}) # For Pydantic v1\nprint({k: v.json_schema_extra for k, v in UserModel.model_fields.items()}) # For Pydantic v2+\n","lang":"python","description":"This quickstart demonstrates how to convert a Protobuf Message object into a Pydantic `BaseModel` at runtime using `msg_to_pydantic_model`. It includes a minimal simulated Protobuf message for immediate execution. For actual use, `UserMessage` would be imported from your generated `_pb2.py` files."},"warnings":[{"fix":"Update imports from `from protobuf_to_pydantic.desc_template import DescTemplate` to `from protobuf_to_pydantic.template import Template` and adjust usage accordingly. [cite: GitHub v0.3.0 release]","message":"The import path for custom template functionality changed significantly in `v0.3.0`. Older code using `DescTemplate` will break.","severity":"breaking","affected_versions":">=0.3.0"},{"fix":"Carefully review the changelog and test existing code after upgrading to or past `v0.2.7`. Consult the official documentation for any API changes if issues arise. [cite: GitHub v0.2.7 release]","message":"Version `0.2.7` included urgent fixes that may have introduced breaking changes in internal APIs, impacting custom integrations or advanced usage.","severity":"breaking","affected_versions":"0.2.7"},{"fix":"Install `protoc` (Protocol Buffer Compiler) from the official Protocol Buffers GitHub releases or via your system's package manager. Ensure it's in your system's PATH.","message":"For 'Plugin Mode' (generating code from `.proto` files using `protoc`), the `protoc` compiler must be installed and accessible in your system's PATH. The library itself does not bundle `protoc`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Change the import statement to `from protobuf_to_pydantic.template import Template`.","cause":"Attempting to import `DescTemplate` after the `v0.3.0` release, where its import path was changed to `Template`.","error":"ModuleNotFoundError: No module named 'protobuf_to_pydantic.desc_template'"},{"fix":"Install the Protocol Buffer Compiler (`protoc`) for your operating system and ensure its executable is discoverable in your system's PATH environment variable.","cause":"The `protoc` command-line tool, required for compiling `.proto` files in plugin mode, is not installed or not found in the system's PATH.","error":"FileNotFoundError: [Errno 2] No such file or directory: 'protoc'"},{"fix":"Upgrade to `protobuf-to-pydantic` version `0.2.5` or newer, which includes a fix for handling `google.protobuf.Struct` types correctly.","cause":"Older versions of the library might have had issues correctly processing `google.protobuf.Struct` types into Pydantic models. [cite: GitHub v0.2.5 fix]","error":"TypeError: 'Struct' object cannot be interpreted as a dict (or similar error when passing google.protobuf.struct_pb2.Struct)"}]}