Skip to content

Tool calling broken after upgrading from langchain 0.4 → 1.x (args becomes string, causes “Malformed args”) #9450

@a289459798

Description

@a289459798

Checked other resources

  • This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

import { StateGraph } from "@langchain/langgraph";
import { StateAnnotation } from "./state.js";
import { HumanMessage } from "@langchain/core/messages";
import { createAgent, tool } from "langchain";
import { ChatOpenAI } from "@langchain/openai";
import { z } from "zod";

const searchDatabase = tool(
  ({ query, limit }) => `Found ${limit} results for '${query}'`,
  {
    name: "search_database",
    description: "Search the customer database for records matching the query.",
    schema: z.object({
      query: z.string().describe("Search terms to look for"),
      limit: z.number().describe("Maximum number of results to return"),
    }),
  }
);

const agent = createAgent({
  model: new ChatOpenAI({
    model: "qwen-max-latest",
    apiKey: "sk-xxxxxxxxxxxxxxxxxxxxxx",
    configuration: {
      baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
    },
  }),
  tools: [searchDatabase],
});
const callModel = async function () {
  
  const res = await agent.invoke({
    messages: [new HumanMessage("查一10条手机数据")],
  });

  console.log("res", res);
}

const builder = new StateGraph(StateAnnotation)
  .addNode("call_model", callModel)
  .addEdge("__start__", "call_model")
  .addEdge("call_model", "__end__")

export const graph = builder.compile();

graph.name = "test graph";

Error Message and Stack Trace (if applicable)

{
  messages: [
    HumanMessage {
      "id": "d493b17e-8796-4a09-a8c1-529fe761b3bb",
      "content": "查一10条手机数据",
      "additional_kwargs": {},
      "response_metadata": {}
    },
    AIMessageChunk {
      "id": "chatcmpl-955542e4-eed3-435e-91a4-1ae3286641d6",
      "content": "",
      "name": "model",
      "additional_kwargs": {
        "tool_calls": [
          {
            "index": 0,
            "id": "",
            "type": "function",
            "function": "[Object]"
          }
        ]
      },
      "response_metadata": {
        "model_provider": "openai",
        "usage": {
          "prompt_tokens": 295,
          "completion_tokens": 24,
          "total_tokens": 319
        }
      },
      "tool_calls": [],
      "tool_call_chunks": [
        {
          "name": "search_database",
          "args": "{\"query\": \"手机\", \"limit\": 10}",
          "id": "",
          "index": 0,
          "type": "tool_call_chunk"
        }
      ],
      "invalid_tool_calls": [
        {
          "name": "search_database",
          "args": "{\"query\": \"手机\", \"limit\": 10}",
          "id": "",
          "error": "Malformed args.",
          "type": "invalid_tool_call"
        }
      ],
      "usage_metadata": {
        "input_tokens": 295,
        "output_tokens": 24,
        "total_tokens": 319,
        "input_token_details": {},
        "output_token_details": {}
      }
    }
  ]
}

Description

After upgrading:

langchain from 0.4.x → 1.x

@langchain/openai from 0.6.3 → 1.x

tool calling stopped working.
The model now outputs stringified arguments, which causes LangChain to treat the call as malformed, producing:

"error": "Malformed args."

System Info

"@langchain/anthropic": "^1.1.0",
    "@langchain/community": "^1.0.4",
    "@langchain/core": "^1.0.6",
    "@langchain/langgraph": "^1.0.2",
    "@langchain/openai": "^1.1.2",
    "langchain": "^1.0.6"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions