fix(kosong): support adaptive thinking for Opus 4.7+ models#1911
fix(kosong): support adaptive thinking for Opus 4.7+ models#1911sisterdong wants to merge 3 commits intoMoonshotAI:mainfrom
Conversation
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.
There was a problem hiding this comment.
💡 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".
| 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) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
reimport 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.
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agree that test coverage would be valuable. Will leave that to the maintainers to decide on the preferred test structure for this project.
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.
| thinking_config = {"type": "disabled"} | ||
| case _: | ||
| thinking_config = {"type": "adaptive"} # type: ignore[typeddict-item] | ||
| thinking_config = {"type": "adaptive", "display": "summarized"} # type: ignore[typeddict-item] |
There was a problem hiding this comment.
🟡 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
Summary
_use_adaptive_thinking()to support Claude Opus 4.7 and future Opus versions (>= 4.6)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 withinvalid_request_errorChanges
import reat the top of the file_use_adaptive_thinking()to extract major.minor version from model name and compare against (4, 6)Test
Manually verified with various model names:
_use_adaptive_thinking()claude-opus-4-7True✅claude-opus-4.7True✅claude-opus-4-6True✅aws/claude-opus-4-6True✅claude-opus-4-8True✅ (future-proof)claude-sonnet-4-5False✅ (non-opus unaffected)