Skip to content

fix(kosong): support adaptive thinking for Opus 4.7+ models#1911

Open
sisterdong wants to merge 3 commits intoMoonshotAI:mainfrom
sisterdong:fix/adaptive-thinking-opus-4.7
Open

fix(kosong): support adaptive thinking for Opus 4.7+ models#1911
sisterdong wants to merge 3 commits intoMoonshotAI:mainfrom
sisterdong:fix/adaptive-thinking-opus-4.7

Conversation

@sisterdong
Copy link
Copy Markdown

@sisterdong sisterdong commented Apr 16, 2026

Summary

  • Fix _use_adaptive_thinking() to support Claude Opus 4.7 and future Opus versions (>= 4.6)
  • The method was hardcoded to only match opus-4.6 / opus-4-6, causing Opus 4.7 to use the legacy {"type": "enabled", "budget_tokens": ...} thinking format, which Opus 4.7 rejects with invalid_request_error
  • Replace hardcoded string matching with regex-based version comparison so any Opus >= 4.6 automatically uses adaptive thinking

Changes

  • Add import re at the top of the file
  • Rewrite _use_adaptive_thinking() to extract major.minor version from model name and compare against (4, 6)

Test

Manually verified with various model names:

Model _use_adaptive_thinking()
claude-opus-4-7 True
claude-opus-4.7 True
claude-opus-4-6 True
aws/claude-opus-4-6 True
claude-opus-4-8 True ✅ (future-proof)
claude-sonnet-4-5 False ✅ (non-opus unaffected)

Open with Devin

The _use_adaptive_thinking method was hardcoded to only match
opus-4.6 and opus-4-6, causing Opus 4.7 to fall through to the
legacy budget-based thinking code path. Opus 4.7 rejects this
format with an invalid_request_error.

Replace the hardcoded string check with a regex-based version
comparison so that any Opus model >= 4.6 automatically uses the
adaptive thinking API.
Copilot AI review requested due to automatic review settings April 16, 2026 16:23
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f30a6e7e7c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +243 to +246
match = re.search(r"opus[.-]?(\d+)[.-](\d+)", model)
if match:
major, minor = int(match.group(1)), int(match.group(2))
return (major, minor) >= (4, 6)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Avoid parsing release date as Opus minor version

The new pattern treats the second numeric segment after opus as a semantic minor version, so model IDs like claude-opus-4-20250514 are parsed as (4, 20250514) and incorrectly classified as >= 4.6. That flips pre-4.6 Opus models onto adaptive thinking, which this method explicitly says should only be used for 4.6+ and can cause request-shape incompatibilities on older models. Please tighten version extraction so date suffixes are not interpreted as the minor version.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch! Fixed in c248955 — tightened the regex to (\d{1,3})(?!\d) so date suffixes like claude-opus-4-20250514 are no longer misinterpreted as minor versions.

devin-ai-integration[bot]

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates Anthropic chat provider model detection to correctly enable adaptive thinking for Claude Opus models versioned at 4.6+ (including Opus 4.7+), avoiding invalid legacy thinking payloads for newer Opus variants.

Changes:

  • Add re import for model parsing.
  • Rewrite _use_adaptive_thinking() to regex-extract Opus major/minor and enable adaptive thinking for versions >= 4.6.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 238 to +247
def _use_adaptive_thinking(self) -> bool:
"""Whether to use adaptive thinking (Opus 4.6+) instead of budget-based thinking."""
model = self._model.lower()
return "opus-4.6" in model or "opus-4-6" in model
if "opus" not in model:
return False
match = re.search(r"opus[.-]?(\d+)[.-](\d+)", model)
if match:
major, minor = int(match.group(1)), int(match.group(2))
return (major, minor) >= (4, 6)
return False
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

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

The updated model parsing logic is intended to support additional Opus naming variants (e.g., claude-opus-4-7, claude-opus-4.7, aws/claude-opus-4-6) and future minor versions, but the existing snapshot tests only cover claude-opus-4-6-.... Add automated coverage for at least one 4.7 model name and one dotted form (4.7) to prevent regressions in _use_adaptive_thinking() behavior.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agree that test coverage would be valuable. Will leave that to the maintainers to decide on the preferred test structure for this project.

dongmengnan added 2 commits April 17, 2026 11:49
Tighten the regex to limit the minor version capture to at most 3
digits followed by a non-digit boundary. This prevents model IDs
like claude-opus-4-20250514 from being incorrectly classified as
adaptive-thinking-capable.
Opus 4.7 defaults display to "omitted", which means thinking blocks
have empty text. This makes the thinking stream invisible to the user
even when thinking is enabled. Set display to "summarized" so users
can see the thinking process.
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 1 new potential issue.

View 5 additional findings in Devin Review.

Open in Devin Review

thinking_config = {"type": "disabled"}
case _:
thinking_config = {"type": "adaptive"} # type: ignore[typeddict-item]
thinking_config = {"type": "adaptive", "display": "summarized"} # type: ignore[typeddict-item]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Test snapshot not updated after adding display: summarized to adaptive thinking config

The code now produces {"type": "adaptive", "display": "summarized"} for adaptive thinking (line 258), but the existing test test_anthropic_opus_46_adaptive_thinking at packages/kosong/tests/api_snapshot_tests/test_anthropic.py:522 still asserts body["thinking"] == snapshot({"type": "adaptive"}). This will cause the test to fail because the request body now includes the extra "display": "summarized" key.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

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