Skip to content

Commit 7521150

Browse files
committed
fix(cerebras): ensure all tools have consistent strict mode values
Cerebras API requires all tools to have the same strict mode setting. The base provider sets strict: true for regular tools and strict: false for MCP tools, causing a 422 error when both types are present. This fix overrides convertToolsForOpenAI in CerebrasHandler to set all tools to strict: false, ensuring consistency with Cerebras API requirements. Fixes #10334
1 parent 108f78d commit 7521150

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/api/providers/__tests__/cerebras.spec.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,77 @@ describe("CerebrasHandler", () => {
173173
// Test fallback token estimation logic
174174
})
175175
})
176+
177+
describe("convertToolsForOpenAI", () => {
178+
it("should set all tools to strict: false for Cerebras API consistency", () => {
179+
// Access the protected method through a test subclass
180+
const regularTool = {
181+
type: "function",
182+
function: {
183+
name: "read_file",
184+
parameters: {
185+
type: "object",
186+
properties: {
187+
path: { type: "string" },
188+
},
189+
required: ["path"],
190+
},
191+
},
192+
}
193+
194+
// MCP tool with the 'mcp--' prefix
195+
const mcpTool = {
196+
type: "function",
197+
function: {
198+
name: "mcp--server--tool",
199+
parameters: {
200+
type: "object",
201+
properties: {
202+
arg: { type: "string" },
203+
},
204+
},
205+
},
206+
}
207+
208+
// Create a test wrapper to access protected method
209+
class TestCerebrasHandler extends CerebrasHandler {
210+
public testConvertToolsForOpenAI(tools: any[]) {
211+
return this.convertToolsForOpenAI(tools)
212+
}
213+
}
214+
215+
const testHandler = new TestCerebrasHandler({ cerebrasApiKey: "test" })
216+
const converted = testHandler.testConvertToolsForOpenAI([regularTool, mcpTool])
217+
218+
// Both tools should have strict: false
219+
expect(converted).toHaveLength(2)
220+
expect(converted![0].function.strict).toBe(false)
221+
expect(converted![1].function.strict).toBe(false)
222+
})
223+
224+
it("should return undefined when tools is undefined", () => {
225+
class TestCerebrasHandler extends CerebrasHandler {
226+
public testConvertToolsForOpenAI(tools: any[] | undefined) {
227+
return this.convertToolsForOpenAI(tools)
228+
}
229+
}
230+
231+
const testHandler = new TestCerebrasHandler({ cerebrasApiKey: "test" })
232+
expect(testHandler.testConvertToolsForOpenAI(undefined)).toBeUndefined()
233+
})
234+
235+
it("should pass through non-function tools unchanged", () => {
236+
class TestCerebrasHandler extends CerebrasHandler {
237+
public testConvertToolsForOpenAI(tools: any[]) {
238+
return this.convertToolsForOpenAI(tools)
239+
}
240+
}
241+
242+
const nonFunctionTool = { type: "other", data: "test" }
243+
const testHandler = new TestCerebrasHandler({ cerebrasApiKey: "test" })
244+
const converted = testHandler.testConvertToolsForOpenAI([nonFunctionTool])
245+
246+
expect(converted![0]).toEqual(nonFunctionTool)
247+
})
248+
})
176249
})

src/api/providers/cerebras.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ export class CerebrasHandler extends BaseProvider implements SingleCompletionHan
9191
return result
9292
}
9393

94+
/**
95+
* Override convertToolsForOpenAI to ensure all tools have consistent strict values.
96+
* Cerebras API requires all tools to have the same strict mode setting.
97+
* We use strict: false for all tools since MCP tools cannot use strict mode
98+
* (they have optional parameters from the MCP server schema).
99+
*/
100+
protected override convertToolsForOpenAI(tools: any[] | undefined): any[] | undefined {
101+
if (!tools) {
102+
return undefined
103+
}
104+
105+
return tools.map((tool) => {
106+
if (tool.type !== "function") {
107+
return tool
108+
}
109+
110+
return {
111+
...tool,
112+
function: {
113+
...tool.function,
114+
strict: false,
115+
parameters: this.convertToolSchemaForOpenAI(tool.function.parameters),
116+
},
117+
}
118+
})
119+
}
120+
94121
async *createMessage(
95122
systemPrompt: string,
96123
messages: Anthropic.Messages.MessageParam[],

0 commit comments

Comments
 (0)