Skip to content

CallableResource

Base classes

Name Children Inherits
BaseResource
llmling.config.models
Base class for all resource types.

⋔ Inheritance diagram

graph TD
  2020841922["models.CallableResource"]
  1028592330["models.BaseResource"]
  330168112["schema.Schema"]
  3879467795["main.BaseModel"]
  2751680898["builtins.object"]
  1028592330 --> 2020841922
  330168112 --> 1028592330
  3879467795 --> 330168112
  2751680898 --> 3879467795

🛈 DocStrings

Bases: BaseResource

Resource from executing a Python callable.

Source code in src/llmling/config/models.py
class CallableResource(BaseResource):
    """Resource from executing a Python callable."""

    type: Literal["callable"] = Field(default="callable", init=False)
    """Discriminator field identifying this as a callable-based resource."""

    import_path: str
    """Dotted import path to the callable to execute."""

    keyword_args: dict[str, Any] = Field(default_factory=dict)
    """Keyword arguments to pass to the callable."""

    @model_validator(mode="after")
    def validate_import_path(self) -> CallableResource:
        """Validate that the import path is properly formatted."""
        if not all(part.isidentifier() for part in self.import_path.split(".")):
            msg = f"Invalid import path: {self.import_path}"
            raise ValueError(msg)
        return self

    def is_templated(self) -> bool:
        """Callable resources are templated if they take parameters."""
        fn = import_callable(self.import_path)
        sig = inspect.signature(fn, annotation_format=annotationlib.Format.FORWARDREF)
        return bool(sig.parameters)

import_path instance-attribute

import_path: str

Dotted import path to the callable to execute.

keyword_args class-attribute instance-attribute

keyword_args: dict[str, Any] = Field(default_factory=dict)

Keyword arguments to pass to the callable.

type class-attribute instance-attribute

type: Literal['callable'] = Field(default='callable', init=False)

Discriminator field identifying this as a callable-based resource.

is_templated

is_templated() -> bool

Callable resources are templated if they take parameters.

Source code in src/llmling/config/models.py
def is_templated(self) -> bool:
    """Callable resources are templated if they take parameters."""
    fn = import_callable(self.import_path)
    sig = inspect.signature(fn, annotation_format=annotationlib.Format.FORWARDREF)
    return bool(sig.parameters)

validate_import_path

validate_import_path() -> CallableResource

Validate that the import path is properly formatted.

Source code in src/llmling/config/models.py
@model_validator(mode="after")
def validate_import_path(self) -> CallableResource:
    """Validate that the import path is properly formatted."""
    if not all(part.isidentifier() for part in self.import_path.split(".")):
        msg = f"Invalid import path: {self.import_path}"
        raise ValueError(msg)
    return self

:fa-brands-github: Show source on GitHub