Skip to content

fix(ai): tolerate empty-string tool call id/name in streaming deltas - #37842

Closed
Garfier wants to merge 1 commit into
anomalyco:v2from
Garfier:fix-tool-stream-empty-id
Closed

fix(ai): tolerate empty-string tool call id/name in streaming deltas#37842
Garfier wants to merge 1 commit into
anomalyco:v2from
Garfier:fix-tool-stream-empty-id

Conversation

@Garfier

Copy link
Copy Markdown

Issue for this PR

Closes#37841

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Some OpenAI-compatible APIs (e.g. DashScope token-plan / GLM-5.2) send empty strings for id and function.name on subsequent tool call deltas instead of omitting them or sending null. ToolStream.appendOrStart uses ?? (nullish coalescing) to fall back to accumulated identity from the first delta, but "" is not nullish so the fallback never triggers, and !id / !name then fails with "OpenAI Chat tool call delta is missing id or name".

The fix treats empty strings as absent by using || before ??:

constid=(delta.id||undefined)??current?.idconstname=(delta.name||undefined)??current?.name

This preserves the accumulated identity from the first delta (which does contain valid id and name) when subsequent deltas send empty strings.

How did you verify your code works?

  • Added a test case simulating the DashScope token-plan streaming format (first delta with valid id/name, subsequent deltas with empty strings). All 8 tests in tool-stream.test.ts pass.
  • Verified against the real token-plan API (token-plan.cn-beijing.maas.aliyuncs.com) with GLM-5.2 by curling a streaming tool call request and confirming the empty-string delta pattern.
  • Built a local binary and confirmed tool calls work end-to-end with the fix.

Screenshots / recordings

N/A (not a UI change)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Some OpenAI-compatible APIs (e.g. DashScope token-plan / GLM-5.2) send
empty strings for id and function.name on subsequent tool call deltas
instead of omitting them or sending null. The nullish coalescing
operator (??) does not treat empty strings as absent, causing
appendOrStart to fail with 'tool call delta is missing id or name'.
Treat empty strings as absent so the accumulated identity from the
first delta is preserved.
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on the search results, I found one potentially related PR:

Related PR:

However, #33622 is not a duplicate—it addresses a different issue with a different provider. This PR (#37842) is specifically about tolerating empty strings in tool call id and name fields from DashScope/GLM-5.2, which is a distinct problem from the spurious events issue in Qwen.

@Garfier

Copy link
Copy Markdown
Author

Closing: upstream v2 already includes an equivalent fix (prefer accumulated identity over delta values). The upstream approach current?.id ?? delta.id is cleaner. Glad to see this resolved!

@GarfierGarfier closed this Jul 20, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@Garfier