Skip to main content

A SQLAlchemy integration for the Haystack framework.

Project description

sqlalchemy-haystack

PyPI - Version PyPI - Python Version


A Haystack integration for querying SQL databases via SQLAlchemy. Provides a SQLAlchemyTableRetriever component that connects to any SQLAlchemy-supported database, executes a SQL query, and returns results as a Pandas DataFrame and an optional Markdown table.

Installation

pip install sqlalchemy-haystack

You also need to install the appropriate database driver for your backend:

Backend Driver package
PostgreSQL psycopg2-binary or psycopg[binary]
MySQL / MariaDB pymysql or mysqlclient
SQLite built-in (no extra package needed)
MSSQL pyodbc
Oracle cx_oracle or oracledb

Usage

from haystack_integrations.components.retrievers.sqlalchemy import SQLAlchemyTableRetriever

# SQLite in-memory example (no driver needed)
retriever = SQLAlchemyTableRetriever(
    drivername="sqlite",
    database=":memory:",
    init_script=[
        "CREATE TABLE products (id INTEGER, name TEXT, price REAL)",
        "INSERT INTO products VALUES (1, 'Widget', 9.99)",
        "INSERT INTO products VALUES (2, 'Gadget', 19.99)",
    ],
)
retriever.warm_up()

result = retriever.run(query="SELECT * FROM products WHERE price < 15")
print(result["dataframe"])
print(result["table"])

For PostgreSQL:

from haystack.utils import Secret

retriever = SQLAlchemyTableRetriever(
    drivername="postgresql+psycopg2",
    host="localhost",
    port=5432,
    database="mydb",
    username="myuser",
    password=Secret.from_env_var("DB_PASSWORD"),
)

Security

This component executes raw SQL queries passed at runtime. Keep the following in mind:

  • Never pass unsanitised user input directly as a query — this exposes you to SQL injection.
  • Use a read-only database user. This is the most effective mitigation. Even if a malicious query is executed, a read-only user cannot modify or delete data.
  • Restrict database permissions to the minimum required — specific tables and schemas only, no DDL privileges (no CREATE, DROP, ALTER).

Contributing

Refer to the general Contribution Guidelines.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_haystack-0.1.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_haystack-0.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_haystack-0.1.0.tar.gz.

File metadata

  • Download URL: sqlalchemy_haystack-0.1.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for sqlalchemy_haystack-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7897b0ecf49913d334e4c0939b65cd0410212d63548fb022890f4cfce8ca6930
MD5 ba366c4938e1d25b6428009e81ed51ce
BLAKE2b-256 4a53b84557a577fefe24ff3bddbdc2cada4a73bde00b458659e618d26d57fe27

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_haystack-0.1.0.tar.gz:

Publisher: CI_pypi_release.yml on deepset-ai/haystack-core-integrations

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlalchemy_haystack-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_haystack-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7356c5a2a00c6d3dd3a71576c3b69c298dceda3c3a2bd3c83a73defbf1ec841c
MD5 8c1ad46e7ea774dc72b48f254390dffb
BLAKE2b-256 3d1e3e3185031b23477d61da0401c2ac16c4b38b27ed1e2033c6c31e539cca96

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_haystack-0.1.0-py3-none-any.whl:

Publisher: CI_pypi_release.yml on deepset-ai/haystack-core-integrations

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page