Skip to content

Commit 2ee380e

Browse files
committed
fix: o3 tools with array parameters
Apparently the o3 model things items is a required property for array types in JSON schema. This is not the case, but we fixed it anyway.
1 parent 7e82c94 commit 2ee380e

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/backend/src/modules/puterai/lib/FunctionCalling.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ module.exports = class FunctionCalling {
2020

2121
const normalize_function = fn => {
2222
const normal_fn = {};
23-
const parameters =
23+
let parameters =
2424
fn.parameters ||
2525
fn.input_schema;
2626

2727
normal_fn.parameters = parameters ?? {
2828
type: 'object',
2929
};
3030

31+
if ( parameters.properties ) {
32+
parameters = this.normalize_json_schema(parameters);
33+
}
34+
3135
if ( fn.name ) {
3236
normal_fn.name = fn.name;
3337
}
@@ -61,6 +65,31 @@ module.exports = class FunctionCalling {
6165
return tools;
6266
}
6367

68+
static normalize_json_schema (schema) {
69+
if ( ! schema ) return schema;
70+
71+
if ( schema.type === 'object' ) {
72+
if ( ! schema.properties ) {
73+
return schema;
74+
}
75+
76+
const keys = Object.keys(schema.properties);
77+
for ( const key of keys ) {
78+
schema.properties[key] = this.normalize_json_schema(schema.properties[key]);
79+
}
80+
}
81+
82+
if ( schema.type === 'array' ) {
83+
if ( ! schema.items ) {
84+
schema.items = {};
85+
} else {
86+
schema.items = this.normalize_json_schema(schema.items);
87+
}
88+
}
89+
90+
return schema;
91+
}
92+
6493
/**
6594
* This function will convert a normalized tools object to the
6695
* format expected by OpenAI.

0 commit comments

Comments
 (0)