langchain · difficulty ◆◆
Consistent Tool-Call Chunks in the v1 Stream
Watch the function name type out, then stream clean JSON args.
Token-by-token tool calls should look exactly like the one-shot version. Now they do.
fully AI-generatedThis text is fully AI-generated and was published without human editorial review.$ pip install -q langchain-openai==1.3.1- openai 1.3.1 / core 1.4.7
- Release
- #35983
- PR
- gpt-4o / gpt-4o-mini
- Models
What it does
When you use ChatOpenAI with streaming enabled and call the model with a tool/function-calling schema, the model streams back partial chunks that represent the tool call in real time. This fix normalizes how those streamed tool-call chunks are formatted in API version v1, ensuring the output is consistent with non-streamed tool calls regardless of whether you receive the tool call all at once or token-by-token.
Why it matters
Tool calling is one of the most common production patterns, especially with gpt-4o and gpt-4o-mini via langchain-openai. If you build streaming UX (showing a function name being typed out live) or consume tool calls in an agent loop with streaming callbacks, you need reliable chunk shapes. This fix closes the compatibility gap between streamed and non-streamed output in the v1 API.
Example
$ Stream tool call chunksStreaming tool call chunks:
content chunk: ''
tool_call_chunk: [{'name': 'get_weather', 'args': '{"city": ', 'index': 0}]
tool_call_chunk: [{'name': '', 'args': '"Paris', 'index': 0}]
tool_call_chunk: [{'name': '', 'args': '"}', 'index': 0}]The name arrives in the first chunk and args stream token-by-token as the model generates JSON.
Common flags
- AIMessageChunk.tool_call_chunks
- Structured accessor for streamed tool-call fragments
- --v1
- OpenAI v1 API chunk normalization
History
Origin
Released in langchain-openai==1.3.1 and langchain-core==1.4.7 (2026-06-13), PR #35983.
Compatibility goal
The stream now yields clean, well-structured chunks that match the shape of a non-streamed AIMessage.tool_calls list.
Under the hood
Streamed vs one-shot parity
Before the fix, streaming on the v1 API could produce malformed or inconsistently-shaped chunks. After it, the tool-call event stream yields clean, well-structured chunks you can consume incrementally without post-hoc normalization.
Fun facts
Pros & cons
pros
- + Streamed output matches non-streamed
- + Clean incremental chunks
- + Fewer edge-case bugs
cons
- − Needs both core and openai updated
- − Chunks still require accumulation logic
Takeaways
- 1Upgrade langchain-openai to 1.3.1 and core to 1.4.7.
- 2Inspect chunk.tool_call_chunks while streaming to see the normalized shape.
- 3Confirm parity by comparing astream() output with a plain invoke().
Related commands
archive entry — written and published by a scheduled job, never editorially reviewed. Kept for reference: out of the site’s search index, and no ads run here. the reviewed library →
