@@ -260,6 +260,17 @@ export async function generateQuestion(type: string, category: string, difficult
260260 The response MUST be directly parseable as JSON without any cleanup needed.
261261 Ensure all special characters in strings are properly escaped according to JSON standards.
262262
263+ When creating coding questions with test cases, format the input as a plain string rather than a JSON object.
264+ For example, use 'a = "helloworld", b = "world"' rather than {a: "helloworld", b: "world"}.
265+
266+ IMPORTANT: If a test case output is a string, you MUST enclose it in additional double quotes and escape them properly in JSON.
267+ For example:
268+ - For string output "hello": { "input": "some input", "output": "\"hello\"" }
269+ - For empty string: { "input": "some input", "output": "\"\"" }
270+ - For numbers: { "input": "some input", "output": "42" } (no extra quotes for numbers)
271+
272+ DO NOT leave string outputs without proper double quotes. Only add the extra quotes for string outputs, not for numbers or other types.
273+
263274 Return your response in JSON format exactly matching the structure provided, with no additional text.` ;
264275
265276 const prompt = `
@@ -287,19 +298,37 @@ export async function generateQuestion(type: string, category: string, difficult
287298 }
288299 }${ type === 'Coding' ? `,
289300 "testCases": [
290- { "input": "example input 1", "output": "expected output 1" },
291- { "input": "example input 2", "output": "expected output 2" }
301+ { "input": "a = \"helloworld\", b = \"world\"", "output": "\"world\"" },
302+ {"input": "a = \"programming\", b = \"prog\"", "output": "\"prog\"" },
303+ { "input": "x = 5, y = 10", "output": "50" },
304+ { "input": "str = \"\"", "output": "\"\"" },
305+ { "input": "", "output": "\"\"" }
292306 ]` : '' }
293307 }
294308
295309 Make sure the question is appropriate for the difficulty level and incorporates concepts from all the specified categories.
296310 If multiple categories are provided, create a question that combines elements from these categories.
311+
312+ IMPORTANT: For test cases where the output is a string, ALWAYS enclose the output in additional double quotes:
313+ - For string output: "output": "\"hello\""
314+ - For empty string: "output": "\"\""
315+ - For number output: "output": "42" (no extra quotes for numbers)
316+
317+ Pay careful attention to the data type of the expected output and format it accordingly.
297318 `
298319
299320 try {
300321 const result = await callOpenAI ( prompt , systemPrompt , ( _ ) => { } , "question" )
301-
302- return JSON . parse ( jsonrepair ( result ) )
322+
323+ // Preprocess the JSON to handle incorrectly formatted empty strings
324+ const preprocessedResult = result
325+ // Replace four consecutive double quotes """" with the properly escaped empty string "\"\""
326+ . replace ( / " " " " / g, "\"\\\"\\\"\"" )
327+ // Replace two consecutive double quotes "" with the properly escaped empty string "\"\""
328+ // But avoid replacing already properly escaped empty strings "\"\""
329+ . replace ( / (?< ! \\ ) " " / g, "\"\\\"\\\"\"" ) ;
330+
331+ return JSON . parse ( jsonrepair ( preprocessedResult ) )
303332 } catch ( error ) {
304333 console . error ( "Error generating question:" , error )
305334 throw new Error ( "Failed to generate question. Please check your API settings and try again." )
0 commit comments