Skip to content

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

Open
Qiiks wants to merge 1 commit intoanomalyco:devfrom
Qiiks:fix/nvidia-nim-numeric-tool-call-ids-v2
Open

fix(provider): coerce numeric tool call IDs for OpenAI-compatible providers#24026
Qiiks wants to merge 1 commit intoanomalyco:devfrom
Qiiks:fix/nvidia-nim-numeric-tool-call-ids-v2

Conversation

@Qiiks
Copy link
Copy Markdown

@Qiiks Qiiks commented Apr 23, 2026

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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

1 participant