Skip to content

PathResource

Base classes

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

⋔ Inheritance diagram

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

🛈 DocStrings

Bases: BaseResource

Resource loaded from a file or URL.

Source code in src/llmling/config/models.py
class PathResource(BaseResource):
    """Resource loaded from a file or URL."""

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

    path: str | os.PathLike[str]
    """Path to the file or URL to load."""

    watch: WatchConfig | None = None
    """Configuration for watching the file for changes."""

    def validate_resource(self) -> list[str]:
        """Check if path exists for local files."""
        from upathtools import to_upath

        path = to_upath(self.path)
        prefixes = ("http://", "https://")
        return [
            f"Resource path not found: {path}"
            for path in [path]
            if not path.exists() and not path.as_uri().startswith(prefixes)
        ]

    @property
    def supports_watching(self) -> bool:
        """Whether this resource instance supports watching."""
        from upathtools import to_upath

        path = to_upath(self.path)
        if not path.exists():
            msg = f"Cannot watch non-existent path: {self.path}"
            warnings.warn(msg, UserWarning, stacklevel=2)
            return False
        return True

    def get_watch_path(self) -> str | None:
        """Get the path to watch."""
        return str(self.path) if self.supports_watching else None

    @model_validator(mode="after")
    def validate_path(self) -> PathResource:
        """Validate that the path is not empty."""
        if not self.path:
            msg = "Path cannot be empty"
            raise ValueError(msg)
        return self

    def is_templated(self) -> bool:
        """Path resources are templated if they contain placeholders."""
        return "{" in str(self.path)

    @property
    def mime_type(self) -> str:
        """Get MIME type based on file extension."""
        return guess_mime_type(self.path)

mime_type property

mime_type: str

Get MIME type based on file extension.

path instance-attribute

path: str | PathLike[str]

Path to the file or URL to load.

supports_watching property

supports_watching: bool

Whether this resource instance supports watching.

type class-attribute instance-attribute

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

Discriminator field identifying this as a path-based resource.

watch class-attribute instance-attribute

watch: WatchConfig | None = None

Configuration for watching the file for changes.

get_watch_path

get_watch_path() -> str | None

Get the path to watch.

Source code in src/llmling/config/models.py
def get_watch_path(self) -> str | None:
    """Get the path to watch."""
    return str(self.path) if self.supports_watching else None

is_templated

is_templated() -> bool

Path resources are templated if they contain placeholders.

Source code in src/llmling/config/models.py
def is_templated(self) -> bool:
    """Path resources are templated if they contain placeholders."""
    return "{" in str(self.path)

validate_path

validate_path() -> PathResource

Validate that the path is not empty.

Source code in src/llmling/config/models.py
@model_validator(mode="after")
def validate_path(self) -> PathResource:
    """Validate that the path is not empty."""
    if not self.path:
        msg = "Path cannot be empty"
        raise ValueError(msg)
    return self

validate_resource

validate_resource() -> list[str]

Check if path exists for local files.

Source code in src/llmling/config/models.py
def validate_resource(self) -> list[str]:
    """Check if path exists for local files."""
    from upathtools import to_upath

    path = to_upath(self.path)
    prefixes = ("http://", "https://")
    return [
        f"Resource path not found: {path}"
        for path in [path]
        if not path.exists() and not path.as_uri().startswith(prefixes)
    ]

:fa-brands-github: Show source on GitHub