fix: RAG vector search SQL syntax error breaking assistant chat

- Use CAST(:embedding AS vector) instead of :embedding::vector to avoid
  SQLAlchemy named param conflict with PostgreSQL :: cast syntax
- Add db.rollback() before recording AI usage on failure to prevent
  InFailedSQLTransactionError cascade

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Michael Chihlas
2026-03-05 11:41:22 -05:00
parent 199cf315c6
commit 125d7e7d61
2 changed files with 3 additions and 2 deletions

View File

@@ -155,6 +155,7 @@ async def post_message(
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
except Exception as e:
logger.exception("Assistant chat message failed: %s", e)
await db.rollback()
await record_ai_usage(
user_id=current_user.id,
account_id=current_user.account_id,

View File

@@ -143,13 +143,13 @@ async def search(
te.chunk_text,
te.chunk_type,
te.node_id,
1 - (te.embedding <=> :embedding::vector) as similarity
1 - (te.embedding <=> CAST(:embedding AS vector)) as similarity
FROM tree_embeddings te
JOIN trees t ON t.id = te.tree_id
WHERE te.account_id = :account_id
AND t.deleted_at IS NULL
{exclude_clause}
ORDER BY te.embedding <=> :embedding::vector
ORDER BY te.embedding <=> CAST(:embedding AS vector)
LIMIT :limit
"""),
params,