Skip to content

Commit bde31d1

Browse files
authored
Merge pull request QwenLM#1448 from QwenLM/fix/openai-compatible
fix(core): handle missing delta in OpenAI stream chunks
2 parents 6714f9c + cba9c42 commit bde31d1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/core/src/core/openaiContentGenerator/converter.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,27 @@ describe('OpenAIContentConverter', () => {
207207
expect.objectContaining({ text: 'visible text' }),
208208
);
209209
});
210+
211+
it('should not throw when streaming chunk has no delta', () => {
212+
const chunk = converter.convertOpenAIChunkToGemini({
213+
object: 'chat.completion.chunk',
214+
id: 'chunk-2',
215+
created: 456,
216+
choices: [
217+
{
218+
index: 0,
219+
// Some OpenAI-compatible providers may omit delta entirely.
220+
delta: undefined,
221+
finish_reason: null,
222+
logprobs: null,
223+
},
224+
],
225+
model: 'gpt-test',
226+
} as unknown as OpenAI.Chat.ChatCompletionChunk);
227+
228+
const parts = chunk.candidates?.[0]?.content?.parts;
229+
expect(parts).toEqual([]);
230+
});
210231
});
211232

212233
describe('convertGeminiToolsToOpenAI', () => {

packages/core/src/core/openaiContentGenerator/converter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ export class OpenAIContentConverter {
799799
const parts: Part[] = [];
800800

801801
const reasoningText = (choice.delta as ExtendedCompletionChunkDelta)
802-
.reasoning_content;
802+
?.reasoning_content;
803803
if (reasoningText) {
804804
parts.push({ text: reasoningText, thought: true });
805805
}

0 commit comments

Comments
 (0)