> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/cloudflare/agents/llms.txt
> Use this file to discover all available pages before exploring further.

# Migrating to AI SDK v6

> Upgrade guide for migrating from AI SDK v5 to v6 with @cloudflare/ai-chat

This guide covers the changes needed when upgrading from AI SDK v5 to v6 with `@cloudflare/ai-chat`.

## Installation

```bash theme={null}
npm install ai@latest @ai-sdk/react@latest @ai-sdk/openai@latest
```

## Breaking changes

### 1. `convertToModelMessages()` is now async

Add `await` to all calls:

<CodeGroup>
  ```typescript v5 theme={null}
  const result = streamText({
    messages: convertToModelMessages(this.messages),
    model: openai("gpt-4o")
  });
  ```

  ```typescript v6 theme={null}
  const result = streamText({
    messages: await convertToModelMessages(this.messages),
    model: openai("gpt-4o")
  });
  ```
</CodeGroup>

### 2. `CoreMessage` removed

Replace `CoreMessage` with `ModelMessage` and `convertToCoreMessages()` with `convertToModelMessages()`:

<CodeGroup>
  ```typescript v5 theme={null}
  import { convertToCoreMessages, type CoreMessage } from "ai";
  ```

  ```typescript v6 theme={null}
  import { convertToModelMessages, type ModelMessage } from "ai";
  ```
</CodeGroup>

### 3. Tool pattern: server-side tools (recommended)

v6 introduces `needsApproval` and the `onToolCall` callback. For most apps, define tools on the server with `tool()` from `"ai"` for full Zod type safety:

<CodeGroup>
  ```typescript v5 (Before) theme={null}
  // Client defined tools with AITool type
  useAgentChat({
    agent,
    tools: clientTools,
    experimental_automaticToolResolution: true,
    toolsRequiringConfirmation: ["askConfirmation"]
  });
  ```

  ```typescript v6 (After - Server) theme={null}
  // Server: all tools defined here
  const tools = {
    getWeather: tool({
      description: "Get weather",
      inputSchema: z.object({ city: z.string() }),
      execute: async ({ city }) => fetchWeather(city)
    }),
    getLocation: tool({
      description: "Get user location",
      inputSchema: z.object({})
      // No execute -- client handles via onToolCall
    }),
    processPayment: tool({
      description: "Process payment",
      inputSchema: z.object({ amount: z.number() }),
      needsApproval: async ({ amount }) => amount > 100,
      execute: async ({ amount }) => charge(amount)
    })
  };
  ```

  ```typescript v6 (After - Client) theme={null}
  // Client: handle tools via callbacks
  useAgentChat({
    agent,
    onToolCall: async ({ toolCall, addToolOutput }) => {
      if (toolCall.toolName === "getLocation") {
        const pos = await getPosition();
        addToolOutput({
          toolCallId: toolCall.toolCallId,
          output: { lat: pos.coords.latitude, lng: pos.coords.longitude }
        });
      }
    }
  });
  ```
</CodeGroup>

**Dynamic client tools (SDK/platform pattern):**

If you are building an SDK or platform where tools are defined dynamically by the embedding application at runtime, the `tools` option on `useAgentChat` and `createToolsFromClientSchemas()` on the server are still fully supported:

<CodeGroup>
  ```typescript Server theme={null}
  // Server: accept whatever tools the client sends
  const tools = {
    ...createToolsFromClientSchemas(options.clientTools),
    ...serverTools
  };
  ```

  ```typescript Client theme={null}
  // Client: register tools dynamically
  useAgentChat({
    agent,
    tools: dynamicTools,
    onToolCall: async ({ toolCall, addToolOutput }) => {
      const tool = dynamicTools[toolCall.toolName];
      if (tool?.execute) {
        const output = await tool.execute(toolCall.input);
        addToolOutput({ toolCallId: toolCall.toolCallId, output });
      }
    }
  });
  ```
</CodeGroup>

### 4. `generateObject` mode option removed

Remove `mode: "json"` or similar from `generateObject` calls.

### 5. `isToolUIPart` and `getToolName` now include dynamic tools

In v6, these check both static and dynamic tool parts. For the old behavior, use `isStaticToolUIPart` and `getStaticToolName`. Most users do not need to change anything.

## Deprecated APIs

| Deprecated                             | Replacement                                                    |
| -------------------------------------- | -------------------------------------------------------------- |
| `toolsRequiringConfirmation`           | [`needsApproval`](/advanced/human-in-the-loop) on server tools |
| `experimental_automaticToolResolution` | [`onToolCall`](/ai/client-tools-continuation) callback         |
| `addToolResult()`                      | `addToolOutput()` or `addToolApprovalResponse()`               |

<Note>
  **Not deprecated:** `AITool`, `createToolsFromClientSchemas()`, `extractClientToolSchemas()`, and the `tools` option on `useAgentChat` are supported for SDK/platform use cases where tools are defined dynamically at runtime.
</Note>

## Migration checklist

<Steps>
  <Step title="Update packages">
    * `ai` to `^6.0.0`
    * `@ai-sdk/react` to `^3.0.0`
    * `@ai-sdk/openai` (and other providers) to `^3.0.0`
  </Step>

  <Step title="Add await to convertToModelMessages()">
    Add `await` to all `convertToModelMessages()` calls
  </Step>

  <Step title="Replace CoreMessage">
    Replace `CoreMessage` with `ModelMessage`
  </Step>

  <Step title="Replace convertToCoreMessages()">
    Replace `convertToCoreMessages()` with `convertToModelMessages()`
  </Step>

  <Step title="Remove mode from generateObject">
    Remove `mode` from `generateObject` calls
  </Step>

  <Step title="Move tool definitions to server">
    Move static tool definitions to server using `tool()` (recommended for most apps)
  </Step>

  <Step title="Use onToolCall callback">
    Use `onToolCall` in `useAgentChat` for client-side tool execution
  </Step>

  <Step title="Replace toolsRequiringConfirmation">
    Replace `toolsRequiringConfirmation` with `needsApproval`
  </Step>

  <Step title="Replace addToolResult()">
    Replace `addToolResult()` with `addToolOutput()` or `addToolApprovalResponse()`
  </Step>
</Steps>

## Further reading

* [Official AI SDK v6 migration guide](https://ai-sdk.dev/docs/migration-guides/migration-guide-6-0)
* [Human in the Loop](/advanced/human-in-the-loop) - `needsApproval` and `addToolApprovalResponse`
* [Client Tools](/ai/client-tools-continuation) - `onToolCall` and auto-continuation
