fix(openai-adapters): Don't emit usage from fullStream finish event

The Vercel AI SDK's fullStream may emit a finish event with incomplete or
zero usage data. The correct usage is available via the stream.usage Promise
which resolves after the stream completes.

Changed strategy:
- convertVercelStream now skips the finish event entirely (returns null)
- After consuming fullStream, we await stream.usage Promise
- Emit usage chunk with complete data from the Promise

This fixes the "expected 0 to be greater than 0" test failures.

Co-authored-by: nate <nate@continue.dev>
Generated with [Continue](https://continue.dev)
This commit is contained in:
continue[bot] 2025-12-10 19:36:54 +00:00
parent 64f4924984
commit a89187b409
3 changed files with 46 additions and 33 deletions

View file

@ -665,10 +665,33 @@ export class AnthropicApi implements BaseLlmApi {
});
// Convert Vercel AI SDK stream to OpenAI format
// Note: fullStream includes a "finish" event with usage, which convertVercelStream will handle
yield* convertVercelStream(stream.fullStream as any, {
// Note: We need to consume fullStream first, then await stream.usage Promise
for await (const chunk of convertVercelStream(stream.fullStream as any, {
model: body.model,
});
})) {
yield chunk;
}
// Await final usage from stream.usage Promise with Anthropic-specific cache details
const finalUsage = await stream.usage;
if (finalUsage) {
const { usageChatChunk } = await import("../util.js");
yield usageChatChunk({
model: body.model,
usage: {
prompt_tokens: finalUsage.promptTokens,
completion_tokens: finalUsage.completionTokens,
total_tokens: finalUsage.totalTokens,
prompt_tokens_details: {
cached_tokens:
(finalUsage as any).promptTokensDetails?.cachedTokens ?? 0,
cache_read_tokens:
(finalUsage as any).promptTokensDetails?.cachedTokens ?? 0,
cache_write_tokens: 0,
} as any,
},
});
}
}
private getHeaders(): Record<string, string> {

View file

@ -334,10 +334,25 @@ export class OpenAIApi implements BaseLlmApi {
});
// Convert Vercel AI SDK stream to OpenAI format
// Note: fullStream includes a "finish" event with usage, which convertVercelStream will handle
yield* convertVercelStream(stream.fullStream as any, {
// Note: We need to consume fullStream first, then await stream.usage Promise
for await (const chunk of convertVercelStream(stream.fullStream as any, {
model: modifiedBody.model,
});
})) {
yield chunk;
}
// Await final usage from stream.usage Promise (resolves after fullStream completes)
const finalUsage = await stream.usage;
if (finalUsage) {
yield usageChatChunk({
model: modifiedBody.model,
usage: {
prompt_tokens: finalUsage.promptTokens,
completion_tokens: finalUsage.completionTokens,
total_tokens: finalUsage.totalTokens,
},
});
}
}
async completionNonStream(
body: CompletionCreateParamsNonStreaming,

View file

@ -121,33 +121,8 @@ export function convertVercelStreamPart(
});
case "finish":
// Emit usage chunk at the end if usage data is present
if (part.usage && part.usage.completionTokens !== undefined) {
// Check for Anthropic-specific cache token details
const promptTokensDetails =
(part.usage as any).promptTokensDetails?.cachedTokens !== undefined
? {
cached_tokens:
(part.usage as any).promptTokensDetails.cachedTokens ?? 0,
cache_read_tokens:
(part.usage as any).promptTokensDetails.cachedTokens ?? 0,
cache_write_tokens: 0,
}
: undefined;
return usageChatChunk({
model,
usage: {
prompt_tokens: part.usage.promptTokens || 0,
completion_tokens: part.usage.completionTokens || 0,
total_tokens: part.usage.totalTokens || 0,
...(promptTokensDetails
? { prompt_tokens_details: promptTokensDetails as any }
: {}),
},
});
}
// If no usage data, don't emit a usage chunk
// Don't emit usage from finish event - we'll get it from stream.usage Promise
// The finish event may have incomplete usage data, so we wait for the Promise
return null;
case "error":