Suppose the same sentence yields different exported vectors when you batch it with a longer sentence. Before blaming the encoder, check how you turn token vectors into a sentence vector. An unmasked mean divides by the padded batch width and may include values from padding positions. A masked mean divides by the count of positions that actually contribute.

Prerequisites and example

Use Python 3.12 and save this as pooling.py. There are no model downloads or tensor packages. The input is one sequence of token vectors and a same-length mask of integer zeros and ones. The two-coordinate vectors are synthetic so you can compute the expected result without relying on any library implementation.

from math import fsum, isfinite

def mean_pool(tokens, mask):
    if not tokens or len(tokens) != len(mask) or not tokens[0]:
        raise ValueError("invalid shape")
    width = len(tokens[0])
    kept = []
    for row, bit in zip(tokens, mask):
        if len(row) != width or type(bit) is not int or bit not in (0, 1):
            raise ValueError("invalid row or mask")
        if bit:
            if not all(map(isfinite, row)):
                raise ValueError("non-finite active token")
            kept.append(row)
    if not kept:
        raise ValueError("all tokens masked")
    return tuple(fsum(col) / len(kept) for col in zip(*kept))

a = [(2, 4), (6, 8)]
result = mean_pool(a, [1, 1])
assert result == (4.0, 6.0)
assert mean_pool([(float("nan"), 99), *a], [0, 1, 1]) == result
assert mean_pool([*a, (99, 99)], [1, 1, 0]) == result
for tokens, mask in [(a, [0, 0]), (a, [1]), ([(1, 2), (4,)], [1, 0])]:
    try:
        mean_pool(tokens, mask)
    except ValueError:
        continue
    raise AssertionError("accepted invalid input")
print(result)

Run and check the invariant

python3 pooling.py
(4.0, 6.0)

For the active rows, the coordinate sums are 8 and 12 and the divisor is 2. Adding an ignored row must leave both the sums and the divisor unchanged. The left-padding fixture deliberately contains a NaN. Filtering it out before any arithmetic matters because multiplying NaN by zero does not sanitize it. The code still checks the width of an ignored row so a malformed tensor representation cannot pass merely by putting the malformed row behind a zero mask.

Troubleshoot an export mismatch

First compare the same active token vectors with different padding positions. If this passes but a full encoder export still differs, compare token IDs, attention masks and active hidden states before pooling. Then inspect the published model pipeline. It may include normalization, projection, a different pooling operation, or a policy on including prompt or special tokens. Adding or removing any of those is a representation change and may require rebuilding an index. This routine intentionally rejects an all-masked sequence instead of inventing a vector. Apply the reduction separately per sequence in a real batch, and compare floating-point implementations with tolerances appropriate to the model and dtype.

Sentence Transformers pooling module reference · Java Samples AI tutorials