Skip to content

Commit 075ed78

Browse files
committed
Fix multi choices in OpenAI response with different types of content(some with text content, some with tool calls), the translation only picked up the text content from the first choice and missing the tool calls from the seconed choice. The code was written by Copilot.
1 parent 941c190 commit 075ed78

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/routes/messages/non-stream-translation.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,34 @@ function translateAnthropicToolChoiceToOpenAI(
271271
export function translateToAnthropic(
272272
response: ChatCompletionResponse,
273273
): AnthropicResponse {
274-
const choice = response.choices[0]
275-
const textBlocks = getAnthropicTextBlocks(choice.message.content)
276-
const toolUseBlocks = getAnthropicToolUseBlocks(choice.message.tool_calls)
274+
// Merge content from all choices
275+
let allTextBlocks: Array<AnthropicTextBlock> = []
276+
let allToolUseBlocks: Array<AnthropicToolUseBlock> = []
277+
let stopReason: "stop" | "length" | "tool_calls" | "content_filter" | null = "stop" // default
278+
279+
// Process all choices to extract text and tool use blocks
280+
for (const choice of response.choices) {
281+
const textBlocks = getAnthropicTextBlocks(choice.message.content)
282+
const toolUseBlocks = getAnthropicToolUseBlocks(choice.message.tool_calls)
283+
284+
allTextBlocks.push(...textBlocks)
285+
allToolUseBlocks.push(...toolUseBlocks)
286+
287+
// Use the finish_reason from the first choice, or prioritize tool_calls
288+
if (choice.finish_reason === "tool_calls" || stopReason === "stop") {
289+
stopReason = choice.finish_reason
290+
}
291+
}
292+
277293
// Note: GitHub Copilot doesn't generate thinking blocks, so we don't include them in responses
278294

279295
return {
280296
id: response.id,
281297
type: "message",
282298
role: "assistant",
283299
model: response.model,
284-
content: [...textBlocks, ...toolUseBlocks],
285-
stop_reason: mapOpenAIStopReasonToAnthropic(choice.finish_reason),
300+
content: [...allTextBlocks, ...allToolUseBlocks],
301+
stop_reason: mapOpenAIStopReasonToAnthropic(stopReason),
286302
stop_sequence: null,
287303
usage: {
288304
input_tokens: response.usage?.prompt_tokens ?? 0,

0 commit comments

Comments
 (0)