Skip to content

fix(ai): correct off-by-one in array form of mock model helpers#15143

Closed
serhiizghama wants to merge 2 commits into
vercel:mainfrom
serhiizghama:fix/mock-model-array-off-by-one
Closed

fix(ai): correct off-by-one in array form of mock model helpers#15143
serhiizghama wants to merge 2 commits into
vercel:mainfrom
serhiizghama:fix/mock-model-array-off-by-one

Conversation

@serhiizghama

Copy link
Copy Markdown

Problem

MockLanguageModelV3 / MockLanguageModelV4 and MockEmbeddingModelV3 / MockEmbeddingModelV4 accept an array of results to script multi-call test scenarios (e.g. tool-call → tool-result → final text for ToolLoopAgent). The array form silently misbehaved: the first call returned the second entry, and the last call returned undefined.

The cause is the indexer running after the call has already been pushed onto the call list:

this.doGenerate = async (options) => {
  this.doGenerateCalls.push(options);          // length is incremented…
  if (Array.isArray(doGenerate)) {
    return doGenerate[this.doGenerateCalls.length]; // …then used as the index
  }
  ...
};

The same off-by-one is in doStream for both V3 and V4 and in doEmbed for both embedding mocks.

Fixes #15141.

Solution

Index with length - 1 after the push so the n-th call returns the n-th array entry, matching the obvious user expectation.

Testing

Added mock-language-model-v3.test.ts, mock-language-model-v4.test.ts, mock-embedding-model-v3.test.ts, mock-embedding-model-v4.test.ts covering both doGenerate / doStream / doEmbed array forms. Verified the new tests fail against main and pass with the fix. Existing wrap-language-model and wrap-embedding-model tests continue to pass (they use empty arrays, so behavior is unchanged for that path).

When MockLanguageModelV3/V4 and MockEmbeddingModelV3/V4 are constructed
with an array of results, the indexer used doCalls.length after the
push, so the first scripted call returned the second entry and the
last call returned undefined.

Use length - 1 so calls return entries in declaration order.
Add tests confirming that MockLanguageModelV3/V4 and
MockEmbeddingModelV3/V4 return scripted results in declaration order
when constructed with an array. These would have failed against the
previous off-by-one indexer.
@lgrammel

lgrammel commented May 13, 2026

Copy link
Copy Markdown
Collaborator

Fixed via #15154

@lgrammel lgrammel closed this May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: MockLanguageModelV3 / V4 array form for doGenerate/doStream skips first entry (off-by-one)

2 participants