Skip to content

ChatCerebras fails to call tools when messages.content === "" #8643

@hideya

Description

@hideya

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).

NOTE: I have a fix: hideya@01577b5
I wanted to file a PR, but I currently don't have enough time to fully test the fix... (sorry...)

Example Code

import { ChatCerebras } from '@langchain/cerebras';
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { HumanMessage } from "@langchain/core/messages";
import { tool } from '@langchain/core/tools';

const magicTool = tool(
    ({ input }) => {
        return '42';
    },
    {
        name: 'magic_function',
        description: 'Applies a magic function to an input.',
    },
);

const llm = new ChatCerebras({
    model: 'gpt-oss-120b',
});

const agent = createReactAgent({
    llm,
    tools: [magicTool]
});

const query = 'Can you apply the magic function to 123?';
const messages =  { messages: [new HumanMessage(query)] };
const result = await agent.invoke(messages);
const response = result.messages[result.messages.length - 1].content;

console.log(response);

Error Message and Stack Trace (if applicable)

file:///.../langchain-cerebras-bug/node_modules/@cerebras/cerebras_cloud_sdk/error.mjs:36
            return new BadRequestError(status, error, message, headers);
                   ^

BadRequestError: 400 Messages with role 'tool' must be a response to a preceeding message with 'tool_calls'
    at APIError.generate (file:///.../langchain-cerebras-bug/node_modules/@cerebras/cerebras_cloud_sdk/error.mjs:36:20)
    at Cerebras.makeStatusError (file:///.../langchain-cerebras-bug/node_modules/@cerebras/cerebras_cloud_sdk/core.mjs:286:25)
    at Cerebras.makeRequest (file:///.../langchain-cerebras-bug/node_modules/@cerebras/cerebras_cloud_sdk/core.mjs:332:30)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///.../langchain-cerebras-bug/node_modules/@langchain/cerebras/dist/chat_models.js:504:25
    at async RetryOperation._fn (/...langchain-cerebras-bug/node_modules/p-retry/index.js:50:12) {
  status: 400,
  headers: {
    'alt-svc': 'h3=":443"; ma=86400',
    'cf-cache-status': 'DYNAMIC',
    'cf-ray': '96ba1e55892305b2-KIX',
    connection: 'keep-alive',
    'content-length': '176',
    'content-type': 'application/json',
    date: 'Thu, 07 Aug 2025 22:12:58 GMT',
    'referrer-policy': 'strict-origin-when-cross-origin',
    server: 'cloudflare',
    'set-cookie': '...',
    'strict-transport-security': 'max-age=3600; includeSubDomains',
    'x-content-type-options': 'nosniff'
  },
  error: {
    message: "Messages with role 'tool' must be a response to a preceeding message with 'tool_calls'",
    type: 'invalid_request_error',
    param: 'messages',
    code: 'wrong_api_format'
  },
  attemptNumber: 1,
  retriesLeft: 6,
  pregelTaskId: '2fe113f1-efdf-56f3-b0b6-045ca3c5ae83'
}

Node.js v20.18.1

Description

When I run the above example code, I get the above error.

I looked into the cause and found an issue in

if (typeof messages.content === "string") {
return [
{
role: "assistant",
content: messages.content,
},
];
}

The issue is that messages.content is set to "" when calling tools, thus it fails to perform tool calling.
I guess messages.content is expected to be null, but the reality is that the above test code passes "".

This diffs should fix the error: hideya@01577b5

I applied equivalent changes to node_modules/@langchain/cerebras/dist/utils.js and confirmed it fixes the error.

System Info

% yarn info @langchain/cerebras
   ...
  name: '@langchain/cerebras',
  'dist-tags': {
    latest: '0.0.2'
  },
  ...
% sw_vers
ProductName:            macOS
ProductVersion:         15.6
BuildVersion:           24G84
% node -v              
v20.18.1

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