Skip to content

cosineSimilarityMatrix column-count guard compares matrixA to itself (never fires) #1558

Description

@Osamaali313

Summary

cosineSimilarityMatrix (typescript/src/internals/helpers/math.ts) has a column-count guard that can never fire, because it compares matrixA[0] to itself:

if ((matrixA[0]?.length ?? 0) !== (matrixA[0]?.length ?? 0)) {
  throw new ValueError("Matrices must have the same number of columns.");
}

The error message shows the intent is to compare A vs B, so the second operand should be matrixB[0].

Impact

Matrices with differing column counts bypass validation. For an empty matrixB the function returns [[]] instead of raising; for other mismatches it eventually throws the less-specific inner "Vectors must have equal length" error, and the intended guard message is unreachable.

Fix

if ((matrixA[0]?.length ?? 0) !== (matrixB[0]?.length ?? 0)) {

PR to follow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions