Skip to content

BaseRegistry

Sub classes

Name Children Inherits
ProcessorRegistry
llmling.processors.registry
Registry for content processors.
    PromptRegistry
    llmling.prompts.registry
    Registry for prompt templates.
      ResourceLoaderRegistry
      llmling.resources.loaders.registry
      Registry for context loaders.
        ResourceRegistry
        llmling.resources.registry
        Registry for managing configured resources.
          ToolRegistry
          llmling.tools.registry
          Registry for functions that can be called by LLMs.

            Base classes

            Name Children Inherits
            MutableMapping
            collections.abc
            A MutableMapping is a generic container for associating
            ABC
            abc
            Helper class that provides a standard way to create an ABC using
            Generic
            typing
            Abstract base class for generic types.

            ⋔ Inheritance diagram

            graph TD
              1668955754["baseregistry.BaseRegistry"]
              1799060090["abc.MutableMapping"]
              4016029557["abc.Mapping"]
              2852422536["abc.Collection"]
              4051874921["abc.Sized"]
              2751680898["builtins.object"]
              2021119437["abc.Iterable"]
              1031828782["abc.Container"]
              925645236["abc.ABC"]
              3481597255["typing.Generic"]
              1799060090 --> 1668955754
              4016029557 --> 1799060090
              2852422536 --> 4016029557
              4051874921 --> 2852422536
              2751680898 --> 4051874921
              2021119437 --> 2852422536
              2751680898 --> 2021119437
              1031828782 --> 2852422536
              2751680898 --> 1031828782
              925645236 --> 1668955754
              2751680898 --> 925645236
              3481597255 --> 1668955754
              2751680898 --> 3481597255

            🛈 DocStrings

            Bases: MutableMapping[TKey, TItem], ABC

            Base class for registries providing item storage and change notifications.

            This registry implements a dictionary-like interface backed by an EventedDict, providing automatic event emission for all mutations (additions, removals, modifications).

            Features: - Dictionary-like access (registry[key] = item) - Event emission for all changes - Item validation - Type safety - Customizable error handling

            Available events (accessed via .events): - adding(key, value): Before an item is added - added(key, value): After an item is added - removing(key, value): Before an item is removed - removed(key, value): After an item is removed - changing(key, value): Before an item is modified - changed(key, value): After an item is modified

            To implement, override: - _validate_item: Custom validation/transformation of items - _error_class: Custom error type for exceptions

            Source code in src/llmling/core/baseregistry.py
            class BaseRegistry[TKey, TItem](MutableMapping[TKey, TItem], ABC):
                """Base class for registries providing item storage and change notifications.
            
                This registry implements a dictionary-like interface backed by an EventedDict,
                providing automatic event emission for all mutations (additions, removals,
                modifications).
            
                Features:
                - Dictionary-like access (registry[key] = item)
                - Event emission for all changes
                - Item validation
                - Type safety
                - Customizable error handling
            
                Available events (accessed via .events):
                    - adding(key, value): Before an item is added
                    - added(key, value): After an item is added
                    - removing(key, value): Before an item is removed
                    - removed(key, value): After an item is removed
                    - changing(key, value): Before an item is modified
                    - changed(key, value): After an item is modified
            
                To implement, override:
                - _validate_item: Custom validation/transformation of items
                - _error_class: Custom error type for exceptions
                """
            
                def __init__(self) -> None:
                    """Initialize an empty registry."""
                    self._items = EventedDict[TKey, TItem]()
                    self._initialized = False
                    self._configs: dict[TKey, Any] = {}
            
                def __repr__(self) -> str:
                    return f"{self.__class__.__name__}({self._items})"
            
                @property
                def is_empty(self) -> bool:
                    """Check if registry has any items."""
                    return not bool(self._items)
            
                def has_item(self, key: TKey) -> bool:
                    """Check if an item is registered."""
                    return key in self._items
            
                @property
                def events(self):
                    """Access to all dictionary events."""
                    return self._items.events
            
                def register(self, key: TKey, item: TItem | Any, replace: bool = False) -> None:
                    """Register an item."""
                    if key in self._items and not replace:
                        msg = f"Item already registered: {key}"
                        raise self._error_class(msg)
            
                    validated_item = self._validate_item(item)
                    self._items[key] = validated_item
            
                def get(self, key: TKey) -> TItem:  # type: ignore
                    """Get an item by key."""
                    return self[key]
            
                def list_items(self) -> Sequence[TKey]:
                    """List all registered item keys."""
                    return list(self._items.keys())
            
                def reset(self) -> None:
                    """Reset registry to initial state."""
                    self._items.clear()
                    self._configs.clear()
                    self._initialized = False
            
                async def startup(self) -> None:
                    """Initialize all registered items."""
                    if self._initialized:
                        return
            
                    try:
                        for item in self._items.values():
                            await self._initialize_item(item)
                        self._initialized = True
                    except Exception as exc:
                        await self.shutdown()
                        msg = f"Registry startup failed: {exc}"
                        raise self._error_class(msg) from exc
            
                async def shutdown(self) -> None:
                    """Cleanup all registered items."""
                    if not self._initialized:
                        return
            
                    errors: list[tuple[TKey, Exception]] = []
            
                    for key, item in self._items.items():
                        try:
                            await self._cleanup_item(item)
                        except Exception as exc:  # noqa: BLE001
                            errors.append((key, exc))
            
                    self._initialized = False
            
                    if errors:
                        error_msgs = [f"{key}: {exc}" for key, exc in errors]
                        msg = f"Errors during shutdown: {', '.join(error_msgs)}"
                        raise self._error_class(msg)
            
                @property
                @abstractmethod
                def _error_class(self) -> type[exceptions.LLMLingError]:
                    """Error class to use for this registry."""
            
                @abstractmethod
                def _validate_item(self, item: Any) -> TItem:
                    """Validate and possibly transform item before registration."""
            
                async def _initialize_item(self, item: TItem) -> None:
                    """Initialize an item during startup."""
                    if hasattr(item, "startup") and callable(item.startup):  # pyright: ignore
                        await item.startup()  # pyright: ignore  # ty: ignore[invalid-await]
            
                async def _cleanup_item(self, item: TItem) -> None:
                    """Clean up an item during shutdown."""
                    if hasattr(item, "shutdown") and callable(item.shutdown):  # pyright: ignore
                        await item.shutdown()  # pyright: ignore  # ty: ignore[invalid-await]
            
                # Implementing MutableMapping methods
                def __getitem__(self, key: TKey) -> TItem:
                    try:
                        return self._items[key]
                    except KeyError as exc:
                        msg = f"Item not found: {key}"
                        raise self._error_class(msg) from exc
            
                def __setitem__(self, key: TKey, value: Any) -> None:
                    """Support dict-style assignment."""
                    self.register(key, value)
            
                def __contains__(self, key: object) -> bool:
                    """Support 'in' operator without raising exceptions."""
                    return key in self._items
            
                def __delitem__(self, key: TKey) -> None:
                    if key in self._items:
                        del self._items[key]
                    else:
                        msg = f"Item not found: {key}"
                        raise self._error_class(msg)
            
                def __iter__(self) -> Iterator[TKey]:
                    return iter(self._items)
            
                async def __aiter__(self):
                    """Async iterate over items, ensuring they're initialized."""
                    if not self._initialized:
                        await self.startup()
                    for key, item in self._items.items():
                        yield key, item
            
                def __len__(self) -> int:
                    return len(self._items)
            

            events property

            events
            

            Access to all dictionary events.

            is_empty property

            is_empty: bool
            

            Check if registry has any items.

            __aiter__ async

            __aiter__()
            

            Async iterate over items, ensuring they're initialized.

            Source code in src/llmling/core/baseregistry.py
            async def __aiter__(self):
                """Async iterate over items, ensuring they're initialized."""
                if not self._initialized:
                    await self.startup()
                for key, item in self._items.items():
                    yield key, item
            

            __contains__

            __contains__(key: object) -> bool
            

            Support 'in' operator without raising exceptions.

            Source code in src/llmling/core/baseregistry.py
            def __contains__(self, key: object) -> bool:
                """Support 'in' operator without raising exceptions."""
                return key in self._items
            

            __init__

            __init__() -> None
            

            Initialize an empty registry.

            Source code in src/llmling/core/baseregistry.py
            def __init__(self) -> None:
                """Initialize an empty registry."""
                self._items = EventedDict[TKey, TItem]()
                self._initialized = False
                self._configs: dict[TKey, Any] = {}
            

            __setitem__

            __setitem__(key: TKey, value: Any) -> None
            

            Support dict-style assignment.

            Source code in src/llmling/core/baseregistry.py
            def __setitem__(self, key: TKey, value: Any) -> None:
                """Support dict-style assignment."""
                self.register(key, value)
            

            get

            get(key: TKey) -> TItem
            

            Get an item by key.

            Source code in src/llmling/core/baseregistry.py
            def get(self, key: TKey) -> TItem:  # type: ignore
                """Get an item by key."""
                return self[key]
            

            has_item

            has_item(key: TKey) -> bool
            

            Check if an item is registered.

            Source code in src/llmling/core/baseregistry.py
            def has_item(self, key: TKey) -> bool:
                """Check if an item is registered."""
                return key in self._items
            

            list_items

            list_items() -> Sequence[TKey]
            

            List all registered item keys.

            Source code in src/llmling/core/baseregistry.py
            def list_items(self) -> Sequence[TKey]:
                """List all registered item keys."""
                return list(self._items.keys())
            

            register

            register(key: TKey, item: TItem | Any, replace: bool = False) -> None
            

            Register an item.

            Source code in src/llmling/core/baseregistry.py
            def register(self, key: TKey, item: TItem | Any, replace: bool = False) -> None:
                """Register an item."""
                if key in self._items and not replace:
                    msg = f"Item already registered: {key}"
                    raise self._error_class(msg)
            
                validated_item = self._validate_item(item)
                self._items[key] = validated_item
            

            reset

            reset() -> None
            

            Reset registry to initial state.

            Source code in src/llmling/core/baseregistry.py
            def reset(self) -> None:
                """Reset registry to initial state."""
                self._items.clear()
                self._configs.clear()
                self._initialized = False
            

            shutdown async

            shutdown() -> None
            

            Cleanup all registered items.

            Source code in src/llmling/core/baseregistry.py
            async def shutdown(self) -> None:
                """Cleanup all registered items."""
                if not self._initialized:
                    return
            
                errors: list[tuple[TKey, Exception]] = []
            
                for key, item in self._items.items():
                    try:
                        await self._cleanup_item(item)
                    except Exception as exc:  # noqa: BLE001
                        errors.append((key, exc))
            
                self._initialized = False
            
                if errors:
                    error_msgs = [f"{key}: {exc}" for key, exc in errors]
                    msg = f"Errors during shutdown: {', '.join(error_msgs)}"
                    raise self._error_class(msg)
            

            startup async

            startup() -> None
            

            Initialize all registered items.

            Source code in src/llmling/core/baseregistry.py
            async def startup(self) -> None:
                """Initialize all registered items."""
                if self._initialized:
                    return
            
                try:
                    for item in self._items.values():
                        await self._initialize_item(item)
                    self._initialized = True
                except Exception as exc:
                    await self.shutdown()
                    msg = f"Registry startup failed: {exc}"
                    raise self._error_class(msg) from exc
            

            :fa-brands-github: Show source on GitHub