SQL optional where clauses without exponentially large number of only slightly different queries
Given that the phrase column IN () is not valid SQL syntax, I do not believe there is a way to do this with SQL. If there is any SQL syntax to do this, it is going to be exceptionally convoluted, likely lead to sub-optimal query plans, and bloat your queries by requiring you to specify the same argument list multiple times -- once to check the argument list in SQL, and once to actually use it.

In Postgres, the database should not treat a single-item IN clause differently than an equality clause. Rewriting a single-item IN clause as an equality clause gains you nothing.

Since you are using SQLAlchemy already, you can conditionally add these conditions to your query based on what is in the argument lists:

# your values need to be tuples for Postgres to properly
# convert them to lists for IN clauses; Python lists will convert
# to Postgres ARRAY types
args1 = ("value",)
args2 = ()

# the "1=1" is an always-true check so that you can
# specify a WHERE clause, even with no provided arguments
clauses = ["1=1"]
if len(args1) &gt; 0:
    clauses.append("t.first_id IN :args1")
if len(args2) &gt; 0:
    clauses.append("t.second_id IN :args2")
...

query = """
SELECT *  -- in reality, something more complicated
FROM myTable t
WHERE
""" + " AND ".join(clauses)

result = conn.execute(text(query), args1=args1, args2=args2, ...).fetchall()


SQLAlchemy does not care if you provide unused parameters. In other words, if args2 is empty, passing it to execute() will work here, even though the query will not have a placeholder for it.

If you are using the SQLAlchemy Core API for generating queries, you can chain your where() clauses in a similar way:

s = select([myTable])
if len(args1) &gt; 0:
    s = s.where(myTable.c.first_id.in_(args1))
if len(args2) &gt; 0:
    s = s.where(myTable.c.second_id.in_(args2))
...
result = conn.execute(s).fetchall()