Skip to content

Conversation

@dukex
Copy link

@dukex dukex commented Dec 29, 2025

Background

Anthropic introduced a breaking change in their API requiring that sampling parameters use only temperature OR top_p, not both. This aligns with their Claude 4 migration guidelines.

Previously, the AI SDK Anthropic provider would send both parameters to the API when both were specified, which would result in an API error with the new Anthropic API requirements.

See: https://platform.claude.com/docs/en/about-claude/models/migrating-to-claude-4

Summary

This PR implements mutual exclusivity between temperature and topP parameters in the Anthropic provider:

Changes:

  • Added validation logic that runs when thinking mode is NOT enabled (thinking mode has its own parameter validation)
  • When both temperature and topP are provided:
    • temperature takes priority
    • topP is set to undefined and excluded from the API request
    • A warning is added: "topP is not supported when temperature is set. topP is ignored."
  • Updated existing tests to reflect the new behavior
  • Added comprehensive test suite for temperature/topP mutual exclusivity

Files Modified:

  • packages/anthropic/src/anthropic-messages-language-model.ts (lines 436-446)
  • packages/anthropic/src/anthropic-messages-language-model.test.ts

Manual Verification

  1. Test Suite Verification: All 235 tests pass, including:

    • Existing tests updated to remove conflicting parameters
    • New tests specifically for temperature/topP mutual exclusivity scenarios
    • Tests verify that thinking mode validation takes precedence (warnings appear in correct order)
  2. Expected Behavior:

    • When only temperature is provided → sends temperature to API ✅
    • When only topP is provided → sends topP to API ✅
    • When both are provided → sends only temperature, warns about topP being ignored ✅
    • When thinking mode is enabled with both → thinking validation handles all parameters ✅

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Related Issues

Resolves the Anthropic API breaking change for Claude 4 parameter requirements.

Resolves the Anthropic API breaking change where sampling parameters
must use only temperature OR top_p, not both. When both parameters
are provided, temperature takes priority and topP is ignored with a
warning message.
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.

1 participant