Skip to content

Python: Fix Postgres filter predicates treated as string literals#13603

Open
tejasai97 wants to merge 1 commit intomicrosoft:mainfrom
tejasai97:fix/postgres-filter-predicates-13595
Open

Python: Fix Postgres filter predicates treated as string literals#13603
tejasai97 wants to merge 1 commit intomicrosoft:mainfrom
tejasai97:fix/postgres-filter-predicates-13595

Conversation

@tejasai97
Copy link

Summary

  • Fixes Python postgres connector #13595: _lambda_parser in the Postgres connector returned plain Python strings for SQL WHERE clauses, which psycopg's sql.SQL.format() auto-wrapped as sql.Literal (quoted string literals) instead of embedding as SQL expressions
  • Changed _lambda_parser to return sql.Composable objects (sql.Identifier, sql.Literal, sql.SQL) so predicates are correctly embedded as SQL fragments
  • Added 8 unit tests covering equality, numeric comparison, AND/OR, IN, NOT, multiple filters, and chain comparisons

Motivation and Context

When using lambda filters with the Postgres vector search (e.g., filter=lambda x: x.content_type == "course"), the generated SQL was malformed:

-- Before (broken): predicate quoted as a string literal
WHERE '"content_type" = ''course'''

-- After (fixed): predicate embedded as a SQL expression
WHERE "content_type" = 'course'

Description

python/semantic_kernel/connectors/postgres.py:

  • _lambda_parser now returns sql.Composable instead of str
  • Column names use sql.Identifier() instead of manual f-string quoting
  • Constants use sql.Literal() instead of manual string escaping
  • Booleans/NULL use sql.SQL("TRUE") / sql.SQL("NULL")
  • All operators compose via sql.SQL("{} op {}").format(left, right)
  • Fixed missing leading space in WHERE clause in _construct_vector_query

python/tests/unit/connectors/memory/test_postgres_store.py:

  • Added 8 new filter tests; all 28 tests pass

Contribution Checklist

Change _lambda_parser to return sql.Composable objects instead of plain
strings so psycopg's sql.SQL.format() embeds them as SQL fragments
rather than wrapping them as sql.Literal.

Fixes microsoft#13595

Co-Authored-By: Claude <noreply@anthropic.com>
@moonbox3 moonbox3 added the python Pull requests for the Python Semantic Kernel label Feb 27, 2026
@tejasai97
Copy link
Author

@microsoft-github-policy-service agree company="Microsoft"

@tejasai97 tejasai97 marked this pull request as ready for review February 28, 2026 02:46
@tejasai97 tejasai97 requested a review from a team as a code owner February 28, 2026 02:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python postgres connector

2 participants