crewAIInc/crewAI · error · ImportError
sqlalchemy is not installed. Please install it with `pip ins
Error message
sqlalchemy is not installed. Please install it with `pip install crewai-tools[sqlalchemy]`
What it means
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.
Source
Thrown at lib/crewai-tools/src/crewai_tools/tools/nl2sql/nl2sql_tool.py:267
tables: list[dict[str, Any]] = Field(default_factory=list)
columns: dict[str, list[dict[str, Any]] | str] = Field(default_factory=dict)
args_schema: type[BaseModel] = NL2SQLToolInput
@model_validator(mode="after")
def _apply_env_override(self) -> Self:
"""Allow CREWAI_NL2SQL_ALLOW_DML=true to override allow_dml at runtime."""
if os.environ.get("CREWAI_NL2SQL_ALLOW_DML", "").strip().lower() == "true":
if not self.allow_dml:
logger.warning(
"NL2SQLTool: CREWAI_NL2SQL_ALLOW_DML env var is set — "
"DML/DDL operations are enabled. Ensure this is intentional."
)
self.allow_dml = True
return self
def model_post_init(self, __context: Any) -> None:
if not SQLALCHEMY_AVAILABLE:
raise ImportError(
"sqlalchemy is not installed. Please install it with "
"`pip install crewai-tools[sqlalchemy]`"
)
if self.allow_dml:
logger.warning(
"NL2SQLTool: allow_dml=True — write operations (INSERT/UPDATE/"
"DELETE/DROP/…) are permitted. Use with caution."
)
data: dict[str, list[dict[str, Any]] | str] = {}
result = self._fetch_available_tables()
if isinstance(result, str):
raise RuntimeError(f"Failed to fetch tables: {result}")
tables: list[dict[str, Any]] = result
for table in tables:
table_columns = self._fetch_all_available_columns(table["table_name"])View on GitHub (pinned to 754d7323be)
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
Example fix
# before tool = NL2SQLTool(db_uri="mysql+pymysql://u:p@h/db") # ImportError # after # pip install "crewai-tools[sqlalchemy]" tool = NL2SQLTool(db_uri="mysql+pymysql://u:p@h/db")
Defensive patterns
Strategy: validation
Validate before calling
def sqlalchemy_available() -> bool:
try:
import sqlalchemy # noqa: F401
return True
except ImportError:
return False
assert sqlalchemy_available(), 'pip install "crewai-tools[sqlalchemy]"' Try / catch
try:
tool = NL2SQLTool(db_uri=uri)
except ImportError as e:
if "sqlalchemy" in str(e):
raise SystemExit('Install: pip install "crewai-tools[sqlalchemy]"') from e
raise Prevention
- 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
When it happens
Trigger: Constructing NL2SQLTool(db_uri=...) without sqlalchemy installed; a broken/partial sqlalchemy install that fails import; installing crewai-tools without extras in a slim container.
Common situations: Minimal Docker images; adding crewai-tools to an existing project without the sql extras; sqlalchemy uninstalled accidentally by a dependency resolver conflict.
Related errors
- The 'tavily-python' package is required. 'click' and 'subpro
- The 'tavily-python' package is required. 'click' and 'subpro
- The 'tavily-python' package is required. 'click' and 'subpro
- The 'tavily-python' package is required. 'click' and 'subpro
- This subcommand requires the full crewai package. Install it
AI-assisted analysis of crewAIInc/crewAI@754d7323be (2026-08-15).
Data as JSON: /api/errors/59f40d39b8ae1fcd.
Report an issue: GitHub.