File tree 2 files changed +30
-0
lines changed
packages/@n8n/nodes-langchain/nodes/chains/ChainLLM 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,18 @@ export function isModelWithResponseFormat(
38
38
) ;
39
39
}
40
40
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
+
41
53
/**
42
54
* Type guard to check if the LLM has a format property(Ollama)
43
55
*/
@@ -61,6 +73,10 @@ export function getOutputParserForLLM(
61
73
return new NaiveJsonOutputParser ( ) ;
62
74
}
63
75
76
+ if ( isModelInThinkingMode ( llm ) ) {
77
+ return new NaiveJsonOutputParser ( ) ;
78
+ }
79
+
64
80
return new StringOutputParser ( ) ;
65
81
}
66
82
Original file line number Diff line number Diff line change @@ -62,6 +62,20 @@ describe('chainExecutor', () => {
62
62
const parser = chainExecutor . getOutputParserForLLM ( regularModel ) ;
63
63
expect ( parser ) . toBeInstanceOf ( StringOutputParser ) ;
64
64
} ) ;
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
+ } ) ;
65
79
} ) ;
66
80
67
81
describe ( 'NaiveJsonOutputParser' , ( ) => {
You can’t perform that action at this time.
0 commit comments