Skip to content

ChatYandexGPT: TypeError when creating AIMessage - additional_kwargs is undefined #287

@starchou6

Description

@starchou6

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/yandex version 0.1.0
  • Core: @langchain/core version 0.3.73
  • LangChain: langchain version 0.3.32
  • Platform: macOS X 15.6.1, MacBook M1

Steps to Reproduce

  1. Initialize ChatYandexGPT model:
export const llm = new ChatYandexGPT({
  temperature: 0,
  apiKey: "...",
  folderID: "...",
  model: 'yandexgpt',
});
  1. 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
});
  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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions