LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • Agent
  • Middleware
  • Backends
  • Sandboxes
  • Skills
  • Subagents
  • Types
Modal
Daytona
Deno
Node VFS
Sandbox Standard Tests
  • Vitest
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

OverviewAgentMiddlewareBackendsSandboxesSkillsSubagentsTypes
Modal
Daytona
Deno
Node VFS
Sandbox Standard Tests
Vitest
Language
Theme
JavaScriptdeepagentsmiddlewarepatchDanglingToolCalls
Function●Since v1.6

patchDanglingToolCalls

Patch tool call / tool response parity in a messages array.

Ensures strict 1:1 correspondence between AIMessage tool_calls and ToolMessage responses:

  1. Dangling tool_calls — an AIMessage contains a tool_call with no matching ToolMessage anywhere after it. A synthetic cancellation ToolMessage is inserted immediately after the AIMessage.

  2. Orphaned ToolMessages — a ToolMessage whose tool_call_id does not match any tool_call in a preceding AIMessage. The ToolMessage is removed.

Both directions are required for providers that enforce strict parity (e.g. Google Gemini returns 400 INVALID_ARGUMENT otherwise).

Copy
patchDanglingToolCalls(
  messages: BaseMessage<MessageStructure<MessageToolSet>, MessageType>[]
): __type

Parameters

NameTypeDescription
messages*BaseMessage<MessageStructure<MessageToolSet>, MessageType>[]

The messages array to patch

View source on GitHub