Skip to content

Python: fix(sql_server): order every distance metric ascending - #14714

Open
哈基米 (BlueX888) wants to merge 1 commit into
microsoft:mainfrom
BlueX888:contrib/sqlserver-default-distance
Open

哈基米 (BlueX888) wants to merge 1 commit into
microsoft:mainfrom
BlueX888:contrib/sqlserver-default-distance

Conversation

@BlueX888

Copy link
Copy Markdown
Contributor

Fixes #14713

Problem

SQL Server vector searches are ordered using the shared DISTANCE_FUNCTION_DIRECTION_HELPER, which models dot product as a similarity. VECTOR_DISTANCE is a distance for every SQL Server metric — the dot metric is documented as the negative dot product, with smaller values more similar — so this has two consequences:

  • A vector field left at the default distance function (VectorStoreField("vector", dimensions=...) → DistanceFunction.DEFAULT) is accepted by DISTANCE_FUNCTION_MAP (mapped to cosine) but has no entry in the direction helper. Every search fails at runtime:

    VectorSearchExecutionException: An error occurred during the search:
    <DistanceFunction.DEFAULT: 'DEFAULT'> -> KeyError: <DistanceFunction.DEFAULT: 'DEFAULT'>
    
  • A field using DistanceFunction.DOT_PROD builds ORDER BY ... DESC, returning the least similar rows first.

InMemoryCollection already resolves DEFAULT to cosine; SQL Server's supported metrics are all distances, so the direction is always ascending.

Change

Order SQL Server vector results ascending for every supported metric and drop the now-unused direction-helper import. The metric name passed to VECTOR_DISTANCE is unchanged.

Verification

  • test_build_search_query now runs with COSINE_DISTANCE, EUCLIDEAN_DISTANCE, DOT_PROD, and DEFAULT, asserting the matching VECTOR_DISTANCE metric and ORDER BY ... ASC.
  • Added a public-path SqlServerCollection.search test whose model leaves the vector field at DistanceFunction.DEFAULT.
  • On the base commit the DOT_PROD and DEFAULT cases fail (DESC and KeyError respectively); with the change, all 82 tests in tests/unit/connectors/memory/test_sql_server.py pass.
  • ruff check and package mypy -p semantic_kernel are clean.

Copilot AI balanced review requested due to automatic review settings November 3, 2026 03:45
@BlueX888
哈基米 (BlueX888) requested a review from a team as a code owner November 3, 2026 03:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Nov 3, 2026

This branch was successfully deployed

1 active deployment
github-app-auth — d65d6950 Deployed Nov 3, 2026 by BlueX888 via add_label #29566
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.

.Net: Python: Bug: SqlServerCollection search fails with the default distance function and orders dot results farthest-first

2 participants