LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangChain
  • Universal
  • Hub
  • Node
  • Load
  • Serializable
  • Encoder Backed
  • File System
  • In Memory
LangChain Core
  • Agents
  • Caches
  • Base
  • Dispatch
  • Web
  • Manager
  • Promises
  • Chat History
  • Context
  • Base
  • Langsmith
  • Documents
  • Embeddings
  • Errors
  • Example Selectors
  • Indexing
  • Base
  • Chat Models
  • Llms
  • Profile
  • Load
  • Serializable
  • Memory
  • Messages
  • Tool
  • Output Parsers
  • Openai Functions
  • Openai Tools
  • Outputs
  • Prompt Values
  • Prompts
  • Retrievers
  • Document Compressors
  • Runnables
  • Graph
  • Singletons
  • Stores
  • Structured Query
  • Tools
  • Base
  • Console
  • Log Stream
  • Run Collector
  • Tracer Langchain
  • Stream
  • Async Caller
  • Chunk Array
  • Context
  • Env
  • Event Source Parse
  • Format
  • Function Calling
  • Hash
  • Json Patch
  • Json Schema
  • Math
  • Ssrf
  • Stream
  • Testing
  • Tiktoken
  • Types
  • Vectorstores
Text Splitters
MCP Adapters
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangChain
UniversalHubNodeLoadSerializableEncoder BackedFile SystemIn Memory
LangChain Core
AgentsCachesBaseDispatchWebManagerPromisesChat HistoryContextBaseLangsmithDocumentsEmbeddingsErrorsExample SelectorsIndexingBaseChat ModelsLlmsProfileLoadSerializableMemoryMessagesToolOutput ParsersOpenai FunctionsOpenai ToolsOutputsPrompt ValuesPromptsRetrieversDocument CompressorsRunnablesGraphSingletonsStoresStructured QueryToolsBaseConsoleLog StreamRun CollectorTracer LangchainStreamAsync CallerChunk ArrayContextEnvEvent Source ParseFormatFunction CallingHashJson PatchJson SchemaMathSsrfStreamTestingTiktokenTypesVectorstores
Text Splitters
MCP Adapters
Language
Theme
JavaScript@langchain/coremessagescollapseToolCallChunks
Functionā—Since v1.1

collapseToolCallChunks

Collapses an array of tool call chunks into complete tool calls.

This function groups tool call chunks by their id and/or index, then attempts to parse and validate the accumulated arguments for each group. Successfully parsed tool calls are returned as valid ToolCall objects, while malformed ones are returned as InvalidToolCall objects.

Copy
collapseToolCallChunks(chunks: ToolCallChunk<string>[]): __type

Chunks are grouped using the following matching logic:

  • If a chunk has both an id and index, it matches chunks with the same id and index
  • If a chunk has only an id, it matches chunks with the same id
  • If a chunk has only an index, it matches chunks with the same index

For each group, the function:

  1. Concatenates all args strings from the chunks
  2. Attempts to parse the concatenated string as JSON
  3. Validates that the result is a non-null object with a valid id
  4. Creates either a ToolCall (if valid) or InvalidToolCall (if invalid)

Parameters

NameTypeDescription
chunks*ToolCallChunk<string>[]

An array of ToolCallChunk objects to collapse

View source on GitHub