Skip to content
Source
Sign inSign up
TanStack/ai

ai-client: tool-call parts have no `metadata` type, though StreamProcessor sets it (web search sources) #2296

OpenXhale1(GitHub profile, opens in a new tab) opened this issue 0 comments
Xhale1(GitHub profile, opens in a new tab)commented

Disclaimer: I used Opus 5.5 to help report this issue. I ran into it while migrating my company's repo from Vercel AI to Tanstack AI.

TanStack AI version

@tanstack/ai-client 1.4.0, @tanstack/ai-react 1.3.0, @tanstack/ai 1.10.1

Framework/Library version

React 19, TypeScript

Describe the bug and the steps to reproduce it

Since #1376, OpenAI and Gemini web search sources arrive on the tool-call part's metadata.sources, and docs/tools/provider-tools.md says to read them with getProviderExecutedMetadata(part). That works at runtime: StreamProcessor.handleToolCallStartEvent copies TOOL_CALL_START.metadata onto the part, and the core ToolCallPart in @tanstack/ai declares metadata?: TMetadata.

But the client's tool-call part types in packages/ai-client/src/types.ts (UntypedToolCallPart and ToolCallPartForTool) don't declare metadata. So with the UIMessage that useChat returns:

  • part.metadata is TS2339: Property 'metadata' does not exist on type 'UntypedToolCallPart'.
  • getProviderExecutedMetadata(part) is TS2559: Type 'UntypedToolCallPart' has no properties in common with type '{ metadata?: unknown; }', because the helper's parameter is a weak type.

The docs snippet compiles only because it imports UIMessage from @tanstack/ai rather than from @tanstack/ai-client/@tanstack/ai-react.

Type repro (types.ts, with @tanstack/ai@1.10.1 and @tanstack/ai-client@1.4.0):

import { getProviderExecutedMetadata } from "@tanstack/ai";
import type { UIMessage } from "@tanstack/ai-client"; // same type as useChat().messages

export function sourcesOf(message: UIMessage) {
  for (const part of message.parts) {
    if (part.type !== "tool-call") continue;
    console.log(part.metadata); // TS2339
    return getProviderExecutedMetadata(part)?.sources; // TS2559
  }
  return undefined;
}

Runtime (runtime.mjs, node runtime.mjs): the part does have metadata once StreamProcessor sees the events @tanstack/ai-gemini emits for a google_search grounded answer.

import { getProviderExecutedMetadata } from "@tanstack/ai";
import { StreamProcessor } from "@tanstack/ai-client";

// The events @tanstack/ai-gemini emits for a google_search grounded answer, trimmed.
const processor = new StreamProcessor();
processor.prepareAssistantMessage();
for (const chunk of [
  { type: "RUN_STARTED", runId: "r1", threadId: "t1", timestamp: 0 },
  {
    type: "TOOL_CALL_START",
    toolCallId: "call_1",
    toolCallName: "google_search",
    timestamp: 0,
    index: 0,
    metadata: {
      providerExecuted: true,
      sources: [{ url: "https://example.com", title: "Example" }],
    },
  },
  {
    type: "TOOL_CALL_END",
    toolCallId: "call_1",
    toolCallName: "google_search",
    timestamp: 0,
    input: { queries: ["example"] },
  },
  { type: "RUN_FINISHED", runId: "r1", threadId: "t1", timestamp: 0, finishReason: "stop" },
]) {
  processor.processChunk(chunk);
}

const part = processor
  .getMessages()
  .at(-1)
  .parts.find((p) => p.type === "tool-call");
console.log("part.metadata:", JSON.stringify(part.metadata));
console.log("sources:", JSON.stringify(getProviderExecutedMetadata(part)?.sources));

Output:

part.metadata: {"providerExecuted":true,"sources":[{"url":"https://example.com","title":"Example"}]}
sources: [{"url":"https://example.com","title":"Example"}]

Expected: both lines in types.ts compile, matching the runtime and the core ToolCallPart.

Suggested fix

Add metadata?: unknown (or ProviderExecutedToolMetadata) to UntypedToolCallPart and ToolCallPartForTool in packages/ai-client/src/types.ts, the same as the core ToolCallPart. A type test that the client's tool-call part is assignable to the core ToolCallPart would keep the two from drifting again.

Workaround

Narrow with in: const metadata = 'metadata' in part ? part.metadata : undefined.

The self-contained types.ts and runtime.mjs above (no API key or network needed).

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

No

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
  1. tanstack-source added labelsbug

  2. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer

Sign in to join this conversation.