Skip to content

calling

Class info

🛈 DocStrings

Callable execution utilities.

execute_callable async

execute_callable(import_path: str, **kwargs: Any) -> Any

Execute a callable and return its result.

Parameters:

Name Type Description Default
import_path str

Dot-separated path to callable

required
**kwargs Any

Arguments to pass to the callable

{}

Returns:

Type Description
Any

Result of the callable execution

Raises:

Type Description
ValueError

If import or execution fails

Source code in src/llmling/utils/calling.py
async def execute_callable(import_path: str, **kwargs: Any) -> Any:
    """Execute a callable and return its result.

    Args:
        import_path: Dot-separated path to callable
        **kwargs: Arguments to pass to the callable

    Returns:
        Result of the callable execution

    Raises:
        ValueError: If import or execution fails
    """
    try:
        callable_obj = import_callable(import_path)
        logger.debug("Executing %r: kwargs=%s", callable_obj, kwargs)
        # Execute the callable
        if is_async_callable(callable_obj):
            result = await callable_obj(**kwargs)
        else:
            result = callable_obj(**kwargs)
    except Exception as exc:
        msg = f"Error executing callable {import_path}: {exc}"
        raise ValueError(msg) from exc
    else:
        return result

is_async_callable

is_async_callable(obj: Any) -> TypeGuard[Callable[..., Awaitable[Any]]]

Check if an object is an async callable.

Source code in src/llmling/utils/calling.py
def is_async_callable(obj: Any) -> TypeGuard[Callable[..., Awaitable[Any]]]:
    """Check if an object is an async callable."""
    return inspect.iscoroutinefunction(obj)