def metadata_to_proto(metadata: Metadata) -> run_context_pb2.Metadata:
    """Convert the metadata dataclass to the metadata proto."""
    device_parameters: list[run_context_pb2.DeviceParameter] = []
    if params := getattr(metadata, "device_parameters", None):
        for param in params:
            path = getattr(param, "path", None)
            idx = getattr(param, "idx", None)
            device_parameters.append(run_context_pb2.DeviceParameter(path=path, idx=idx))

    return run_context_pb2.Metadata(
        device_parameters=device_parameters or None,  # If empty set this field as None.
        label=metadata.label,
        is_const=metadata.is_const,
        unit=metadata.unit,
    )
Convert the metadata dataclass to the metadata proto.
cirq-google/cirq_google/api/v2/sweeps.py