Skip to content

Commit 343073f

Browse files
committed
fix: normalize tool parameter schema to avoid 400 error
Normalize tool parameter schema so that any object-type tool input has a properties field. This avoids the Copilot 400 error: "object schema missing properties". PR: ericc-ch#192
1 parent 9499342 commit 343073f

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,26 @@ function translateAnthropicToolsToOpenAI(
267267
function: {
268268
name: tool.name,
269269
description: tool.description,
270-
parameters: tool.input_schema,
270+
parameters: normalizeToolParameters(tool.input_schema),
271271
},
272272
}))
273273
}
274274

275+
function normalizeToolParameters(
276+
inputSchema: unknown,
277+
): Record<string, unknown> {
278+
if (!inputSchema || typeof inputSchema !== "object") {
279+
return { type: "object", properties: {} }
280+
}
281+
282+
const schema = { ...inputSchema } as Record<string, unknown>
283+
if (schema.type === "object" && schema.properties === undefined) {
284+
return { ...schema, properties: {} }
285+
}
286+
287+
return schema
288+
}
289+
275290
function translateAnthropicToolChoiceToOpenAI(
276291
anthropicToolChoice: AnthropicMessagesPayload["tool_choice"],
277292
): ChatCompletionsPayload["tool_choice"] {

0 commit comments

Comments
 (0)