crewAIInc/crewAI · error · ImportError

Could not import langchain_apify python package. Please inst

Error message

Could not import langchain_apify python package. Please install it with `pip install langchain-apify` or `uv add langchain-apify`.

What it means

ApifyActorsTool wraps langchain-apify's ApifyActorsTool as an optional dependency; after the token check it tries `from langchain_apify import ApifyActorsTool` and on ImportError re-raises with install instructions. The package is not installed with crewai-tools by default.

Source

Thrown at lib/crewai-tools/src/crewai_tools/tools/apify_actors_tool/apify_actors_tool.py:69

                print(f"URL: {result['metadata']['url']}")
                print(f"Content: {result.get('markdown', 'N/A')[:100]}...")
    """
    actor_tool: _ApifyActorsTool = Field(description="Apify Actor Tool")
    package_dependencies: list[str] = Field(default_factory=lambda: ["langchain-apify"])

    def __init__(self, actor_name: str, *args: Any, **kwargs: Any) -> None:
        if not os.environ.get("APIFY_API_TOKEN"):
            msg = (
                "APIFY_API_TOKEN environment variable is not set. "
                "Please set it to your API key, to learn how to get it, "
                "see https://docs.apify.com/platform/integrations/api"
            )
            raise ValueError(msg)

        try:
            from langchain_apify import ApifyActorsTool as _ApifyActorsTool
        except ImportError as e:
            raise ImportError(
                "Could not import langchain_apify python package. "
                "Please install it with `pip install langchain-apify` or `uv add langchain-apify`."
            ) from e
        actor_tool = _ApifyActorsTool(actor_name)

        kwargs.update(
            {
                "name": actor_tool.name,
                "description": actor_tool.description,
                "args_schema": actor_tool.args_schema,
                "actor_tool": actor_tool,
            }
        )
        super().__init__(*args, **kwargs)

    def _run(self, run_input: dict[str, Any]) -> list[dict[str, Any]]:
        """Run the Actor tool with the given input.

View on GitHub (pinned to 754d7323be)

Solutions

  1. Install it: `pip install langchain-apify` (or `uv add langchain-apify`).
  2. Confirm the import works in the running interpreter: `python -c "from langchain_apify import ApifyActorsTool"`.
  3. Add it to project dependencies so environments are reproducible.

Example fix

# before
tool = ApifyActorsTool("my-actor")  # ImportError

# after (shell first)
# pip install langchain-apify
tool = ApifyActorsTool("my-actor")
Defensive patterns

Strategy: fallback

Validate before calling

def langchain_apify_available() -> bool:
    try:
        from langchain_apify import ApifyActorsTool  # noqa: F401,F401
        return True
    except ImportError:
        return False

Try / catch

try:
    tool = ApifyActorsTool("my-actor")
except ImportError as e:
    if "langchain_apify" in str(e):
        raise SystemExit("Run: pip install langchain-apify") from e
    raise

Prevention

When it happens

Trigger: Constructing ApifyActorsTool in an environment lacking langchain-apify; installing it into a different venv/interpreter than the one running CrewAI; a broken partial install where the import raises ImportError.

Common situations: Fresh environment without extras; lockfile missing the optional package; system vs project Python mismatch; CI cache without the wheel.

Related errors


AI-assisted analysis of crewAIInc/crewAI@754d7323be (2026-08-15). Data as JSON: /api/errors/a0c659e815f5ed26. Report an issue: GitHub.