Skip to content

SQLAlchemyTableRetriever: colons inside string literals are parsed as bind parameters #4278

Done

Description

@astroknot-sheep

Describe the bug
SQLAlchemyTableRetriever wraps every query and every init_script statement in sqlalchemy.text(). text() treats :name as a bind parameter, even inside a quoted string literal, and the component never passes parameters. So valid SQL that has a colon followed by a word character inside a string fails.

To Reproduce

from haystack_integrations.components.retrievers.sqlalchemy import SQLAlchemyTableRetriever

r = SQLAlchemyTableRetriever(drivername="sqlite", database=":memory:",
                             init_script=["CREATE TABLE t (id INT, body TEXT)"])
print(r.run(query="SELECT 1 WHERE 'see :ref' = 'see :ref'")["error"])
# (sqlalchemy.exc.InvalidRequestError) A value is required for bind parameter 'ref'

SQLAlchemyTableRetriever(drivername="sqlite", database=":memory:",
                         init_script=["CREATE TABLE t (b TEXT)", "INSERT INTO t VALUES ('{\"k\":1}')"]).warm_up()
# raises StatementError: A value is required for bind parameter '1'

Expected behavior
The SQL runs as written, the same as in sqlite3 / psql. JSON strings like '{"k":1}' and text such as 'see :ref' are common in seed data and in LLM-generated queries.

Additional context
Seen on main (5b1d6c1), SQLAlchemy 2.x, Python 3.14. A small fix is to escape colons before text() (\: is SQLAlchemy's documented escape), which keeps Postgres :: casts and % handling intact. I have a fix with a test ready and will open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions