Skip to content

Commit

Permalink
🐛 fix: fix a corner case of tools_call with empty object (#3955)
Browse files Browse the repository at this point in the history
* 🐛 fix: fix got content in finish_reason

* 🐛 fix: fix a corner case of `tools_call` with empty object
  • Loading branch information
hezhijie0327 committed Sep 16, 2024
1 parent ece60ee commit d3fabdc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/libs/agent-runtime/utils/streams/openai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,49 @@ describe('OpenAIStream', () => {
);
});

it('should handle content with tool_calls but is an empty object', async () => {
// data: {"id":"chatcmpl-A7pokGUqSov0JuMkhiHhWU9GRtAgJ", "object":"chat.completion.chunk", "created":1726430846, "model":"gpt-4o-2024-05-13", "choices":[{"index":0, "delta":{"content":" today", "role":"", "tool_calls":[]}, "finish_reason":"", "logprobs":""}], "prompt_annotations":[{"prompt_index":0, "content_filter_results":null}]}
const mockOpenAIStream = new ReadableStream({
start(controller) {
controller.enqueue({
choices: [
{
"index": 0,
"delta": {
"content": "Some contents",
"role": "",
"tool_calls": []
},
"finish_reason": "",
"logprobs": ""
}
],
id: '456',
});

controller.close();
},
});

const onToolCallMock = vi.fn();

const protocolStream = OpenAIStream(mockOpenAIStream, {
onToolCall: onToolCallMock,
});

const decoder = new TextDecoder();
const chunks = [];

// @ts-ignore
for await (const chunk of protocolStream) {
chunks.push(decoder.decode(chunk, { stream: true }));
}

expect(chunks).toEqual(
['id: 456', 'event: text', `data: "Some contents"\n`].map((i) => `${i}\n`),
);
});

it('should handle other delta data', async () => {
const mockOpenAIStream = new ReadableStream({
start(controller) {
Expand Down
2 changes: 1 addition & 1 deletion src/libs/agent-runtime/utils/streams/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const transformOpenAIStream = (
return { data: chunk, id: chunk.id, type: 'data' };
}

if (item.delta?.tool_calls) {
if (typeof item.delta?.tool_calls === 'object' && item.delta.tool_calls?.length > 0) {
return {
data: item.delta.tool_calls.map((value, index): StreamToolCallChunkData => {
if (stack && !stack.tool) {
Expand Down

0 comments on commit d3fabdc

Please sign in to comment.