@@ -188,6 +188,137 @@ describe("convertToParsedChatCompletionResponse", () => {
188188 convertToParsedChatCompletionResponse ( raw_response , MathDemonstration )
189189 ) . toStrictEqual ( ccr_response ) ;
190190 } ) ;
191+
192+ it ( "should set parsed to undefined when content is malformed JSON" , ( ) => {
193+ const truncatedResponse : components . ChatCompletionResponse = {
194+ id : "test-malformed" ,
195+ object : "chat.completion" ,
196+ model : "mistral-tiny-latest" ,
197+ usage : { promptTokens : 10 , completionTokens : 5 , totalTokens : 15 } ,
198+ created : 1700000000 ,
199+ choices : [
200+ {
201+ index : 0 ,
202+ message : {
203+ content : '{"steps": [{"explanation": "truncated' ,
204+ toolCalls : null ,
205+ prefix : false ,
206+ role : "assistant" ,
207+ } ,
208+ finishReason : "length" ,
209+ } ,
210+ ] ,
211+ } ;
212+
213+ const result = convertToParsedChatCompletionResponse (
214+ truncatedResponse ,
215+ MathDemonstration ,
216+ ) ;
217+
218+ expect ( result . choices ) . toHaveLength ( 1 ) ;
219+ expect ( result . choices ! [ 0 ] . message ) . toBeDefined ( ) ;
220+ expect ( result . choices ! [ 0 ] . message ! . parsed ) . toBeUndefined ( ) ;
221+ expect ( result . choices ! [ 0 ] . message ! . content ) . toBe (
222+ '{"steps": [{"explanation": "truncated' ,
223+ ) ;
224+ } ) ;
225+
226+ it ( "should set parsed to undefined when content is valid JSON but fails schema validation" , ( ) => {
227+ const wrongShapeResponse : components . ChatCompletionResponse = {
228+ id : "test-wrong-shape" ,
229+ object : "chat.completion" ,
230+ model : "mistral-tiny-latest" ,
231+ usage : { promptTokens : 10 , completionTokens : 5 , totalTokens : 15 } ,
232+ created : 1700000000 ,
233+ choices : [
234+ {
235+ index : 0 ,
236+ message : {
237+ content : '{"wrong_field": "not matching schema"}' ,
238+ toolCalls : null ,
239+ prefix : false ,
240+ role : "assistant" ,
241+ } ,
242+ finishReason : "stop" ,
243+ } ,
244+ ] ,
245+ } ;
246+
247+ const result = convertToParsedChatCompletionResponse (
248+ wrongShapeResponse ,
249+ MathDemonstration ,
250+ ) ;
251+
252+ expect ( result . choices ) . toHaveLength ( 1 ) ;
253+ expect ( result . choices ! [ 0 ] . message ) . toBeDefined ( ) ;
254+ expect ( result . choices ! [ 0 ] . message ! . parsed ) . toBeUndefined ( ) ;
255+ } ) ;
256+
257+ it ( "should preserve choices when content is null" , ( ) => {
258+ const nullContentResponse : components . ChatCompletionResponse = {
259+ id : "test-null-content" ,
260+ object : "chat.completion" ,
261+ model : "mistral-tiny-latest" ,
262+ usage : { promptTokens : 10 , completionTokens : 5 , totalTokens : 15 } ,
263+ created : 1700000000 ,
264+ choices : [
265+ {
266+ index : 0 ,
267+ message : {
268+ content : null ,
269+ toolCalls : null ,
270+ prefix : false ,
271+ role : "assistant" ,
272+ } ,
273+ finishReason : "stop" ,
274+ } ,
275+ ] ,
276+ } ;
277+
278+ const result = convertToParsedChatCompletionResponse (
279+ nullContentResponse ,
280+ MathDemonstration ,
281+ ) ;
282+
283+ expect ( result . choices ) . toHaveLength ( 1 ) ;
284+ expect ( result . choices ! [ 0 ] . message ) . toBeDefined ( ) ;
285+ expect ( result . choices ! [ 0 ] . message ! . parsed ) . toBeUndefined ( ) ;
286+ } ) ;
287+
288+ it ( "should return empty choices array for empty choices" , ( ) => {
289+ const emptyChoicesResponse : components . ChatCompletionResponse = {
290+ id : "test-empty" ,
291+ object : "chat.completion" ,
292+ model : "mistral-tiny-latest" ,
293+ usage : { promptTokens : 10 , completionTokens : 0 , totalTokens : 10 } ,
294+ created : 1700000000 ,
295+ choices : [ ] ,
296+ } ;
297+
298+ const result = convertToParsedChatCompletionResponse (
299+ emptyChoicesResponse ,
300+ MathDemonstration ,
301+ ) ;
302+
303+ expect ( result . choices ) . toEqual ( [ ] ) ;
304+ } ) ;
305+
306+ it ( "should return undefined choices when choices is undefined" , ( ) => {
307+ const noChoicesResponse : components . ChatCompletionResponse = {
308+ id : "test-undefined" ,
309+ object : "chat.completion" ,
310+ model : "mistral-tiny-latest" ,
311+ usage : { promptTokens : 10 , completionTokens : 0 , totalTokens : 10 } ,
312+ created : 1700000000 ,
313+ } ;
314+
315+ const result = convertToParsedChatCompletionResponse (
316+ noChoicesResponse ,
317+ MathDemonstration ,
318+ ) ;
319+
320+ expect ( result . choices ) . toBeUndefined ( ) ;
321+ } ) ;
191322} ) ;
192323
193324describe ( "responseFormatFromZodObject" , ( ) => {
0 commit comments