Skip to content

fix(internals): compare both matrices' column counts in cosineSimilarityMatrix - #1559

Open
Osamaali313 wants to merge 1 commit into
i-am-bee:mainfrom
Osamaali313:fix/cosine-matrix-columns
Open

fix(internals): compare both matrices' column counts in cosineSimilarityMatrix#1559
Osamaali313 wants to merge 1 commit into
i-am-bee:mainfrom
Osamaali313:fix/cosine-matrix-columns

Conversation

@Osamaali313

Copy link
Copy Markdown
Contributor

Closes #1558

Problem

cosineSimilarityMatrix (typescript/src/internals/helpers/math.ts) validates that both matrices have the same column count, but the guard compares matrixA[0] to itself, so it can never fire:

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

Mismatched column counts bypass validation — an empty matrixB returns [[]] instead of raising, and other mismatches fall through to the less-specific inner "Vectors must have equal length" error, leaving the intended guard message unreachable.

Fix

Compare matrixB[0] (a one-token typo fix), as the error message intends:

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

Tests

Added math.test.ts asserting that mismatched column counts ([[1,2]] vs [[1,2,3]], and vs []) throw "Matrices must have the same number of columns.", and that a matching pair still computes similarity. Before the fix the mismatch cases don't raise that error; after, they do.

…ityMatrix

The column-count guard compared `matrixA[0]` to itself, so it could never
fire and matrices with differing column counts bypassed validation
(mismatched input returned `[[]]` instead of raising). Compare
`matrixB[0]` as the error message ("Matrices must have the same number of
columns.") intends.

Signed-off-by: Osamaali313 <s.osamaali72@gmail.com>
@Osamaali313
Osamaali313 requested a review from a team as a code owner July 7, 2026 21:31
Copilot AI review requested due to automatic review settings July 7, 2026 21:31
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jul 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown

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 reached their quota limit.

@github-actions github-actions Bot added the typescript Typescript related functionality label Jul 7, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request fixes a bug in the cosineSimilarityMatrix function where the column count of matrixA was incorrectly compared to itself instead of matrixB. It also introduces a new test suite to verify this behavior. There are no review comments, so no feedback is provided.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files. typescript Typescript related functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants