crewAIInc/crewAI · error · ValueError

Database URI is required for MySQL loader

Error message

Database URI is required for MySQL loader

What it means

Raised by MySQLLoader.load() when metadata does not contain a non-empty 'db_uri' key. The loader takes its connection string exclusively from kwargs metadata (not from the source, which holds the SQL query); a missing, None, or empty db_uri fails fast before any parsing or connection attempt.

Source

Thrown at lib/crewai-tools/src/crewai_tools/rag/loaders/mysql_loader.py:30

class MySQLLoader(BaseLoader):
    """Loader for MySQL database content."""

    def load(self, source: SourceContent, **kwargs: Any) -> LoaderResult:  # type: ignore[override]
        """Load content from a MySQL database table.

        Args:
            source: SQL query (e.g., "SELECT * FROM table_name")
            **kwargs: Additional arguments including db_uri

        Returns:
            LoaderResult with database content
        """
        metadata = kwargs.get("metadata", {})
        db_uri = metadata.get("db_uri")

        if not db_uri:
            raise ValueError("Database URI is required for MySQL loader")

        query = source.source

        parsed = urlparse(db_uri)
        if parsed.scheme not in ["mysql", "mysql+pymysql"]:
            raise ValueError(f"Invalid MySQL URI scheme: {parsed.scheme}")

        connection_params = {
            "host": parsed.hostname or "localhost",
            "port": parsed.port or 3306,
            "user": parsed.username,
            "password": parsed.password,
            "database": parsed.path.lstrip("/") if parsed.path else None,
            "charset": "utf8mb4",
            "cursorclass": DictCursor,
        }

        if not connection_params["database"]:

View on GitHub (pinned to 754d7323be)

Solutions

  1. Pass the URI in metadata: MySQLLoader().load(SourceContent(query), metadata={'db_uri': os.environ['DATABASE_URI']}).
  2. Fail loudly at startup if the env var is absent instead of forwarding None.
  3. Check the kwarg name and nesting (metadata dict, not a flat kwarg) against the loader signature.

Example fix

# before
result = MySQLLoader().load(SourceContent('SELECT 1'), db_uri='mysql://u:p@h/db')  # ignored

# after
import os
result = MySQLLoader().load(
    SourceContent('SELECT 1'),
    metadata={'db_uri': os.environ['DATABASE_URI']},
)
Defensive patterns

Strategy: validation

Validate before calling

import os\n\ndef require_db_uri() -> str:\n    uri = os.environ.get('MYSQL_URI')\n    if not uri:\n        raise RuntimeError('MYSQL_URI is not configured')\n    return uri

Prevention

When it happens

Trigger: Calling MySQLLoader().load(SourceContent('SELECT * FROM users')) with no kwargs at all; passing db_uri as a top-level kwarg instead of inside metadata; passing metadata={'db_uri': ''} or {'db_uri': None}.

Common situations: Confusing the API shape — the query is the source and the URI is buried in metadata; environments where the DB URI env var is unset so an empty string is forwarded; config helpers that drop keys with falsy values; copy-paste from PostgresLoader examples that use a different kwarg name.

Related errors


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