Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

informative import error messages #25

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,16 @@ Automated releases are handled by the [release workflow][release-workflow] which
Testing, type checking, and formatting/linting is [checked in CI][ci].

[ci]: .github/workflows/ci.yml

## Style guide

### Adding integrations with external libraries

When adding integrations with external libraries, always use a lazy import. The external dependency should not be required to use the `cleanlab-codex` library. Wrap the lazy import in a `try`/`except` block to catch the `ImportError` and raise a `MissingDependencyError` with a helpful message. See [codex_tool.py](src/cleanlab_codex/codex_tool.py) file for examples one of which is shown below:

```python
try:
from cleanlab_codex.utils.smolagents import CodexTool as SmolagentsCodexTool
except ImportError as e:
raise MissingDependencyError("smolagents", "https://github.com/huggingface/smolagents") from e
```
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ html = "coverage html"
xml = "coverage xml"

[tool.ruff.lint]
ignore = ["FA100", "UP007", "UP006"]
ignore = ["FA100", "UP007", "UP006", "EM101"]
26 changes: 22 additions & 4 deletions src/cleanlab_codex/codex_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from typing_extensions import Annotated

from cleanlab_codex.project import Project
from cleanlab_codex.utils.function import pydantic_model_from_function, required_properties_from_model
from cleanlab_codex.utils.errors import MissingDependencyError
from cleanlab_codex.utils.function import (
pydantic_model_from_function,
required_properties_from_model,
)


class CodexTool:
Expand Down Expand Up @@ -119,7 +123,10 @@ def to_smolagents_tool(self) -> Any:

Note: You must have the [`smolagents` library installed](https://github.com/huggingface/smolagents) to use this method.
"""
from cleanlab_codex.utils.smolagents import CodexTool as SmolagentsCodexTool
try:
from cleanlab_codex.utils.smolagents import CodexTool as SmolagentsCodexTool
except ImportError as e:
raise MissingDependencyError("smolagents", "https://github.com/huggingface/smolagents") from e

return SmolagentsCodexTool(
query=self.query,
Expand All @@ -133,7 +140,14 @@ def to_llamaindex_tool(self) -> Any:

Note: You must have the [`llama-index` library installed](https://docs.llamaindex.ai/en/stable/getting_started/installation/) to use this method.
"""
from llama_index.core.tools import FunctionTool
try:
from llama_index.core.tools import FunctionTool

except ImportError as e:
raise MissingDependencyError(
"llama-index-core",
"https://docs.llamaindex.ai/en/stable/getting_started/installation/",
) from e

return FunctionTool.from_defaults(
fn=self.query,
Expand All @@ -147,7 +161,11 @@ def to_langchain_tool(self) -> Any:

Note: You must have the [`langchain` library installed](https://python.langchain.com/docs/concepts/architecture/) to use this method.
"""
from langchain_core.tools.structured import StructuredTool
try:
from langchain_core.tools.structured import StructuredTool

except ImportError as e:
raise MissingDependencyError("langchain", "https://pypi.org/project/langchain/") from e

return StructuredTool.from_function(
func=self.query,
Expand Down
15 changes: 15 additions & 0 deletions src/cleanlab_codex/utils/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import annotations


class MissingDependencyError(Exception):
"""Raised when a lazy import is missing."""

def __init__(self, import_name: str, package_url: str | None = None) -> None:
self.import_name = import_name
self.package_url = package_url

def __str__(self) -> str:
message = f"Failed to import {self.import_name}. Please install the package using `pip install {self.import_name}` and try again."
if self.package_url:
message += f" For more information, see {self.package_url}."
return message