Skip to content

Commit c8b9a7f

Browse files
authored
fix(Basic LLM Chain Node): Use JSON parsing for Claude 3.7 with thinking enabled (#15381)
1 parent 4657e34 commit c8b9a7f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

packages/@n8n/nodes-langchain/nodes/chains/ChainLLM/methods/chainExecutor.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff 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

packages/@n8n/nodes-langchain/nodes/chains/ChainLLM/test/chainExecutor.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff 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', () => {

0 commit comments

Comments
 (0)