{"id":5686,"library":"protobuf3-to-dict","title":"Protobuf 3 to Python Dict Converter","description":"A Python library designed to convert Protocol Buffer 3 messages into standard Python dictionaries and vice-versa. It is particularly useful as an intermediate step for serialization tasks, such as converting Protobuf messages to JSON. The library is a Python 3 adaptation of an earlier project and currently maintains version 0.1.5, with infrequent updates.","status":"active","version":"0.1.5","language":"en","source_language":"en","source_url":"https://github.com/kaporzhu/protobuf-to-dict","tags":["protobuf","serialization","dict","protocol-buffers","json"],"install":[{"cmd":"pip install protobuf3-to-dict","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for Protobuf message definitions and operations.","package":"protobuf","version_range":"==3.19.4"}],"imports":[{"symbol":"protobuf_to_dict","correct":"from protobuf_to_dict import protobuf_to_dict"},{"symbol":"dict_to_protobuf","correct":"from protobuf_to_dict import dict_to_protobuf"}],"quickstart":{"code":"from google.protobuf.timestamp_pb2 import Timestamp\nfrom protobuf_to_dict import protobuf_to_dict, dict_to_protobuf\nimport datetime\n\n# Create a Protobuf Timestamp message\ndt_obj = datetime.datetime.now(datetime.timezone.utc)\ntimestamp_msg = Timestamp()\ntimestamp_msg.FromDatetime(dt_obj)\n\nprint(f\"Original Protobuf Timestamp: {timestamp_msg}\")\n\n# Convert Protobuf message to Python dictionary\ndata_dict = protobuf_to_dict(timestamp_msg)\nprint(f\"Converted dictionary: {data_dict}\")\n\n# Convert dictionary back to Protobuf message\nnew_timestamp_msg = dict_to_protobuf(Timestamp, data_dict)\nprint(f\"Converted back to Protobuf: {new_timestamp_msg}\")\n\n# Verify conversion\nassert new_timestamp_msg.seconds == timestamp_msg.seconds\nassert new_timestamp_msg.nanos == timestamp_msg.nanos\nprint(\"Conversion verified successfully!\")","lang":"python","description":"This quickstart demonstrates converting a `google.protobuf.Timestamp` message to a Python dictionary and back. For custom Protobuf messages, you would first need to compile your `.proto` files into Python classes using the `protoc` compiler."},"warnings":[{"fix":"Ensure your import statements use `from protobuf_to_dict import ...` after running `pip install protobuf3-to-dict`.","message":"The library is installed as `protobuf3-to-dict` but imported as `protobuf_to_dict`. This is a common source of 'ModuleNotFoundError'.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your project is running on Python 3.6 or later.","message":"This library explicitly targets Protobuf 3 and Python 3. Despite PyPI's `requires_python: None` metadata for some older versions, it is not compatible with Python 2.x environments.","severity":"breaking","affected_versions":"All versions"},{"fix":"Adhere to the specified `protobuf==3.19.4` dependency. If you require a different `protobuf` version, carefully test for compatibility or consider alternative conversion libraries.","message":"The library strictly pins its `protobuf` dependency to `protobuf==3.19.4`. Using a different version of the `protobuf` library in your environment may lead to unexpected behavior or `ImportError` issues.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware of the naming convention when comparing outputs or migrating from other Protobuf-to-dict conversion methods. If `camelCase` is required, manual post-processing of the dictionary will be necessary.","message":"Unlike `google.protobuf.json_format.MessageToDict` which defaults to converting field names to `camelCase`, `protobuf3-to-dict` preserves the original Protobuf field names (e.g., `snake_case`) in the resulting Python dictionary.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Install `protoc` (e.g., via your OS package manager or by downloading from GitHub) and run `protoc --python_out=. your_message.proto` to generate `your_message_pb2.py`.","message":"To convert your own custom Protobuf messages, you must first install the `protoc` compiler and use it to generate Python classes from your `.proto` files. This library works with the generated Python Protobuf classes.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}