Skip to content

Processor

Base classes

Name Children Inherits
BaseProcessor
llmling.processors.base
Base class for all processors.

⋔ Inheritance diagram

graph TD
  1422973396["base.Processor"]
  1058595610["base.BaseProcessor"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  1058595610 --> 1422973396
  925645236 --> 1058595610
  2751680898 --> 925645236

🛈 DocStrings

Bases: BaseProcessor

Content processor that executes a callable.

Source code in src/llmling/processors/base.py
class Processor(BaseProcessor):
    """Content processor that executes a callable."""

    def __init__(self, config: ProcessorConfig) -> None:
        super().__init__()
        self.config = config
        self._callable: Any = None

    async def startup(self) -> None:
        """Load the callable during startup."""
        if not self.config.import_path:
            msg = "Import path not configured"
            raise exceptions.ProcessorError(msg)

        try:
            self._callable = calling.import_callable(self.config.import_path)
        except ValueError as exc:
            msg = f"Failed to load callable: {exc}"
            raise exceptions.ProcessorError(msg) from exc

        self._initialized = True

    async def process(self, context: ProcessingContext) -> ProcessorResult:
        """Process content using the configured callable."""
        if not self._initialized or not self._callable:
            msg = "Processor not initialized"
            raise exceptions.ProcessorError(msg)

        try:
            # Execute callable
            result = self._callable(context.current_content, **context.kwargs)

            # Handle async functions
            if calling.is_async_callable(self._callable):
                result = await result

            # Convert result to string
            content = str(result)
            is_async = calling.is_async_callable(self._callable)
            meta = {"function": self.config.import_path, "is_async": is_async}
            orig = context.original_content
            return ProcessorResult(content=content, original_content=orig, metadata=meta)
        except Exception as exc:
            msg = f"Processing failed: {exc}"
            raise exceptions.ProcessorError(msg) from exc

    async def shutdown(self) -> None:
        """Clean up resources."""
        self._initialized = False
        self._callable = None

process async

process(context: ProcessingContext) -> ProcessorResult

Process content using the configured callable.

Source code in src/llmling/processors/base.py
async def process(self, context: ProcessingContext) -> ProcessorResult:
    """Process content using the configured callable."""
    if not self._initialized or not self._callable:
        msg = "Processor not initialized"
        raise exceptions.ProcessorError(msg)

    try:
        # Execute callable
        result = self._callable(context.current_content, **context.kwargs)

        # Handle async functions
        if calling.is_async_callable(self._callable):
            result = await result

        # Convert result to string
        content = str(result)
        is_async = calling.is_async_callable(self._callable)
        meta = {"function": self.config.import_path, "is_async": is_async}
        orig = context.original_content
        return ProcessorResult(content=content, original_content=orig, metadata=meta)
    except Exception as exc:
        msg = f"Processing failed: {exc}"
        raise exceptions.ProcessorError(msg) from exc

shutdown async

shutdown() -> None

Clean up resources.

Source code in src/llmling/processors/base.py
async def shutdown(self) -> None:
    """Clean up resources."""
    self._initialized = False
    self._callable = None

startup async

startup() -> None

Load the callable during startup.

Source code in src/llmling/processors/base.py
async def startup(self) -> None:
    """Load the callable during startup."""
    if not self.config.import_path:
        msg = "Import path not configured"
        raise exceptions.ProcessorError(msg)

    try:
        self._callable = calling.import_callable(self.config.import_path)
    except ValueError as exc:
        msg = f"Failed to load callable: {exc}"
        raise exceptions.ProcessorError(msg) from exc

    self._initialized = True

:fa-brands-github: Show source on GitHub