fix(core): handle tool call chunks without IDs in streaming mode #8581
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix tool call streaming for empty schemas
Fixes a bug where tool calls with empty schemas (
z.object({})
) were being dropped in streaming mode but worked correctly in non-streaming mode.Problem
The issue occurred because
AIMessageChunk
constructor was filtering out tool call chunks that didn't have an ID. OpenAI sometimes sends chunks without IDs for tools with empty parameter schemas, causing these tool calls to be completely lost in streaming mode while working fine in non-streaming mode.Solution
Changes:
langchain-core/src/messages/ai.ts
to assign fallback IDs for chunks without IDs (format:"fallback-{index}"
)langchain-core/src/messages/tests/base_message.test.ts
libs/langchain-openai/src/tests/chat_models-extended.int.test.ts
to verify streaming/non-streaming parityResult
Tool calls now work consistently in both streaming and non-streaming modes for tools with empty schemas. Users can reliably use parameter-less tools like
get_current_time
in streaming contexts.Before:
After:
Fixes #8518