File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed 
packages/@n8n/nodes-langchain/nodes/chains/ChainLLM Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,18 @@ export function isModelWithResponseFormat(
3838	) ; 
3939} 
4040
41+ export  function  isModelInThinkingMode ( 
42+ 	llm : BaseLanguageModel , 
43+ ) : llm  is BaseLanguageModel  &  {  lc_kwargs : {  invocationKwargs : {  thinking : {  type : string  }  }  }  }  { 
44+ 	return  ( 
45+ 		'lc_kwargs'  in  llm  && 
46+ 		'invocationKwargs'  in  llm . lc_kwargs  && 
47+ 		typeof  llm . lc_kwargs . invocationKwargs  ===  'object'  && 
48+ 		'thinking'  in  llm . lc_kwargs . invocationKwargs  && 
49+ 		llm . lc_kwargs . invocationKwargs . thinking . type  ===  'enabled' 
50+ 	) ; 
51+ } 
52+ 
4153/** 
4254 * Type guard to check if the LLM has a format property(Ollama) 
4355 */ 
@@ -61,6 +73,10 @@ export function getOutputParserForLLM(
6173		return  new  NaiveJsonOutputParser ( ) ; 
6274	} 
6375
76+ 	if  ( isModelInThinkingMode ( llm ) )  { 
77+ 		return  new  NaiveJsonOutputParser ( ) ; 
78+ 	} 
79+ 
6480	return  new  StringOutputParser ( ) ; 
6581} 
6682
Original file line number Diff line number Diff line change @@ -62,6 +62,20 @@ describe('chainExecutor', () => {
6262			const  parser  =  chainExecutor . getOutputParserForLLM ( regularModel ) ; 
6363			expect ( parser ) . toBeInstanceOf ( StringOutputParser ) ; 
6464		} ) ; 
65+ 
66+ 		it ( 'should return NaiveJsonOutputParser for Anthropic models in thinking mode' ,  ( )  =>  { 
67+ 			const  model  =  { 
68+ 				lc_kwargs : { 
69+ 					invocationKwargs : { 
70+ 						thinking : { 
71+ 							type : 'enabled' , 
72+ 						} , 
73+ 					} , 
74+ 				} , 
75+ 			} ; 
76+ 			const  parser  =  chainExecutor . getOutputParserForLLM ( model  as  unknown  as  BaseChatModel ) ; 
77+ 			expect ( parser ) . toBeInstanceOf ( NaiveJsonOutputParser ) ; 
78+ 		} ) ; 
6579	} ) ; 
6680
6781	describe ( 'NaiveJsonOutputParser' ,  ( )  =>  { 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments