static_provider
A ready-to-use, in-memory :class:ModelProvider.
Most third parties already have their records in hand (or as plain dicts) and only
need to expose them under a source id. :class:StaticModelProvider covers that case
without requiring a custom subclass: construct it with already-built records, or use
:meth:StaticModelProvider.from_raw to validate plain dicts against each category's
record type. Subclass :class:~horde_model_reference.providers.base.ModelProvider
directly only when records must be fetched lazily from a live/remote source.
StaticModelProvider
Bases: ModelProvider
A :class:ModelProvider backed by a fixed, in-memory set of records.
This is the lowest-friction way to contribute model records: hand it the records
you already have and register it. The provider is read-only and never refreshes
(force_refresh is a no-op), which is exactly what you want for a static
snapshot.
Example
from horde_model_reference import (
ModelReferenceManager,
MODEL_REFERENCE_CATEGORY,
StaticModelProvider,
)
provider = StaticModelProvider.from_raw(
"civitai",
{
MODEL_REFERENCE_CATEGORY.image_generation: {
"my_model": {"baseline": "stable_diffusion_xl", "nsfw": False},
},
},
)
ModelReferenceManager().register_provider(provider)
Source code in src/horde_model_reference/providers/static_provider.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
_records
instance-attribute
_records: dict[
MODEL_REFERENCE_CATEGORY | str,
dict[str, GenericModelRecord],
] = {
category: (dict(records))
for category, records in (items())
}
__init__
__init__(
source_id: str,
records_by_category: Mapping[
MODEL_REFERENCE_CATEGORY | str,
Mapping[str, GenericModelRecord],
],
*,
cache_ttl_seconds: int | None = None,
) -> None
Store already-built records to serve under source_id.
Parameters:
-
source_id(str) –This provider's stable, unique id (e.g.
"civitai"). Must not be a reserved id ("horde"/"any"); validated on construction. -
records_by_category(Mapping[MODEL_REFERENCE_CATEGORY | str, Mapping[str, GenericModelRecord]]) –Mapping of category to
model_name -> record. The records must already be validated instances of the appropriate :class:~horde_model_reference.model_reference_records.GenericModelRecordsubclass (use :meth:from_rawto build them from plain dicts). -
cache_ttl_seconds(int | None, default:None) –Optional advisory staleness hint reported via :meth:
cache_ttl_seconds.
Raises:
-
ValueError–If source_id is empty or reserved.
Source code in src/horde_model_reference/providers/static_provider.py
from_raw
classmethod
from_raw(
source_id: str,
raw_by_category: Mapping[
MODEL_REFERENCE_CATEGORY | str,
Mapping[str, Mapping[str, object]],
],
*,
cache_ttl_seconds: int | None = None,
) -> StaticModelProvider
Build a provider by validating plain dicts against each category's record type.
Each raw record is validated with the record class registered for its category
(:data:~horde_model_reference.model_reference_records.MODEL_RECORD_TYPE_LOOKUP,
falling back to GenericModelRecord). The model name from the mapping key is
injected as the record's name so callers need not repeat it.
Parameters:
-
source_id(str) –This provider's stable, unique id (e.g.
"civitai"). -
raw_by_category(Mapping[MODEL_REFERENCE_CATEGORY | str, Mapping[str, Mapping[str, object]]]) –Mapping of category to
model_name -> raw field dict. -
cache_ttl_seconds(int | None, default:None) –Optional advisory staleness hint.
Returns:
-
StaticModelProvider(StaticModelProvider) –A provider serving the validated records.
Raises:
-
ValidationError–If any raw record fails validation.
-
ValueError–If source_id is empty or reserved.
Source code in src/horde_model_reference/providers/static_provider.py
provided_categories
Return the categories this provider holds records for.
fetch_category
fetch_category(
category: MODEL_REFERENCE_CATEGORY | str,
*,
force_refresh: bool = False,
) -> dict[str, GenericModelRecord] | None
Return a copy of the stored records for category, or None if absent.
Source code in src/horde_model_reference/providers/static_provider.py
cache_ttl_seconds
Return the advisory staleness hint set at construction, if any.
fetch_category_async
async
fetch_category_async(
category: MODEL_REFERENCE_CATEGORY | str,
*,
force_refresh: bool = False,
) -> dict[str, GenericModelRecord] | None
Asynchronously fetch records for category.
The default implementation runs :meth:fetch_category in a worker thread.
Override when a native async data path is available.
Parameters:
-
category(MODEL_REFERENCE_CATEGORY | str) –The category to fetch.
-
force_refresh(bool, default:False) –If
True, bypass any provider-side cache.
Returns:
-
dict[str, GenericModelRecord] | None–dict[str, GenericModelRecord] | None: Validated records keyed by model name, or
Nonewhen the provider cannot serve this category.
Source code in src/horde_model_reference/providers/base.py
serves_category
Return whether this provider advertises support for category.
supports_writes
Return whether this provider supports write operations.
Always False: third-party write semantics are explicitly out of scope.
The hook exists so consumers can branch on capability uniformly.
Source code in src/horde_model_reference/providers/base.py
validate_source_id
Raise ValueError if :attr:source_id is empty or reserved.
Called by the registry at registration time.
Source code in src/horde_model_reference/providers/base.py
_record_type_for
Return the record class registered for category, or GenericModelRecord.
Unknown category strings (a provider may define its own) fall back to
:class:~horde_model_reference.model_reference_records.GenericModelRecord.