Skip to main content
Version: 2.28-unstable

SQLAlchemy

haystack_integrations.components.retrievers.sqlalchemy.sqlalchemy_table_retriever

SQLAlchemyTableRetriever

Connects to any SQLAlchemy-supported database and executes a SQL query.

Returns results as a Pandas DataFrame and an optional Markdown-formatted table string. Supports any database backend that SQLAlchemy supports, including PostgreSQL, MySQL, SQLite, and MSSQL.

Usage example:

python
from haystack_integrations.components.retrievers.sqlalchemy import SQLAlchemyTableRetriever

retriever = SQLAlchemyTableRetriever(drivername="sqlite", database=":memory:")
retriever.warm_up()
result = retriever.run(query="SELECT 1 AS value")
print(result["dataframe"])
print(result["table"])

init

python
__init__(
drivername: str,
username: str | None = None,
password: Secret | None = None,
host: str | None = None,
port: int | None = None,
database: str | None = None,
init_script: list[str] | None = None,
) -> None

Initialize SQLAlchemyTableRetriever.

Parameters:

  • drivername (str) – The SQLAlchemy driver name (e.g., "sqlite", "postgresql+psycopg2").
  • username (str | None) – Database username.
  • password (Secret | None) – Database password as a Haystack Secret.
  • host (str | None) – Database host.
  • port (int | None) – Database port.
  • database (str | None) – Database name or path (e.g., ":memory:" for SQLite in-memory).
  • init_script (list[str] | None) – Optional list of SQL statements executed once on warm_up() (e.g., to create tables or insert seed data). Each statement should be a separate string in the list.

warm_up

python
warm_up() -> None

Initialize the database engine and execute init_script if provided.

Called automatically by run() on first invocation if not already warmed up.

to_dict

python
to_dict() -> dict[str, Any]

Serialize the component to a dictionary.

Returns:

  • dict[str, Any] – Dictionary with serialized data.

from_dict

python
from_dict(data: dict[str, Any]) -> SQLAlchemyTableRetriever

Deserialize the component from a dictionary.

Parameters:

  • data (dict[str, Any]) – Dictionary to deserialize from.

Returns:

  • SQLAlchemyTableRetriever – Deserialized component.

run

python
run(query: str) -> dict[str, Any]

Execute a SQL query and return the results.

Parameters:

  • query (str) – The SQL query to execute.

Returns:

  • dict[str, Any] – A dictionary with:

  • dataframe: A Pandas DataFrame with the query results.

  • table: A Markdown-formatted string of the results.

  • error: An error message if the query failed, otherwise an empty string.