Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure constructing a tool message from params, MESSAGE_COERCION_FAILURE #7647

Closed
5 tasks done
arturl opened this issue Feb 4, 2025 · 5 comments · Fixed by #7653
Closed
5 tasks done

Failure constructing a tool message from params, MESSAGE_COERCION_FAILURE #7647

arturl opened this issue Feb 4, 2025 · 5 comments · Fixed by #7653
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@arturl
Copy link

arturl commented Feb 4, 2025

Checked other resources

  • 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

see below

Error Message and Stack Trace (if applicable)

"Error: Unable to coerce message from array: only human, AI, system, developer, or tool message coercion is currently supported.
Received: {
  \"lc\": 1,
  \"type\": \"constructor\",
  \"id\": [
    \"langchain_core\",
    \"messages\",
    \"ToolMessage\"
  ],
  \"kwargs\": {
    \"content\": \"{\\\"question\\\":\\\"Show my stacks\\\",\\\"category\\\":\\\"projects/stacks\\\"}\",
    \"tool_call_id\": \"call_6f1y4NtvJtxwEDNuJyOIwdVR\",
    \"additional_kwargs\": {},
    \"response_metadata\": {},
    \"id\": \"6606e71a-9701-4141-af8f-8059bf8238f0\"
  }
}
Troubleshooting URL: https://js.langchain.com/docs/troubleshooting/errors/MESSAGE_COERCION_FAILURE/
    at constructMessageFromParams (/Users/arturl/g/pulumi.ai-2/copilot/nodemodules/@langchain/core/dist/messages/utils.cjs:104:63)
    at coerceMessageLikeToMessage (/Users/arturl/g/pulumi.ai-2/copilot/node_modules/@langchain/core/dist/messages/utils.cjs:124:16)
    at Array.map (<anonymous>)
    at BinaryOperatorAggregate.messagesStateReducer [as operator] (/Users/arturl/g/pulumi.ai-2/copilot/node_modules/@langchain/langgraph/dist/graph/message.cjs:16:36)
    at BinaryOperatorAggregate.update (/Users/arturl/g/pulumi.ai-2/copilot/node_modules/@langchain/langgraph/dist/channels/binop.cjs:57:35)
    at applyWrites (/Users/arturl/g/pulumi.ai-2/copilot/nodemodules/@langchain/langgraph/dist/pregel/algo.cjs:196:46)
    at PregelLoop.tick (/Users/arturl/g/pulumi.ai-2/copilot/node_modules/@langchain/langgraph/dist/pregel/loop.cjs:471:67)
    at CompiledStateGraph._runLoop (/Users/arturl/g/pulumi.ai-2/copilot/node_modules/@langchain/langgraph/dist/pregel/index.cjs:831:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async createAndRunLoop (/Users/arturl/g/pulumi.ai-2/copilot/node_modules/@langchain/langgraph/dist/pregel/index.cjs:984:17)"

Description

My code builds a langchain graph where one of the nodes invokes a tool.
The messages are then serialized using BaseCheckpointSaver.

One of the resulting messages looks like this:

{
  "lc": 1,
  "type": "constructor",
  "id": [
    "langchain_core",
    "messages",
    "ToolMessage"
  ],
  "kwargs": {
    "content": "{\"question\":\"What is the weather in Seattle\",\"category\":\"weather\"}",
    "tool_call_id": "call_6f1y4NtvJtxwEDNuJyOIwdVR",
    "additional_kwargs": {},
    "response_metadata": {},
    "id": "6606e71a-9701-4141-af8f-8059bf8238f0"
  }
}

This message fails in _constructMessageFromParams because it does not handle ToolMessage types:

A suggested fix would be add the following lines just before the last else clause:

        else if (className === "ToolMessage") {
            type = "tool";
        }

System Info

"@langchain/core": "^0.3.3",
"@langchain/langgraph": "^0.2.39",
"@langchain/langgraph-checkpoint": "^0.0.13",
"@langchain/langgraph-checkpoint-postgres": "^0.0.1",
"@langchain/openai": "^0.3.2",

Node.js version v18.20.3

Copy link

dosubot bot commented Feb 4, 2025

Hey there, @arturl! I'm here to help with any bugs, questions, or contributions you have in mind. Let's tackle this together!

To resolve the "MESSAGE_COERCION_FAILURE" error when processing ToolMessage types in the constructMessageFromParams function, ensure that the params object includes the necessary fields for a ToolMessage. Specifically, the params should have a type of "tool" and include a tool_call_id. If these conditions are not met, the function will throw an error with the "MESSAGE_COERCION_FAILURE" code [1].

Additionally, you can modify the code by adding a check for ToolMessage just before the last else clause in the constructMessageFromParams function. Add the following lines:

else if (className === "ToolMessage") {
    type = "tool";
}

This change will allow the function to correctly process ToolMessage types, addressing the error you are experiencing.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Feb 4, 2025
@jacoblee93
Copy link
Collaborator

Thanks for reporting - will fix!

@jacoblee93
Copy link
Collaborator

jacoblee93 commented Feb 5, 2025

Live, please bump to @langchain/[email protected]!

@arturl
Copy link
Author

arturl commented Feb 5, 2025

Thanks for the prompt turnaround @jacoblee93, I will update!

@arturl
Copy link
Author

arturl commented Feb 6, 2025

Fix confirmed, thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants