fix(openai-base): ignore arguments deltas after a tool call ended #2264
main fromrefs/pull/2264/head·merged by AlemTuzlak View on GitHub🎯 Changes#
In the OpenAI Responses adapter (@tanstack/openai-base), a response.function_call_arguments.delta that arrives after the call has already ended, via response.function_call_arguments.done or the response.output_item.done backstop, was still emitted as TOOL_CALL_ARGS after TOOL_CALL_END. Core appends it to the finalized arguments, so {"city":"Paris"} plus a late } fails to parse and the tool never runs (Failed to parse tool arguments as JSON).
The delta handler already guards metadata.started (no ARGS before START). This adds the matching metadata.ended guard (no ARGS after END) and logs the dropped delta through logger.errors, like the existing orphan-delta branch. The .done handler already skips on metadata.ended, so the two now agree.
- Unit: a parameterized case for both completion events. The pre-completion delta still streams, exactly one END carries the final arguments, no ARGS follows END, and the drop is logged once. It fails on
mainand passes here. - E2E:
openai-late-args-delta-wirereplays the raw SSE throughcreateOpenaiChatand asserts that the server tool runs once with{ city: 'Paris' }. Onmain, both cases fail with the parse error above. - Docs: not updated. No public API or documented behavior changes; this only stops an invalid ARGS-after-END event.
Not in this PR: @tanstack/ai-openrouter's Responses adapter has the same delta handler. I can send the same guard there separately if you'd like.
Verified locally: @tanstack/openai-base test:lib (508 passed), test:types and test:oxlint (no new warnings); @tanstack/ai-e2e test:types; the new Playwright spec (2 passed; both fail without the fix); oxfmt --check. I did not run the full pnpm run test:pr.
The fix and tests were written with AI assistance (Claude, reviewed with Codex and Antigravity); I reviewed and verified them as above.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. (Ran the affected package and E2E checks listed above, not the fulltest:pr.) - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Bug Fixes
- Late-arriving tool-call argument updates are now ignored after the call has completed. They no longer change the completed input or generate additional argument updates.
- Completed tool calls and their results remain consistent when streamed events arrive after completion, whether the call completes through either supported completion event.
- Late updates are logged for troubleshooting without affecting the completed call or its results.
AlemTuzlak approved these changes