fix(anthropic): drop non-1 temperature when downgrading adaptive thinking - #33207
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Greptile SummaryThis PR fixes a rejection from Anthropic's API that occurs when a non-1
Confidence Score: 5/5Safe to merge — the change is a two-line guard in a single downgrade path that can only remove a parameter Anthropic would reject anyway, and the new tests confirm all three temperature cases behave correctly. The change is narrowly scoped to the adaptive-to-legacy translation path and cannot affect adaptive models or requests without thinking. The condition is logically correct for all temperature values (absent, 0, 1, 1.0), the No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/llms/anthropic/experimental_pass_through/messages/transformation.py | Adds two lines to drop temperature when it is not 1 during adaptive-to-legacy thinking downgrade; minor: the guard fires pop even when temperature is absent (harmless no-op due to default argument) |
| tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_messages_effort.py | Adds three focused unit tests (temperature=0 dropped, temperature=1 preserved for legacy thinking, temperature=0 preserved for adaptive thinking on 4.6); pure mock tests with no real network calls |
Reviews (1): Last reviewed commit: "fix(anthropic): drop non-1 temperature w..." | Re-trigger Greptile
| if capped_thinking.get("type") == "enabled" and optional_params.get("temperature") != 1: | ||
| optional_params.pop("temperature", None) |
There was a problem hiding this comment.
The condition
optional_params.get("temperature") != 1 evaluates to True when temperature is absent (None != 1), so pop("temperature", None) is called on every legacy-thinking translation even when no temperature was set. The pop default prevents any error, but the intent — "only strip an explicitly-provided non-1 temperature" — is clearer with an explicit membership check.
| if capped_thinking.get("type") == "enabled" and optional_params.get("temperature") != 1: | |
| optional_params.pop("temperature", None) | |
| if ( | |
| capped_thinking.get("type") == "enabled" | |
| and "temperature" in optional_params | |
| and optional_params["temperature"] != 1 | |
| ): | |
| optional_params.pop("temperature") |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Summary
When adaptive thinking is downgraded to legacy extended thinking for pre-4.6 Anthropic models, drop a non-1 temperature so the request is not rejected by Anthropic.
Changes
_translate_adaptive_effort_for_non_adaptive_model, after setting legacythinking.type=enabled, removetemperaturewhen it is present and not1Verification
tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_messages_effort.pyBackwards compatibility
No public API changes. Only strips an invalid temperature combination after adaptive-to-legacy translation.
Fixes #33203