@@ -1557,6 +1557,53 @@ describe('OpenAIProvider', () => {
15571557 expect ( sentBody . max_tokens ) . toBeUndefined ( ) ;
15581558 } ) ;
15591559
1560+ describe ( 'unsupported max_tokens fallback' , ( ) => {
1561+ const unsupportedMaxTokens = ( ) => new Response ( JSON . stringify ( {
1562+ error : {
1563+ message : "Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead." ,
1564+ type : 'invalid_request_error' ,
1565+ param : 'max_tokens' ,
1566+ code : 'unsupported_parameter' ,
1567+ } ,
1568+ } ) , { status : 400 , headers : { 'Content-Type' : 'application/json' } } ) ;
1569+
1570+ it ( 'resends once with max_completion_tokens when an unrecognized model rejects max_tokens' , async ( ) => {
1571+ const fetchSpy = vi . spyOn ( globalThis , 'fetch' )
1572+ . mockResolvedValueOnce ( unsupportedMaxTokens ( ) )
1573+ . mockResolvedValueOnce ( new Response ( JSON . stringify ( {
1574+ id : 'resp-ft' , created : 1 ,
1575+ choices : [ { message : { role : 'assistant' , content : 'OK' } , finish_reason : 'stop' } ] ,
1576+ } ) , { status : 200 , headers : { 'Content-Type' : 'application/json' } } ) ) ;
1577+
1578+ const result = await provider . complete ( {
1579+ model : 'ft:gpt-6.1-sol:acme::abc123' ,
1580+ maxTokens : 4096 ,
1581+ messages : [ { role : 'user' , content : 'hi' } ] ,
1582+ } ) ;
1583+
1584+ expect ( result . content ) . toBe ( 'OK' ) ;
1585+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 2 ) ;
1586+ const firstBody = JSON . parse ( fetchSpy . mock . calls [ 0 ] [ 1 ] ?. body as string ) ;
1587+ const retryBody = JSON . parse ( fetchSpy . mock . calls [ 1 ] [ 1 ] ?. body as string ) ;
1588+ expect ( firstBody . max_tokens ) . toBe ( 4096 ) ;
1589+ expect ( retryBody . max_completion_tokens ) . toBe ( 4096 ) ;
1590+ expect ( retryBody ) . not . toHaveProperty ( 'max_tokens' ) ;
1591+ } ) ;
1592+
1593+ it ( 'does not retry other 400 errors' , async ( ) => {
1594+ const fetchSpy = vi . spyOn ( globalThis , 'fetch' ) . mockResolvedValueOnce ( new Response ( JSON . stringify ( {
1595+ error : { message : "Unsupported parameter: 'temperature' is not supported with this model." } ,
1596+ } ) , { status : 400 , headers : { 'Content-Type' : 'application/json' } } ) ) ;
1597+
1598+ await expect ( provider . complete ( {
1599+ model : 'ft:gpt-6.1-sol:acme::abc123' ,
1600+ maxTokens : 4096 ,
1601+ messages : [ { role : 'user' , content : 'hi' } ] ,
1602+ } ) ) . rejects . toBeInstanceOf ( ApiError ) ;
1603+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 1 ) ;
1604+ } ) ;
1605+ } ) ;
1606+
15601607 describe ( 'Chat Completions finish reason normalization' , ( ) => {
15611608 const mockJson = ( body : unknown ) => vi . spyOn ( globalThis , 'fetch' ) . mockResolvedValueOnce (
15621609 new Response ( JSON . stringify ( body ) , { status : 200 , headers : { 'Content-Type' : 'application/json' } } ) ,
0 commit comments