Skip to content

Conversation

@whoiskatrin
Copy link
Contributor

Migrate Agents SDK to AI SDK v6 beta with unified tool pattern.

Changes
AI SDK v6 Updates

  • Updated ai to ^6.0.0-beta, @ai-sdk/react and providers to ^3.0.0-beta
  • Added await to all convertToModelMessages() calls (now async in v6)
  • Removed deprecated mode option from generateObject calls

Unified Tool Pattern
Simplified client-side tool handling by adopting AI SDK v6's native pattern.

Before:

// Client: Define tools with custom AITool type
const tools = {
  getLocation: {
    parameters: { type: "object" },
    execute: async () => navigator.geolocation.getCurrentPosition(...)
  }
};
useAgentChat({
  tools,
  experimental_automaticToolResolution: true,
  toolsRequiringConfirmation: ["dangerousTool"]
});
// Server: Convert client schemas
const allTools = {
  ...serverTools,
  ...createToolsFromClientSchemas(clientTools)
};

After:

// Server: Define ALL tools in one place
const tools = {
  getLocation: tool({
    description: "Get user location",
    inputSchema: z.object({})
    // No execute = client handles
  }),
  payment: tool({
    inputSchema: z.object({ amount: z.number() }),
    needsApproval: ({ amount }) => amount > 100,  // Dynamic approval
    execute: async ({ amount }) => charge(amount)
  })
};
// Client: Handle via callback
useAgentChat({
  onToolCall: async ({ toolCall, addToolOutput }) => {
    if (toolCall.toolName === 'getLocation') {
      const pos = await getGeolocation();
      addToolOutput({ toolCallId: toolCall.toolCallId, output: pos });
    }
  }
});

New APIs

  • onToolCall callback in useAgentChat for client-side tool execution
  • addToolOutput() function for providing tool results

Deprecated (still functional)

  • AITool type, extractClientToolSchemas(), createToolsFromClientSchemas()
  • tools, toolsRequiringConfirmation, experimental_automaticToolResolution options
  • addToolResult() → use addToolOutput()

Docs

  • Added docs/migration-to-ai-sdk-v6.md with migration guide and checklist

@changeset-bot
Copy link

changeset-bot bot commented Dec 16, 2025

⚠️ No Changeset found

Latest commit: 205160d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@claude
Copy link

claude bot commented Dec 16, 2025

Claude Code Review

Migration Quality: Strong
The v6 migration is well-executed with good backward compatibility.

Issues Found:

  1. Race condition in onToolCall effect (ai-react.tsx:840-899)

    • Effect does not include useChatHelpers.addToolResult in dependencies but uses it
    • Can cause stale closure bugs if the function identity changes
    • Fix: Add to dependency array or use useCallback
  2. Missing test coverage for new onToolCall pattern

    • No tests verify the new onToolCall callback works correctly
    • Existing tests only cover deprecated tools option
    • Recommendation: Add tests for the primary migration path
  3. Type safety gap in addToolOutput (ai-react.tsx:1358-1371)

    • toolName parameter is optional but used as required (via empty string fallback)
    • Could cause issues with tool result tracking
    • Consider: Make toolName required or handle undefined explicitly

Architecture:

  • Unified tool pattern is cleaner than v5 approach
  • Deprecation strategy preserves backward compatibility
  • Migration guide is comprehensive

Minor observations:

  • 362-line migration doc is thorough but dense
  • No examples yet use onToolCall (only deprecated patterns remain)
  • Consider adding a working example to docs/

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 16, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@733

commit: 205160d

@whoiskatrin whoiskatrin marked this pull request as ready for review December 16, 2025 14:37
agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Dec 21, 2025
This documentation update syncs changes from cloudflare/agents PR #733 (migration to AI SDK v6).

**Changes:**
- Added comprehensive migration guide for upgrading from AI SDK v5 to v6 (beta)
- Documented breaking changes including unified tool pattern, async convertToModelMessages(), and removed CoreMessage type
- Included examples showing before/after code patterns
- Listed deprecated APIs and their replacements
- Documented new v6 features: Agent abstraction, tool execution approval, structured output with tool calling, and reranking support
- Added migration checklist for package updates and code changes

Related PR: cloudflare/agents#733
Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've rebased and approved this with a couple of comments. I don't recommend landing this until the sdk release is out, because the way we now do peer deps will break people if it's not a semver version string. We can land and release it the minute we know ai sdk is going out.

* Provide output for a tool call. Use this for tools that require user interaction
* or client-side execution.
*/
addToolOutput: (opts: AddToolOutputOptions) => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice


// Effect for new onToolCall callback pattern (v6 style)
// This fires when there are tool calls that need client-side handling
useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this probably needs a test

agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Dec 22, 2025
This commit adds comprehensive documentation for migrating from AI SDK v5 to v6
when using the Cloudflare Agents SDK. The guide covers:

- Breaking changes including unified tool pattern, async convertToModelMessages,
  CoreMessage type removal, and generateObject mode option removal
- Deprecated APIs and their replacements
- New v6 features: Agent abstraction, tool execution approval, structured output
  with tool calling, and reranking support
- Step-by-step migration checklist
- Third-party provider compatibility notes

This documentation is synced from PR #733 in cloudflare/agents which implements
the migration to AI SDK v6 beta with unified tool pattern.

Related PR: cloudflare/agents#733
@threepointone threepointone merged commit bdc1b0f into main Dec 22, 2025
5 checks passed
@threepointone threepointone deleted the ai-sdk-6 branch December 22, 2025 18:44
@github-actions github-actions bot mentioned this pull request Dec 22, 2025
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.

2 participants