Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/core/src/core/openaiContentGenerator/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,36 @@ describe('OpenAIContentConverter', () => {

expect(response.candidates).toEqual([]);
});

it('keeps the estimated prompt/completion split summing to total tokens', () => {
// When a provider reports only total_tokens, the 70/30 estimate must
// still add back up to the total instead of rounding each half on its
// own (5 would otherwise become 4 + 2 = 6).
const response = converter.convertOpenAIResponseToGemini(
{
object: 'chat.completion',
id: 'chatcmpl-usage',
created: 123,
model: 'test-model',
choices: [
{
index: 0,
message: { role: 'assistant', content: 'hi' },
finish_reason: 'stop',
logprobs: null,
},
],
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 5 },
} as unknown as OpenAI.Chat.ChatCompletion,
requestContext,
);

const usage = response.usageMetadata;
expect(usage?.totalTokenCount).toBe(5);
expect(
(usage?.promptTokenCount ?? 0) + (usage?.candidatesTokenCount ?? 0),
).toBe(5);
});
});

describe('OpenAI -> Gemini reasoning content', () => {
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/core/openaiContentGenerator/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1182,9 +1182,11 @@ export function convertOpenAIResponseToGemini(
let finalCompletionTokens = completionTokens;

if (totalTokens > 0 && promptTokens === 0 && completionTokens === 0) {
// Estimate: assume 70% input, 30% output
// Estimate: assume 70% input, 30% output. Derive completion from the
// remainder so the two halves always add back up to totalTokens rather
// than rounding each independently (e.g. 5 would give 4 + 2 = 6).
finalPromptTokens = Math.round(totalTokens * 0.7);
finalCompletionTokens = Math.round(totalTokens * 0.3);
finalCompletionTokens = totalTokens - finalPromptTokens;
}

response.usageMetadata = {
Expand Down Expand Up @@ -1379,9 +1381,11 @@ export function convertOpenAIChunkToGemini(
let finalCompletionTokens = completionTokens;

if (totalTokens > 0 && promptTokens === 0 && completionTokens === 0) {
// Estimate: assume 70% input, 30% output
// Estimate: assume 70% input, 30% output. Derive completion from the
// remainder so the two halves always add back up to totalTokens rather
// than rounding each independently (e.g. 5 would give 4 + 2 = 6).
finalPromptTokens = Math.round(totalTokens * 0.7);
finalCompletionTokens = Math.round(totalTokens * 0.3);
finalCompletionTokens = totalTokens - finalPromptTokens;
}

response.usageMetadata = {
Expand Down
Loading