Using Hydra to Select Multiple Structured Configs

I have an existing hydra setup with yaml files, and am trying to migrate it to use structured configs to get the type annotation and inheritance advantages. One sticking point I am having is that part of my present yaml setup follows the select multiple configs from a config group pattern from the hydra docs. I am struggling to convert that pattern to use structured config classes instead of the yaml files. Here is my attempt to convert the exact example in the docs to structured configs:

from dataclasses import dataclass, field
from typing import Any

import hydra
from hydra.core.config_store import ConfigStore
from omegaconf import OmegaConf


@dataclass
class Amazon:
    domain: str = "amazon.com"


@dataclass
class Facebook:
    domain: str = "facebook.com"


@dataclass
class Google:
    domain: str = "google.com"


@dataclass
class Apache:
    host: str = "localhost"
    port: int = 443


@dataclass
class Server:
    site: list[Any] = field(default_factory=lambda: [Amazon, Facebook, Google])
    tech: Any = field(default_factory=Apache)


DEFAULTS = [{"server/site": ["facebook", "google"]}]


@dataclass
class Config:
    server: Any = field(default_factory=Server)
    defaults: list[Any] = field(default_factory=lambda: DEFAULTS)


@hydra.main(version_base=None, config_name="config")
def my_app(cfg: Config) -> None:
    print(OmegaConf.to_yaml(cfg))


if __name__ == "__main__":
    cs = ConfigStore.instance()
    cs.store(group="server/site", name="facebook", node=Facebook)
    cs.store(group="server/site", name="amazon", node=Amazon)
    cs.store(group="server/site", name="google", node=Google)
    cs.store(name="config", node=Config)
    my_app()

However, when I run that I get an error:

hydra.errors.ConfigCompositionException: In 'server/site/google': ValidationError raised while composing config:
Merge error: Google is not a subclass of Facebook. value: {'domain': 'google.com'}
    full_key: 
    object_type=dict

It seems like the default server/site being a list is making hydra try to merge the configs for facebook and google instead of having a list-of-dicts as the config. What am I doing wrong?

There are several issues with your port.
If you look at the resulting config in the example page, you would notice that site is not a list but a dictionary.
The different sites each have a different key hard coded into as a way avoid them filling the same node.

The example below creates a Site class with optional fields of each of the configs. This lines up better with the example you are porting (besides the fact that list composition is not supported).

In addition, it overrides the packages if the site configs to be in the correct package when storing the configs in the ConfigStore.

from dataclasses import dataclass, field
from typing import Any, Optional

import hydra
from hydra.core.config_store import ConfigStore
from omegaconf import OmegaConf


@dataclass
class Amazon:
    domain: str = "amazon.com"


@dataclass
class Facebook:
    domain: str = "facebook.com"


@dataclass
class Google:
    domain: str = "google.com"


@dataclass
class Apache:
    host: str = "localhost"
    port: int = 443


@dataclass
class Site:
    facebook: Optional[Facebook] = None
    google: Optional[Google] = None
    amazon: Optional[Amazon] = None


@dataclass
class Server:
    site: Site = field(default_factory=Site)
    tech: Any = field(default_factory=Apache)


DEFAULTS = ["_self_", {"server/site": ["facebook", "google"]}]


@dataclass
class Config:
    server: Any = field(default_factory=Server)
    defaults: list[Any] = field(default_factory=lambda: DEFAULTS)


@hydra.main(version_base=None, config_name="config")
def my_app(cfg: Config) -> None:
    print(OmegaConf.to_yaml(cfg))


if __name__ == "__main__":
    cs = ConfigStore.instance()
    cs.store(group="server/site", name="facebook", node=Facebook, package="server.site.facebook")
    cs.store(group="server/site", name="amazon", node=Amazon, package="server.site.amazon")
    cs.store(group="server/site", name="google", node=Google, package="server.site.google")
    cs.store(name="config", node=Config)
    my_app()

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật