Skip to content

fix: reject multi-label cross encoders in SASEvaluator - #13251

Merged
bogdankostic merged 4 commits into
deepset-ai:mainfrom
AryanRG:fix/sas-multilabel-cross-encoder
Nov 12, 2026
Merged

bogdankostic merged 4 commits into
deepset-ai:mainfrom
AryanRG:fix/sas-multilabel-cross-encoder

Conversation

@AryanRG

@AryanRG AryanRG commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Related Issues

Separate unsupported-model case found while independently validating #13244. That PR now owns the merged one-label normalization change; this PR only rejects multi-label Cross Encoders.

Proposed Changes:

A three-label CrossEncoder returns nested individual scores even though SASEvaluator declares a list of floats. The aggregate then averages class columns rather than scalar answer similarities.

Reject CrossEncoder models whose num_labels is not 1 in warm_up(), before caching the loaded model. Existing input validation, empty-input behavior, lazy warm-up, #13244's Identity/Sigmoid normalization, and bi-encoder behavior remain unchanged. The PR adds an original regression, model and exception docstrings, a concise documentation note, and a fixes release note.

How did you test it?

Rechecked the revised warm-up validation on November 11 at f691f1e4f8eb and against current main bbe4efe50c71:

  • The new two-/three-label warm-up regression cases fail on the previous PR head (2 failures, 9 passes), then pass with the fix.
  • Revised branch: 11 focused SASEvaluator unit tests pass; 2 downloaded-model integration tests deselected.
  • Conflict-free combination with current main: 12 focused tests pass, including upstream's token-loading test; 2 integration tests deselected.
  • Unsupported models are rejected before being cached; single-label models load once across repeated warm_up() calls. The error assertion checks the actual label count, not the model-name digit.
  • Real tiny local BERT CrossEncoders with one, two and three labels verify explicit warm-up and lazy run() initialization. Multi-label models raise without being cached; the single-label score matches the earlier baseline. These random local models test API behavior, not semantic quality.
  • Focused mypy, Ruff lint/format, all applicable pre-commit hooks on the four changed files and git diff --check pass.

All checks ran through Hatch using retained Python 3.12 runtimes under the workspace resource guards. Full-project tests, downloaded pretrained-model integration tests, docs build and additional platform/Python combinations were not rerun. Hosted workflows may require maintainer approval; approval-gated or empty checks are not passing-test evidence.

Notes for the reviewer

#13244 merged after this PR opened and touched the same CrossEncoder branch. This branch is now rebased directly onto that merge. Its parameterized one-label mock explicitly sets num_labels = 1 so both Identity and Sigmoid normalization tests exercise the intended supported model. The #13244 production change, tests, imports, and release note remain base-only and do not appear in this PR's four-file diff.

A label-selection API would be a separate design. This change only rejects a shape that SASEvaluator cannot interpret as one semantic answer-similarity score.

This contribution was fully generated with AI assistance, including the implementation, regression test, documentation, and release note.

Checklist

  • Contributor guidelines and code of conduct read.
  • I shared the independent finding in the related fix: normalize SASEvaluator cross encoder scores based on the model #13244 discussion.
  • I added an original unit regression and updated docstrings.
  • I used a conventional fix title.
  • I updated the SASEvaluator documentation.
  • I added a fixes release note.
  • I ran pre-commit on every changed file and fixed all issues.

@vercel

vercel Bot commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

@AryanRG is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@AryanRG
AryanRG marked this pull request as ready for review October 14, 2026 14:34
@AryanRG
AryanRG requested a review from a team as a code owner October 14, 2026 14:34
@AryanRG
AryanRG requested review from bogdankostic and removed request for a team October 14, 2026 14:34
@AryanRG
AryanRG force-pushed the fix/sas-multilabel-cross-encoder branch from 8b1af31 to d3ad109 Compare October 15, 2026 09:11
@0xamlab

0xamlab commented Oct 15, 2026

Copy link
Copy Markdown

Confirmed the fix locally. On main, the repro with a three-label CrossEncoder mock returns individual_scores: [[2.0, -1.0, 0.0]] and score: 0.333..., averaging labels instead of rejecting the model. With the patch, hatch run test:unit test/components/evaluators/test_sas_evaluator.py passes 9/9 unit tests (2 integration deselected) in 46.4s, including the new regression test. The guard at haystack/components/evaluators/sas_evaluator.py:163 catches num_labels != 1 before model.predict is called, which is the right place to fail fast.

@AryanRG
AryanRG marked this pull request as draft October 17, 2026 00:41
@CLAassistant

CLAassistant commented Nov 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@bogdankostic

Copy link
Copy Markdown
Contributor

Hi @AryanRG! What's the status on this PR? It's in draft since about 4 weeks. Are you planning to keep on working on this or should we close it?

@AryanRG
AryanRG marked this pull request as ready for review November 11, 2026 14:15
@AryanRG

AryanRG commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@bogdankostic yes, I’d like to finish this — sorry for leaving it in draft. Rechecked the fix against current main; focused tests, type/style checks and pre-commit pass. It’s ready for review now.

@bogdankostic bogdankostic self-assigned this Nov 11, 2026

@bogdankostic bogdankostic 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.

Thanks for working on this @AryanRG! I think the check should live in warm_up() instead of run(). I left a few inline comments on this and on the test.

Comment on lines +162 to +170
# SAS needs one similarity score per pair, so multi-label models (for example NLI) are not supported
num_labels = self._similarity_model.num_labels
if num_labels != 1:
msg = (
f"SASEvaluator requires a Cross Encoder with exactly one output score, but '{self._model}' "
f"has {num_labels} labels. Use a single-score Cross Encoder or a Bi-Encoder "
"(SentenceTransformer) model."
)
raise ValueError(msg)

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.

Let's move this check into warm_up(), so that an unsupported model fails once at load time instead of on every non-empty run() call.

if cross_encoder_used:
    cross_encoder = CrossEncoder(self._model, device=device, token=token)
    if cross_encoder.num_labels != 1:
        raise ValueError(msg)
    self._similarity_model = cross_encoder

Comment on lines +127 to +128
# The mocked model has a length of 0, so run would try to load the real model without this
monkeypatch.setattr(SASEvaluator, "warm_up", lambda self: None)

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.

With the check in warm_up(), this test can follow test_warm_up_passes_token_to_hf: patch CrossEncoder and AutoConfig and call evaluator.warm_up(). Then we don't need this workaround here.

evaluator = SASEvaluator(model="cross-encoder/nli-deberta-v3-xsmall")
model = MagicMock(spec=CrossEncoder)
model.num_labels = 3
model.predict.return_value = np.array([[2.0, -1.0, 0.0]])

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.

This return value is never used, since the test asserts that predict isn't called. Let's remove it.

Suggested change
model.predict.return_value = np.array([[2.0, -1.0, 0.0]])

model.predict.return_value = np.array([[2.0, -1.0, 0.0]])
evaluator._similarity_model = model

with pytest.raises(ValueError, match="3"):

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.

match="3" also matches the v3 in the model name, so this passes regardless of the reported label count.

Suggested change
with pytest.raises(ValueError, match="3"):
with pytest.raises(ValueError, match="has 3 labels"):

@AryanRG

AryanRG commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@bogdankostic moved validation into warm_up() before caching the model. Reworked the test as suggested, removed the unused stub and tightened the error match. All 11 focused tests, mypy and pre-commit pass; the patch also passes 12 focused tests with current main.

@github-actions github-actions Bot added the type:documentation Improvements on the docs label Nov 12, 2026
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/evaluators
  sas_evaluator.py
Project Total  

This report was generated by python-coverage-comment-action

@bogdankostic bogdankostic 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.

Thanks for addressing the comments @AryanRG!

Congrats on your first contribution 🎉

@bogdankostic
bogdankostic merged commit fb31914 into deepset-ai:main Nov 12, 2026
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants