Skip to content

Commit 3189c93

Browse files
committed
fix(console): drop unsupported tool types when converting to chat completions
1 parent f33cdbc commit 3189c93

2 files changed

Lines changed: 57 additions & 8 deletions

File tree

‎packages/console/app/src/routes/zen/util/provider/openai-compatible.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,21 @@ export function toOaCompatibleRequest(body: CommonRequest) {
195195
}
196196
}
197197

198+
// Chat-completions upstreams only accept function tools. Responses-API
199+
// requests carry extra tool types (e.g. web_search) that have no chat
200+
// equivalent; mapping them into a function tool yields an undefined name
201+
// and a 400 from the upstream, so drop them instead.
198202
const tools = Array.isArray(body.tools)
199-
? body.tools.map((tool: any) => ({
200-
type: "function",
201-
function: {
202-
name: tool.name,
203-
description: tool.description,
204-
parameters: tool.parameters,
205-
},
206-
}))
203+
? body.tools
204+
.filter((tool: any) => tool && typeof tool === "object" && tool.type === "function")
205+
.map((tool: any) => ({
206+
type: "function",
207+
function: {
208+
name: tool.function?.name,
209+
description: tool.function?.description,
210+
parameters: tool.function?.parameters,
211+
},
212+
}))
207213
: undefined
208214

209215
return {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { fromOpenaiRequest } from "../src/routes/zen/util/provider/openai"
3+
import {
4+
fromOaCompatibleRequest,
5+
toOaCompatibleRequest,
6+
} from "../src/routes/zen/util/provider/openai-compatible"
7+
8+
describe("toOaCompatibleRequest tool conversion", () => {
9+
test("drops non-function tools like web_search and keeps function tools", () => {
10+
const common = fromOpenaiRequest({
11+
model: "deepseek-v4-flash-free",
12+
input: [{ role: "user", content: "hi" }],
13+
tools: [
14+
{ type: "function", function: { name: "get_weather", description: "x", parameters: { type: "object" } } },
15+
{ type: "web_search", external_web_access: false },
16+
],
17+
})
18+
19+
expect(toOaCompatibleRequest(common).tools).toEqual([
20+
{
21+
type: "function",
22+
function: { name: "get_weather", description: "x", parameters: { type: "object" } },
23+
},
24+
])
25+
})
26+
27+
test("passes through chat-completions function tools unchanged", () => {
28+
const common = fromOaCompatibleRequest({
29+
model: "m",
30+
messages: [{ role: "user", content: "hi" }],
31+
tools: [{ type: "function", function: { name: "get_weather", parameters: { type: "object" } } }],
32+
})
33+
34+
expect(toOaCompatibleRequest(common).tools).toEqual([
35+
{ type: "function", function: { name: "get_weather", parameters: { type: "object" } } },
36+
])
37+
})
38+
39+
test("omits tools when the request has none", () => {
40+
const common = fromOpenaiRequest({ model: "m", input: [{ role: "user", content: "hi" }] })
41+
expect(toOaCompatibleRequest(common).tools).toBeUndefined()
42+
})
43+
})

0 commit comments

Comments
 (0)