fix(anthropic): enforce thinking sampling and forced tool_choice constraints

PC
9h ago
+236-24

Description

Anthropic answers 400 to two kinds of request that the Messages translation still sends, while the Bedrock Converse adapter (#1657) already adapts them:

  • top_p below 0.95 with thinking. Only temperature != 1 was dropped; extended thinking also requires top_p >= 0.95, so e.g. reasoning_effort: "low" with top_p: 0.9 on claude-sonnet-4-6 failed.
  • Thinking with a forced tool_choice. Thinking now runs after the tool translation (including the relaxation to auto); when tool_choice still forces a tool (any/tool), thinking and output_config are left out, as on Bedrock. thinking: disabled and auto are kept.

Tests: TestConvertToAnthropicRequest_ThinkingSamplingConstraints and TestConvertToAnthropicRequest_ThinkingWithForcedToolChoice in internal/providers/anthropic.

AI Generated (optional)

Prepared with AI assistance; the provider test suite was run locally.

Summary by CodeRabbit

  • Bug Fixes
    • Anthropic requests now handle reasoning settings more consistently: incompatible temperature and sampling values are omitted when thinking is enabled, and the token limit is adjusted when needed.
    • Thinking is omitted for forced tool choices, while remaining available for automatic tool selection. Disabled thinking and compatible sampling settings are preserved.