Skip to content

Commit 35d1467

Browse files
add try catch to json parse
1 parent 3523ba0 commit 35d1467

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

components/openai/actions/chat-using-file-search/chat-using-file-search.mjs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,16 @@ export default {
202202
}
203203

204204
if (this.responseFormat === constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) {
205-
data.text = {
206-
format: {
207-
type: this.responseFormat,
208-
...JSON.parse(this.jsonSchema),
209-
},
210-
};
205+
try {
206+
data.text = {
207+
format: {
208+
type: this.responseFormat,
209+
...JSON.parse(this.jsonSchema),
210+
},
211+
};
212+
} catch (error) {
213+
throw new Error("Invalid JSON format in the provided JSON Schema");
214+
}
211215
}
212216

213217
const response = await this.openai.responses({

components/openai/actions/chat-using-functions/chat-using-functions.mjs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ export default {
189189

190190
let functions = this.functions;
191191
if (typeof functions === "string") {
192-
functions = JSON.parse(functions);
192+
try {
193+
functions = JSON.parse(functions);
194+
} catch (error) {
195+
throw new Error("Invalid JSON format in the provided Functions Schema");
196+
}
193197
}
194198

195199
if (Array.isArray(functions)) {
@@ -224,12 +228,16 @@ export default {
224228
}
225229

226230
if (this.responseFormat === constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) {
227-
data.text = {
228-
format: {
229-
type: this.responseFormat,
230-
...JSON.parse(this.jsonSchema),
231-
},
232-
};
231+
try {
232+
data.text = {
233+
format: {
234+
type: this.responseFormat,
235+
...JSON.parse(this.jsonSchema),
236+
},
237+
};
238+
} catch (error) {
239+
throw new Error("Invalid JSON format in the provided JSON Schema");
240+
}
233241
}
234242

235243
const response = await this.openai.responses({

components/openai/actions/chat-using-web-search/chat-using-web-search.mjs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,16 @@ export default {
173173
};
174174

175175
if (this.responseFormat === constants.CHAT_RESPONSE_FORMAT.JSON_SCHEMA.value) {
176-
data.text = {
177-
format: {
178-
type: this.responseFormat,
179-
...JSON.parse(this.jsonSchema),
180-
},
181-
};
176+
try {
177+
data.text = {
178+
format: {
179+
type: this.responseFormat,
180+
...JSON.parse(this.jsonSchema),
181+
},
182+
};
183+
} catch (error) {
184+
throw new Error("Invalid JSON format in the provided JSON Schema");
185+
}
182186
}
183187

184188
const response = await this.openai.responses({

0 commit comments

Comments
 (0)