Skip to content

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

Open

Description

@BlueX888

Describe the bug

SQL Server vector searches are ordered through the shared DISTANCE_FUNCTION_DIRECTION_HELPER, which treats dot product as a similarity. Two user-visible failures follow:

  • A vector field left at its 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, so every SQL Server search fails at runtime with KeyError: <DistanceFunction.DEFAULT: 'DEFAULT'>.
  • A field using DistanceFunction.DOT_PROD builds ORDER BY ... DESC, returning the least similar rows first.

VECTOR_DISTANCE is a distance for every metric SQL Server supports; the dot metric is the negative dot product and Microsoft documents that smaller values are more similar.

To Reproduce

No database or model call is needed to construct the query:

from semantic_kernel.connectors.sql_server import _build_search_query
from semantic_kernel.data.vector import DistanceFunction, VectorSearchOptions, VectorStoreField

key = VectorStoreField("key", name="id", type="str")
vector = VectorStoreField("vector", name="embedding", type="float", dimensions=3)
print(vector.distance_function)  # DistanceFunction.DEFAULT

_build_search_query(
    "dbo",
    "test",
    key,
    [],
    [vector],
    [0.1, 0.2, 0.3],
    VectorSearchOptions(vector_property_name="embedding"),
)
# KeyError: <DistanceFunction.DEFAULT: 'DEFAULT'>

The same failure is reachable through SqlServerCollection.search(vector=...) when the model's vector field omits distance_function. With distance_function=DistanceFunction.DOT_PROD, the generated query ends with ORDER BY _vector_distance_value DESC.

Expected behavior

The default search should return results ordered by cosine distance, nearest first. Dot-product results should also be ordered ascending: SQL Server's VECTOR_DISTANCE('dot', ...) is the negative dot product, and smaller values indicate more similar vectors.

Screenshots

Not applicable.

Platform

  • Language: Python
  • Source: main at d41312f
  • AI model: Not applicable (query construction only)
  • IDE: Not applicable
  • OS: Windows 11

Additional context

InMemoryCollection already resolves DEFAULT to COSINE_DISTANCE. The .NET SQL Server connector made the same dot-product ordering fix in #13071 (NegativeDotProductSimilarity from DESC to ASC). I have a fix and regression tests ready and will open a PR linking this issue.

Activity

  1. added
    .NETIssue or Pull requests regarding .NET code
    pythonPull requests for the Python Semantic Kernel
    on Nov 3, 2026
  2. changed the title [-]Python: Bug: SqlServerCollection search fails with the default distance function and orders dot results farthest-first[/-] [+].Net: Python: Bug: SqlServerCollection search fails with the default distance function and orders dot results farthest-first[/+] on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETIssue or Pull requests regarding .NET codepythonPull requests for the Python Semantic Kerneltriage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions