@@ -518,6 +518,145 @@ describe("MimoHandler", () => {
518518 expect ( textChunks [ 0 ] . text ) . toBe ( "Retried" )
519519 } )
520520
521+ it ( "should retry without the strict flag when the endpoint rejects strict tool schemas" , async ( ) => {
522+ // First call rejects with a 400 error naming the strict field
523+ const rejectionError = Object . assign ( new Error ( "400 - Unknown parameter: tools[0].function.strict" ) , {
524+ status : 400 ,
525+ } )
526+ mockCreate . mockRejectedValueOnce ( rejectionError )
527+
528+ // Second call (retry) succeeds
529+ mockCreate . mockImplementationOnce ( async ( ) => ( {
530+ [ Symbol . asyncIterator ] : async function * ( ) {
531+ yield {
532+ choices : [ { delta : { content : "Retried" } , index : 0 } ] ,
533+ usage : null ,
534+ }
535+ yield {
536+ choices : [ { delta : { } , index : 0 , finish_reason : "stop" } ] ,
537+ usage : { prompt_tokens : 1 , completion_tokens : 1 , total_tokens : 2 } ,
538+ }
539+ } ,
540+ } ) )
541+
542+ const tools : OpenAI . Chat . ChatCompletionTool [ ] = [
543+ {
544+ type : "function" ,
545+ function : {
546+ name : "read_file" ,
547+ description : "Read a file" ,
548+ parameters : {
549+ type : "object" ,
550+ properties : { path : { type : "string" } } ,
551+ required : [ "path" ] ,
552+ } ,
553+ } ,
554+ } ,
555+ ]
556+
557+ const messages : Anthropic . Messages . MessageParam [ ] = [
558+ { role : "user" , content : [ { type : "text" , text : "Hello" } ] } ,
559+ ]
560+
561+ const chunks : ApiStreamChunk [ ] = [ ]
562+ const stream = handler . createMessage ( "System prompt" , messages , { taskId : "test-task" , tools } )
563+ for await ( const chunk of stream ) {
564+ chunks . push ( chunk )
565+ }
566+
567+ expect ( mockCreate ) . toHaveBeenCalledTimes ( 2 )
568+
569+ // First call sent tools with the strict flag applied
570+ const firstCallParams = mockCreate . mock . calls [ 0 ] [ 0 ]
571+ expect ( firstCallParams . tools [ 0 ] . function ) . toHaveProperty ( "strict" )
572+
573+ // Retry stripped the strict flag but kept the original schema
574+ const retryCallParams = mockCreate . mock . calls [ 1 ] [ 0 ]
575+ expect ( retryCallParams . tools ) . toHaveLength ( 1 )
576+ expect ( retryCallParams . tools [ 0 ] . function . name ) . toBe ( "read_file" )
577+ expect ( retryCallParams . tools [ 0 ] . function ) . not . toHaveProperty ( "strict" )
578+ expect ( retryCallParams . tools [ 0 ] . function . parameters ) . toEqual ( {
579+ type : "object" ,
580+ properties : { path : { type : "string" } } ,
581+ required : [ "path" ] ,
582+ } )
583+
584+ const textChunks = chunks . filter ( ( c ) => c . type === "text" )
585+ expect ( textChunks [ 0 ] . text ) . toBe ( "Retried" )
586+ } )
587+
588+ it ( "should retry without the strict flag when the endpoint rejects hardened schema fields" , async ( ) => {
589+ // 400 naming additionalProperties in a tools context
590+ const rejectionError = Object . assign (
591+ new Error ( "400 - Invalid tools: additionalProperties is not a supported field" ) ,
592+ { status : 400 } ,
593+ )
594+ mockCreate . mockRejectedValueOnce ( rejectionError )
595+
596+ mockCreate . mockImplementationOnce ( async ( ) => ( {
597+ [ Symbol . asyncIterator ] : async function * ( ) {
598+ yield {
599+ choices : [ { delta : { content : "Retried" } , index : 0 } ] ,
600+ usage : null ,
601+ }
602+ yield {
603+ choices : [ { delta : { } , index : 0 , finish_reason : "stop" } ] ,
604+ usage : { prompt_tokens : 1 , completion_tokens : 1 , total_tokens : 2 } ,
605+ }
606+ } ,
607+ } ) )
608+
609+ const tools : OpenAI . Chat . ChatCompletionTool [ ] = [
610+ {
611+ type : "function" ,
612+ function : { name : "read_file" , description : "Read" , parameters : { } } ,
613+ } ,
614+ ]
615+
616+ const messages : Anthropic . Messages . MessageParam [ ] = [
617+ { role : "user" , content : [ { type : "text" , text : "Hello" } ] } ,
618+ ]
619+
620+ const stream = handler . createMessage ( "System prompt" , messages , { taskId : "test-task" , tools } )
621+ for await ( const _chunk of stream ) {
622+ // drain
623+ }
624+
625+ expect ( mockCreate ) . toHaveBeenCalledTimes ( 2 )
626+ const retryCallParams = mockCreate . mock . calls [ 1 ] [ 0 ]
627+ expect ( retryCallParams . tools [ 0 ] . function ) . not . toHaveProperty ( "strict" )
628+ } )
629+
630+ it ( "should not retry schema-unrelated 400 errors" , async ( ) => {
631+ // A 400 about reasoning_content (not tool schemas) must NOT trigger
632+ // the strict-schema fallback.
633+ const rejectionError = Object . assign (
634+ new Error ( "400 - reasoning_content is required in multi-turn tool call conversations" ) ,
635+ { status : 400 } ,
636+ )
637+ mockCreate . mockRejectedValueOnce ( rejectionError )
638+
639+ const tools : OpenAI . Chat . ChatCompletionTool [ ] = [
640+ {
641+ type : "function" ,
642+ function : { name : "read_file" , description : "Read" , parameters : { } } ,
643+ } ,
644+ ]
645+
646+ const messages : Anthropic . Messages . MessageParam [ ] = [
647+ { role : "user" , content : [ { type : "text" , text : "Hello" } ] } ,
648+ ]
649+
650+ await expect ( async ( ) => {
651+ const stream = handler . createMessage ( "System prompt" , messages , { taskId : "test-task" , tools } )
652+ for await ( const _chunk of stream ) {
653+ // drain
654+ }
655+ } ) . rejects . toThrow ( )
656+
657+ expect ( mockCreate ) . toHaveBeenCalledTimes ( 1 )
658+ } )
659+
521660 it ( "should send stream_options with include_usage" , async ( ) => {
522661 const messages : Anthropic . Messages . MessageParam [ ] = [
523662 { role : "user" , content : [ { type : "text" , text : "Hello" } ] } ,
0 commit comments