Skip to content

Commit 015c374

Browse files
authored
Merge pull request #1114 from microsoft/roblou/coastal-gayal
Don't send empty assistant messages
2 parents 0cb2680 + eacbbf3 commit 015c374

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/platform/endpoint/node/responsesApi.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import { ILogService } from '../../log/common/logService';
2020
import { FinishedCallback, IResponseDelta, OpenAiResponsesFunctionTool } from '../../networking/common/fetch';
2121
import { ICreateEndpointBodyOptions, IEndpointBody } from '../../networking/common/networking';
2222
import { ChatCompletion, FinishedCompletionReason, TokenLogProb } from '../../networking/common/openai';
23+
import { IExperimentationService } from '../../telemetry/common/nullExperimentationService';
2324
import { ITelemetryService } from '../../telemetry/common/telemetry';
2425
import { TelemetryData } from '../../telemetry/common/telemetryData';
2526
import { IChatModelInformation } from '../common/endpointProvider';
2627
import { getStatefulMarkerAndIndex } from '../common/statefulMarkerContainer';
2728
import { rawPartAsThinkingData } from '../common/thinkingDataContainer';
28-
import { IExperimentationService } from '../../telemetry/common/nullExperimentationService';
2929

3030
export function createResponsesRequestBody(accessor: ServicesAccessor, options: ICreateEndpointBodyOptions, model: string, modelInfo: IChatModelInformation): IEndpointBody {
3131
const configService = accessor.get(IConfigurationService);
@@ -84,14 +84,17 @@ function rawMessagesToResponseAPI(modelId: string, messages: readonly Raw.ChatMe
8484
case Raw.ChatRole.Assistant:
8585
if (message.content.length) {
8686
input.push(...extractThinkingData(message.content));
87-
input.push({
88-
role: 'assistant',
89-
content: message.content.map(rawContentToResponsesOutputContent).filter(isDefined),
90-
// I don't think this needs to be round-tripped.
91-
id: 'msg_123',
92-
status: 'completed',
93-
type: 'message',
94-
} satisfies OpenAI.Responses.ResponseOutputMessage);
87+
const asstContent = message.content.map(rawContentToResponsesOutputContent).filter(isDefined);
88+
if (asstContent.length) {
89+
input.push({
90+
role: 'assistant',
91+
content: asstContent,
92+
// I don't think this needs to be round-tripped.
93+
id: 'msg_123',
94+
status: 'completed',
95+
type: 'message',
96+
} satisfies OpenAI.Responses.ResponseOutputMessage);
97+
}
9598
}
9699
if (message.toolCalls) {
97100
for (const toolCall of message.toolCalls) {
@@ -150,7 +153,9 @@ function rawContentToResponsesContent(part: Raw.ChatCompletionContentPart): Open
150153
function rawContentToResponsesOutputContent(part: Raw.ChatCompletionContentPart): OpenAI.Responses.ResponseOutputText | OpenAI.Responses.ResponseOutputRefusal | undefined {
151154
switch (part.type) {
152155
case Raw.ChatCompletionContentPartKind.Text:
153-
return { type: 'output_text', text: part.text, annotations: [] };
156+
if (part.text.trim()) {
157+
return { type: 'output_text', text: part.text, annotations: [] };
158+
}
154159
}
155160
}
156161

0 commit comments

Comments
 (0)