Skip to content

Commit 8262baa

Browse files
committed
TypeScript: fix streaming replay and empty OpenAI responses (#1216)
Address PR #8414 review comments by omitting content-free aggregate messages while preserving response metadata and rejecting streams with no completion choices. Add replay, history, metadata-only, empty-stream, and usage-trailer regressions.
1 parent 3a44a23 commit 8262baa

5 files changed

Lines changed: 181 additions & 2 deletions

File tree

‎typescript/packages/core/src/types.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ export class ChatResponse {
261261
}
262262

263263
return new ChatResponse({
264-
messages: [...messages.values()],
264+
// Metadata-only updates must not create empty turns in the conversation transcript.
265+
messages: [...messages.values()].filter((message) => message.contents.length > 0),
265266
...(responseId === undefined ? {} : { responseId }),
266267
...(finishReason === undefined ? {} : { finishReason }),
267268
...(usageDetails === undefined ? {} : { usageDetails }),

‎typescript/packages/core/test/chat-client.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,62 @@ describe("BaseChatClient function invocation", () => {
113113
expect(response.messages.map((message) => message.role)).toEqual(["assistant", "tool", "assistant"]);
114114
});
115115

116+
it("omits suppressed replay turns from streaming responses and follow-up history", async () => {
117+
const client = new TestChatClient();
118+
let executions = 0;
119+
const tool = defineTool({
120+
name: "process",
121+
parameters,
122+
execute: ({ value }) => {
123+
executions += 1;
124+
return value;
125+
},
126+
});
127+
const call = { ...functionCall("call-1", "process", { value: "one" }), id: "stable-occurrence" };
128+
client.streamingResponses.push(
129+
[new ChatResponseUpdate({ role: "assistant", messageId: "original-turn", contents: call })],
130+
[
131+
new ChatResponseUpdate({ role: "assistant", messageId: "replayed-turn", contents: call }),
132+
new ChatResponseUpdate({
133+
responseId: "replayed-response",
134+
messageId: "replayed-metadata",
135+
finishReason: "tool_calls",
136+
usageDetails: { outputTokenCount: 3 },
137+
}),
138+
],
139+
[
140+
new ChatResponseUpdate({
141+
role: "assistant",
142+
messageId: "final-turn",
143+
responseId: "final-response",
144+
contents: "done",
145+
finishReason: "stop",
146+
usageDetails: { outputTokenCount: 2 },
147+
}),
148+
],
149+
);
150+
151+
const stream = client.getResponse("hello", { stream: true, options: { tools: [tool] } });
152+
const updates = [];
153+
for await (const update of stream) updates.push(update);
154+
const response = await stream.getFinalResponse();
155+
156+
expect(executions).toBe(1);
157+
expect(client.requests).toHaveLength(3);
158+
expect(client.requests[2]?.options.toolChoice).toBe("none");
159+
expect(client.requests[2]?.messages).toEqual(client.requests[1]?.messages);
160+
expect(client.requests[2]?.messages.map((message) => message.role)).toEqual(["user", "assistant", "tool"]);
161+
expect(response.messages.map((message) => message.role)).toEqual(["assistant", "tool", "assistant"]);
162+
expect(response.messages.every((message) => message.contents.length > 0)).toBe(true);
163+
expect(response.text).toBe("done");
164+
expect(response.responseId).toBe("final-response");
165+
expect(response.finishReason).toBe("stop");
166+
expect(response.usageDetails).toEqual({ outputTokenCount: 5 });
167+
expect(updates.some((update) => update.contents.length === 0 && update.usageDetails?.outputTokenCount === 3)).toBe(
168+
true,
169+
);
170+
});
171+
116172
it("executes parallel calls while preserving model result order", async () => {
117173
const client = new TestChatClient();
118174
const completed: string[] = [];

‎typescript/packages/core/test/types.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ describe("core response types", () => {
4747
await expect(responseStream.getFinalResponse()).resolves.toBe(response);
4848
});
4949

50+
it.each([false, true])("preserves metadata without creating empty messages (with text: %s)", (withText) => {
51+
const response = ChatResponse.fromUpdates([
52+
...(withText ? [new ChatResponseUpdate({ role: "assistant", messageId: "message-1", contents: "hello" })] : []),
53+
new ChatResponseUpdate({
54+
role: "assistant",
55+
messageId: "metadata-only",
56+
responseId: "response-1",
57+
finishReason: "stop",
58+
usageDetails: { outputTokenCount: 3 },
59+
additionalProperties: { model: "test-model" },
60+
}),
61+
]);
62+
63+
expect(response.messages).toHaveLength(withText ? 1 : 0);
64+
expect(response.text).toBe(withText ? "hello" : "");
65+
expect(response.responseId).toBe("response-1");
66+
expect(response.finishReason).toBe("stop");
67+
expect(response.usageDetails).toEqual({ outputTokenCount: 3 });
68+
expect(response.additionalProperties).toEqual({ model: "test-model" });
69+
});
70+
5071
it("keeps stream failures sticky during finalization", async () => {
5172
const failure = new Error("stream failed");
5273
const responseStream = new ResponseStream(

‎typescript/packages/openai/src/index.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,10 @@ export class OpenAIChatCompletionClient extends BaseChatClient<OpenAIChatComplet
268268

269269
async *#mapStream(chunks: AsyncIterable<ChatCompletionChunk>): AsyncGenerator<ChatResponseUpdate> {
270270
const pendingCalls = new Map<number, PendingToolCall>();
271+
let hasChoice = false;
271272
for await (const chunk of chunks) {
272273
const choice = chunk.choices[0];
274+
hasChoice ||= choice !== undefined;
273275
const contents: Content[] = [];
274276
if (choice?.delta.content !== undefined && choice.delta.content !== null) {
275277
contents.push({ type: "text", text: choice.delta.content });
@@ -323,6 +325,10 @@ export class OpenAIChatCompletionClient extends BaseChatClient<OpenAIChatComplet
323325
}
324326
}
325327

328+
if (!hasChoice) {
329+
throw new AgentInvalidResponseError("OpenAI returned no completion choices.");
330+
}
331+
326332
const unresolved = [...pendingCalls.values()].find(
327333
(pending) => pending.id === undefined && (pending.name.length > 0 || pending.argumentsValue.length > 0),
328334
);

‎typescript/packages/openai/test/openai-chat-client.test.ts‎

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineTool } from "@microsoft/agent-framework-core";
1+
import { AgentInvalidResponseError, defineTool } from "@microsoft/agent-framework-core";
22
import OpenAI from "openai";
33
import type {
44
ChatCompletion,
@@ -154,6 +154,55 @@ describe("OpenAIChatCompletionClient", () => {
154154
expect(response.text).toBe("Rainy");
155155
});
156156

157+
it.each([
158+
{ name: "empty stream", chunks: [] },
159+
{ name: "choice-less chunks", chunks: [{ ...chunk("response-1", {}), choices: [] }] },
160+
{
161+
name: "usage-only stream",
162+
chunks: [
163+
{
164+
...chunk("response-1", {}),
165+
choices: [],
166+
usage: { prompt_tokens: 2, completion_tokens: 3, total_tokens: 5 },
167+
},
168+
],
169+
},
170+
])("rejects a malformed $name without any completion choice", async ({ chunks }) => {
171+
const { client, create } = mockOpenAI([chunks]);
172+
const chatClient = new OpenAIChatCompletionClient({ client, model: "gpt-test" });
173+
const stream = chatClient.getResponse("request", { stream: true });
174+
const consume = async () => {
175+
for await (const _update of stream) {
176+
// Even if usage metadata is emitted, the response must not finalize successfully.
177+
}
178+
};
179+
180+
await expect(consume()).rejects.toThrow(AgentInvalidResponseError);
181+
await expect(stream.getFinalResponse()).rejects.toThrow("OpenAI returned no completion choices.");
182+
expect(create).toHaveBeenCalledTimes(1);
183+
});
184+
185+
it("preserves a valid response followed by a usage-only chunk", async () => {
186+
const { client } = mockOpenAI([
187+
[
188+
chunk("response-1", { role: "assistant", content: "hello" }, "stop"),
189+
{
190+
...chunk("response-1", {}),
191+
choices: [],
192+
usage: { prompt_tokens: 2, completion_tokens: 3, total_tokens: 5 },
193+
},
194+
],
195+
]);
196+
const chatClient = new OpenAIChatCompletionClient({ client, model: "gpt-test" });
197+
const response = await chatClient.getResponse("request", { stream: true }).getFinalResponse();
198+
199+
expect(response.text).toBe("hello");
200+
expect(response.messages).toHaveLength(1);
201+
expect(response.responseId).toBe("response-1");
202+
expect(response.finishReason).toBe("stop");
203+
expect(response.usageDetails).toEqual({ inputTokenCount: 2, outputTokenCount: 3, totalTokenCount: 5 });
204+
});
205+
157206
it("preserves non-streaming and streaming refusals", async () => {
158207
const nonStreamingRefusal = completion("response-1", null);
159208
nonStreamingRefusal.choices[0]!.message.refusal = "I cannot help with that.";
@@ -232,4 +281,50 @@ describe("OpenAIChatCompletionClient", () => {
232281
expect(calls[0]?.id).toBe("openai:response-replayed:choice:0:tool:0");
233282
expect(response.text).toBe("done");
234283
});
284+
285+
it("does not add an empty OpenAI assistant message when a streaming tool turn is replayed", async () => {
286+
const replayed = [
287+
chunk("response-replayed", {
288+
role: "assistant",
289+
tool_calls: [{ index: 0, id: "call-1", type: "function", function: { name: "side_", arguments: "{" } }],
290+
}),
291+
chunk(
292+
"response-replayed",
293+
{ tool_calls: [{ index: 0, function: { name: "effect", arguments: "}" } }] },
294+
"tool_calls",
295+
),
296+
];
297+
const { client, create } = mockOpenAI([
298+
replayed,
299+
replayed,
300+
[chunk("response-final", { role: "assistant", content: "done" }, "stop")],
301+
]);
302+
const execute = vi.fn(() => "complete");
303+
const sideEffect = defineTool({
304+
name: "side_effect",
305+
parameters: { type: "object", additionalProperties: false },
306+
execute,
307+
});
308+
const chatClient = new OpenAIChatCompletionClient({ client, model: "gpt-test" });
309+
const response = await chatClient
310+
.getResponse("request", { stream: true, options: { tools: [sideEffect] } })
311+
.getFinalResponse();
312+
const finalRequest = create.mock.calls[2]?.[0] as ChatCompletionCreateParams;
313+
314+
expect(execute).toHaveBeenCalledTimes(1);
315+
expect(create).toHaveBeenCalledTimes(3);
316+
expect(finalRequest.tool_choice).toBe("none");
317+
expect(finalRequest.messages).toEqual(create.mock.calls[1]?.[0].messages);
318+
expect(finalRequest.messages).toEqual([
319+
{ role: "user", content: "request" },
320+
{
321+
role: "assistant",
322+
content: null,
323+
tool_calls: [{ id: "call-1", type: "function", function: { name: "side_effect", arguments: "{}" } }],
324+
},
325+
{ role: "tool", tool_call_id: "call-1", content: "complete" },
326+
]);
327+
expect(response.messages.map((message) => message.role)).toEqual(["assistant", "tool", "assistant"]);
328+
expect(response.text).toBe("done");
329+
});
235330
});

0 commit comments

Comments
 (0)