Files
deepset-ai--haystack/docs-website/reference_versioned_docs/version-2.26/integrations-api/sqlalchemy.md
T
wehub-resource-sync c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:22:28 +08:00

3.0 KiB
Raw Blame History

title, id, description, slug
title id description slug
SQLAlchemy integrations-sqlalchemy SQLAlchemy integration for Haystack /integrations-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:

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

__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

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

to_dict() -> dict[str, Any]

Serialize the component to a dictionary.

Returns:

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

from_dict

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

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.