-
Notifications
You must be signed in to change notification settings - Fork 306
Description
Bug Description
When using ChatYandexGPT with agents that require multiple iterations (maxIterations > 1), a TypeError occurs during AIMessage construction:
TypeError: Cannot read properties of undefined (reading 'additional_kwargs')
The error occurs at line 30 in the AIMessage constructor:
const rawToolCalls = initParams.additional_kwargs?.tool_calls;Environment
- Package:
@langchain/yandexversion0.1.0 - Core:
@langchain/coreversion0.3.73 - LangChain:
langchainversion0.3.32 - Platform: macOS X 15.6.1, MacBook M1
Steps to Reproduce
- Initialize ChatYandexGPT model:
export const llm = new ChatYandexGPT({
temperature: 0,
apiKey: "...",
folderID: "...",
model: 'yandexgpt',
});- Create an agent with tools:
import { ChatYandexGPT } from "@langchain/yandex";
import { AgentExecutor, createToolCallingAgent } from "langchain/agents";
import { RequestsGetTool } from "langchain/tools";
const tools = [new RequestsGetTool()];
const agent = await createOpenAIFunctionsAgent({
llm: llm,
tools: tools,
prompt: ChatPromptTemplate.fromMessages([
new SystemMessage('You are an API assistant...'),
new HumanMessage('{input}'),
['placeholder', '{agent_scratchpad}'],
]),
streamRunnable: true,
});
const agentExecutor = new AgentExecutor({
agent,
tools,
maxIterations: 3, // Error occurs when > 1
});- Execute any query that requires tool usage:
await agentExecutor.invoke({ input: "Make an HTTP request hello" });Expected Behavior
The agent should execute successfully with multiple iterations, properly handling tool calls and creating AIMessage instances with correctly initialized additional_kwargs.
Actual Behavior
The execution fails with a TypeError when the agent attempts to create AIMessage instances during subsequent iterations, specifically when trying to access additional_kwargs.tool_calls.
Root Cause Analysis
The issue appears to be in the ChatYandexGPT._generate() method implementation. When creating AIMessage instances, the method is not properly initializing the additional_kwargs parameter, causing it to be undefined when the AIMessage constructor tries to access it.
Suggested Fix
The ChatYandexGPT._generate() method should ensure that additional_kwargs is always provided when creating AIMessage instances:
// Current (problematic) implementation likely:
return new AIMessage(response.content);
// Should be:
return new AIMessage({
content: response.content,
additional_kwargs: response.additional_kwargs || {}
});Additional Context
- The error only occurs when
maxIterations > 1, suggesting it's related to tool calling in multi-step agent execution - Other chat models (ChatOpenAI, ChatAnthropic, etc.) work correctly with the same setup
- The issue is specific to the JavaScript/TypeScript implementation of ChatYandexGPT
Related Issues
This appears to be related to issue #32814 in the main langchain repository, but the actual fix needs to be implemented in the langchain-community package.