Skip to content

Protocol Buffer Schema Generator

Here's the reference information for the included Protocol Buffer schema generator classes with all their parameters, attributes and methods.

You can import these classes directly from pyrmute:

from pyrmute import ProtoExporter

pyrmute.ProtoExporter

ProtoExporter(
    registry,
    package="com.example",
    include_docs=True,
    use_proto3=True,
)

Export Pydantic models to Protocol Buffer .proto files.

This class provides methods to export individual schemas or all schemas from a model registry to .proto files.

Initialize the Protocol Buffer exporter.

PARAMETER DESCRIPTION
registry

Model registry instance.

TYPE: Registry

package

Protobuf package name.

TYPE: str DEFAULT: 'com.example'

include_docs

Whether to include documentation as comments.

TYPE: bool DEFAULT: True

use_proto3

Use proto3 syntax (True) or proto2 (False).

TYPE: bool DEFAULT: True

Source code in src/pyrmute/protobuf_schema.py
def __init__(
    self: Self,
    registry: Registry,
    package: str = "com.example",
    include_docs: bool = True,
    use_proto3: bool = True,
) -> None:
    """Initialize the Protocol Buffer exporter.

    Args:
        registry: Model registry instance.
        package: Protobuf package name.
        include_docs: Whether to include documentation as comments.
        use_proto3: Use proto3 syntax (True) or proto2 (False).
    """
    self._registry = registry
    self.generator = ProtoSchemaGenerator(
        package=package,
        include_docs=include_docs,
        use_proto3=use_proto3,
    )

generator instance-attribute

generator = ProtoSchemaGenerator(
    package=package,
    include_docs=include_docs,
    use_proto3=use_proto3,
)

export_schema

export_schema(name, version, output_path=None)

Export a single model version as a Protocol Buffer schema.

PARAMETER DESCRIPTION
name

Model name.

TYPE: str

version

Model version.

TYPE: str | ModelVersion

output_path

Optional file path to save schema (.proto file).

TYPE: str | Path | None DEFAULT: None

RETURNS DESCRIPTION
str

Protocol Buffer file as a string.

Example
exporter = ProtoExporter(manager._registry, package="com.myapp")

# Export and save
proto_file = exporter.export_schema("User", "1.0.0", "protos/user_v1.proto")

# Or just get the proto definition
proto_file = exporter.export_schema("User", "1.0.0")
Source code in src/pyrmute/protobuf_schema.py
def export_schema(
    self: Self,
    name: str,
    version: str | ModelVersion,
    output_path: str | Path | None = None,
) -> str:
    """Export a single model version as a Protocol Buffer schema.

    Args:
        name: Model name.
        version: Model version.
        output_path: Optional file path to save schema (.proto file).

    Returns:
        Protocol Buffer file as a string.

    Example:
        ```python
        exporter = ProtoExporter(manager._registry, package="com.myapp")

        # Export and save
        proto_file = exporter.export_schema("User", "1.0.0", "protos/user_v1.proto")

        # Or just get the proto definition
        proto_file = exporter.export_schema("User", "1.0.0")
        ```
    """
    model = self._registry.get_model(name, version)

    registry_name_map: dict[str, str] = {}
    for model_name in self._registry.list_models():
        for model_version in self._registry.get_versions(model_name):
            registered_model = self._registry.get_model(model_name, model_version)
            registry_name_map[registered_model.__name__] = model_name

    document = self.generator.generate_schema(
        model, name, version, registry_name_map
    )
    proto_content = self.generator.proto_file_to_string(document)

    if output_path:
        output_path = Path(output_path)
        output_path.parent.mkdir(parents=True, exist_ok=True)
        output_path.write_text(proto_content)

    return proto_content

export_all_schemas

export_all_schemas(output_dir)

Export all registered models as Protocol Buffer schemas.

PARAMETER DESCRIPTION
output_dir

Directory to save .proto files.

TYPE: str | Path

RETURNS DESCRIPTION
dict[str, dict[str, str]]

Dictionary mapping model names to version to Protocol Buffer file as a

dict[str, dict[str, str]]

string.

Example
exporter = ProtoExporter(manager._registry, package="com.myapp")
proto_files = exporter.export_all_schemas("protos/")

# Creates files like:
# protos/User_v1_0_0.proto
# protos/User_v2_0_0.proto
# protos/Order_v1_0_0.proto
Source code in src/pyrmute/protobuf_schema.py
def export_all_schemas(
    self: Self,
    output_dir: str | Path,
) -> dict[str, dict[str, str]]:
    """Export all registered models as Protocol Buffer schemas.

    Args:
        output_dir: Directory to save .proto files.

    Returns:
        Dictionary mapping model names to version to Protocol Buffer file as a
        string.

    Example:
        ```python
        exporter = ProtoExporter(manager._registry, package="com.myapp")
        proto_files = exporter.export_all_schemas("protos/")

        # Creates files like:
        # protos/User_v1_0_0.proto
        # protos/User_v2_0_0.proto
        # protos/Order_v1_0_0.proto
        ```
    """
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)

    all_schemas: dict[str, dict[str, str]] = {}

    registry_name_map: dict[str, str] = {}
    for model_name in self._registry.list_models():
        for model_version in self._registry.get_versions(model_name):
            registered_model = self._registry.get_model(model_name, model_version)
            registry_name_map[registered_model.__name__] = model_name

    for model_name in self._registry.list_models():
        all_schemas[model_name] = {}
        versions = self._registry.get_versions(model_name)

        for version in versions:
            model = self._registry.get_model(model_name, version)
            document = self.generator.generate_schema(
                model, model_name, version, registry_name_map
            )

            version_str = str(version).replace(".", "_")
            filename = f"{model_name}_v{version_str}.proto"
            filepath = output_dir / filename

            proto_content = self.generator.proto_file_to_string(document)
            filepath.write_text(proto_content)

            all_schemas[model_name][str(version)] = proto_content

    return all_schemas