How to Convert Python Classes to Protobuf
Paste or upload Python classes
Paste dataclasses, Pydantic models, TypedDicts or Enum classes, upload a .py file, or click Sample to load an example. The page reads classes that type their attributes, the way dataclasses and Pydantic models do, plus any Enum classes they use. Imports, functions and the code inside methods are ignored, so you can paste a whole models.py file.
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional
class Role(Enum):
ADMIN = "admin"
VIEWER = "viewer"
@dataclass
class User:
id: int
name: str
roles: list[Role] = field(default_factory=list)
nickname: Optional[str] = NoneCheck the generated schema
The proto3 schema appears on the right as you type. Read the comments above any field that was kept as string or renamed. The example above turns into this proto3 schema. Field numbers follow the order of the attributes, so keep them once the schema is in use; renumbering a field breaks messages that were already written.
syntax = "proto3";
// Role: numbered from 1 because proto3 needs 0 for the default; the Python values are not kept
enum Role {
ROLE_UNSPECIFIED = 0;
ROLE_ADMIN = 1;
ROLE_VIEWER = 2;
}
message User {
int64 id = 1;
string name = 2;
repeated Role roles = 3;
optional string nickname = 4;
}Copy or download the .proto file
Copy the schema or download it as converted.proto, then run protoc --python_out=. converted.proto to get the Python classes for it. The protoc compiler can also generate code for Go, Java, C# and the other languages your services use from the same file. The page writes messages and enums only; add any gRPC service definitions yourself.
protoc --python_out=. converted.proto # then, in Python import converted_pb2 user = converted_pb2.User(id=8901240544102066246, name="Elara Quinn") data = user.SerializeToString()
Frequently Asked Questions
Which Python classes does it read?
Anything that declares its attributes with type hints: @dataclass classes, Pydantic BaseModel classes, TypedDict and NamedTuple classes, attrs classes and plain classes with annotated attributes. A class that only types its __init__ parameters works too; those parameters become the fields. Enum, IntEnum, StrEnum and Flag classes become proto enums. Exception classes, Protocol classes and classes with nothing to store are left out, and the first line of the output names them.
How are Python type hints mapped to proto types?
int becomes int64, because a Python int has no fixed size and int64 still holds an ID like 8901240544102066246. float becomes double, str becomes string, bool stays bool and bytes becomes bytes. list, set, Sequence and tuple[X, ...] become repeated fields, and dict[str, X] becomes map<string, X>. Optional[X] and X | None become optional fields. Decimal, datetime, date, timedelta and UUID have no exact proto scalar, so they are written as string with a comment naming the typed alternative, which keeps values such as 14.50 exactly as written.
What happens to nested classes, enums and inheritance?
A class defined inside another class becomes a nested message, and a field typed with another class from your input points to that message. Every enum gets an _UNSPECIFIED = 0 value first, as proto3 requires, and its own name as a prefix so two enums never clash. The members are numbered from 1 in the order you wrote them; the Python values themselves are not kept. When a class inherits from another class in the input, the parent fields are copied in first, because protobuf has no inheritance.
Which attributes are skipped?
ClassVar attributes, names that start with an underscore, methods, properties and anything inside a method body. Pydantic Config and Django-style Meta settings classes are skipped as well. If a Pydantic field sets alias= or serialization_alias= in Field(...), that name is kept as json_name so JSON output still uses it.
What if a type has no protobuf equivalent?
Any, object, a Union of two real types, lists of lists, dicts without key and value types, dict keys that are not strings, integers or bools, TypeVar parameters and classes that are not in your input are written as string fields with a comment that says why. Literal["a", "b"] becomes a string with the allowed values in a comment. You can then wrap the inner part in its own message or switch to a oneof by hand.
What happens if my Python code has a syntax error?
You get an error with the line number instead of a half-finished schema: an unclosed bracket or string, a class line without its colon, a line indented where Python would not allow it, or a field like "age int" that is missing its colon. The page reads the code much like Python's own tokenizer does, but it is not a full Python parser, so it only catches the mistakes that matter for finding classes and fields. Every schema it shows has passed a .proto parser first.
Is my code uploaded anywhere?
No. Your Python is processed in your browser, never uploaded and never run, and nothing is kept: a reload starts with an empty editor. Only the class and field declarations are read.
Working from the other side? Protobuf to Python turns a .proto file back into dataclasses, and C# to Protobuf and Java to Protobuf do the same job for those languages.