{"record":{"id":"59f40d39b8ae1fcd","repo":"crewAIInc/crewAI","slug":"sqlalchemy-is-not-installed-please-install-it-wit","errorCode":null,"errorMessage":"sqlalchemy is not installed. Please install it with `pip install crewai-tools[sqlalchemy]`","messagePattern":"sqlalchemy is not installed\\. Please install it with `pip install crewai-tools\\[sqlalchemy\\]`","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/tools/nl2sql/nl2sql_tool.py","lineNumber":267,"sourceCode":"    tables: list[dict[str, Any]] = Field(default_factory=list)\n    columns: dict[str, list[dict[str, Any]] | str] = Field(default_factory=dict)\n    args_schema: type[BaseModel] = NL2SQLToolInput\n\n    @model_validator(mode=\"after\")\n    def _apply_env_override(self) -> Self:\n        \"\"\"Allow CREWAI_NL2SQL_ALLOW_DML=true to override allow_dml at runtime.\"\"\"\n        if os.environ.get(\"CREWAI_NL2SQL_ALLOW_DML\", \"\").strip().lower() == \"true\":\n            if not self.allow_dml:\n                logger.warning(\n                    \"NL2SQLTool: CREWAI_NL2SQL_ALLOW_DML env var is set — \"\n                    \"DML/DDL operations are enabled. Ensure this is intentional.\"\n                )\n            self.allow_dml = True\n        return self\n\n    def model_post_init(self, __context: Any) -> None:\n        if not SQLALCHEMY_AVAILABLE:\n            raise ImportError(\n                \"sqlalchemy is not installed. Please install it with \"\n                \"`pip install crewai-tools[sqlalchemy]`\"\n            )\n\n        if self.allow_dml:\n            logger.warning(\n                \"NL2SQLTool: allow_dml=True — write operations (INSERT/UPDATE/\"\n                \"DELETE/DROP/…) are permitted. Use with caution.\"\n            )\n\n        data: dict[str, list[dict[str, Any]] | str] = {}\n        result = self._fetch_available_tables()\n        if isinstance(result, str):\n            raise RuntimeError(f\"Failed to fetch tables: {result}\")\n        tables: list[dict[str, Any]] = result\n\n        for table in tables:\n            table_columns = self._fetch_all_available_columns(table[\"table_name\"])","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/tools/nl2sql/nl2sql_tool.py#L249-L285","documentation":"NL2SQLTool depends on SQLAlchemy to introspect the database; the module probes for it at import time (SQLALCHEMY_AVAILABLE) and raises this ImportError in model_post_init when the probe failed. The message points at the crewai-tools[sqlalchemy] extra rather than plain sqlalchemy because the extra pins a tested version.","triggerScenarios":"Constructing NL2SQLTool(db_uri=...) without sqlalchemy installed; a broken/partial sqlalchemy install that fails import; installing crewai-tools without extras in a slim container.","commonSituations":"Minimal Docker images; adding crewai-tools to an existing project without the sql extras; sqlalchemy uninstalled accidentally by a dependency resolver conflict.","solutions":["Install the extra: `pip install \"crewai-tools[sqlalchemy]\"` (or `uv add 'crewai-tools[sqlalchemy]'`)","Failing that, `pip install sqlalchemy` — then verify `python -c \"import sqlalchemy\"`","Add the extra to your pyproject/requirements so rebuilds keep it"],"exampleFix":"# before\ntool = NL2SQLTool(db_uri=\"mysql+pymysql://u:p@h/db\")  # ImportError\n\n# after\n# pip install \"crewai-tools[sqlalchemy]\"\ntool = NL2SQLTool(db_uri=\"mysql+pymysql://u:p@h/db\")","handlingStrategy":"validation","validationCode":"def sqlalchemy_available() -> bool:\n    try:\n        import sqlalchemy  # noqa: F401\n        return True\n    except ImportError:\n        return False\n\nassert sqlalchemy_available(), 'pip install \"crewai-tools[sqlalchemy]\"'","typeGuard":null,"tryCatchPattern":"try:\n    tool = NL2SQLTool(db_uri=uri)\nexcept ImportError as e:\n    if \"sqlalchemy\" in str(e):\n        raise SystemExit('Install: pip install \"crewai-tools[sqlalchemy]\"') from e\n    raise","preventionTips":["Install the [sqlalchemy] extra when adopting NL2SQLTool","Smoke-test optional imports in CI for every tool you use","Keep extras pinned in pyproject alongside crewai-tools itself"],"tags":["dependency","sqlalchemy","nl2sql","crewai-tools","import-error"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}