Skip to content

fix(provider): coerce numeric tool call IDs for OpenAI-compatible providers#24026

Closed
Qiiks wants to merge 2 commits into
anomalyco:devfrom
Qiiks:fix/nvidia-nim-numeric-tool-call-ids-v2
Closed

fix(provider): coerce numeric tool call IDs for OpenAI-compatible providers#24026
Qiiks wants to merge 2 commits into
anomalyco:devfrom
Qiiks:fix/nvidia-nim-numeric-tool-call-ids-v2

Conversation

@Qiiks

@Qiiks Qiiks commented Apr 23, 2026

Copy link
Copy Markdown

Issue for this PR

Fixes #19947

Type of change

  • Bug fix

What does this PR do?

Some OpenAI-compatible providers (e.g. NVIDIA NIM kimi-k2.5) return numeric tool call IDs instead of strings, violating the OpenAI API spec. This causes Zod validation errors when the AI SDK processes responses.

The fix adds coercion at two points:

  1. Fetch interceptor (provider.ts): For @ai-sdk/openai-compatible responses, numeric IDs in tool_calls[].id and delta.tool_calls[].id are converted to strings before the AI SDK processes them. Both JSON and SSE streaming responses are handled.

  2. Part hydration (message-v2.ts): For backwards compatibility, numeric callID values stored in existing sessions are coerced to strings when loaded from the database.

The coercion functions were extracted to a shared utility (src/util/coerce-tool-call-ids.ts) to keep them usable across layers.

How did you verify your code works?

  • 16 unit tests covering coerceNumericToolCallIds and transformSSEStream (numeric coercion, string passthrough, nested objects, delta.tool_calls, SSE parsing, [DONE] passthrough, invalid JSON passthrough, multiple events)
  • bun typecheck passes with zero errors
  • Existing provider tests still pass (258/260, 2 pre-existing timeouts unrelated to this change)
  • Verified the coercion mutates the object correctly through type assertions

Checklist

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

@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.

…viders

Some OpenAI-compatible providers (e.g. NVIDIA NIM kimi-k2.5) return
numeric tool call IDs instead of strings, violating the OpenAI API spec.
This causes Zod validation errors when the AI SDK processes responses.

- Extract coerceNumericToolCallIds and transformSSEStream to shared utility
- Apply coercion at fetch interceptor for @ai-sdk/openai-compatible responses
- Coerce numeric callID on part hydration for backwards compat with
  existing sessions that cached numeric IDs before this fix

Refs: anomalyco#23886
@Qiiks Qiiks force-pushed the fix/nvidia-nim-numeric-tool-call-ids-v2 branch from 3ba8daf to 63343a1 Compare April 24, 2026 07:48
Comment thread packages/opencode/src/provider/provider.ts Outdated
@krasnikau-andrei

Copy link
Copy Markdown

I tried this version on NVIDIA Nim Devstral, and the bug still persists

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NVIDIA NIM kimik2.5 returns numeric tool call IDs causing 'expected id to be a string' error

2 participants