mirror of
https://github.com/OpenHands/OpenHands.git
synced 2026-03-22 13:47:19 +08:00
Remove inconsistent parameters in claude sonnet (#11719)
This commit is contained in:
@@ -188,13 +188,13 @@ class LLM(RetryMixin, DebugMixin):
|
||||
if 'claude-opus-4-1' in self.config.model.lower():
|
||||
kwargs['thinking'] = {'type': 'disabled'}
|
||||
|
||||
# Anthropic constraint: Opus models cannot accept both temperature and top_p
|
||||
# Anthropic constraint: Opus 4.1 and Sonnet 4 models cannot accept both temperature and top_p
|
||||
# Prefer temperature (drop top_p) if both are specified.
|
||||
_model_lower = self.config.model.lower()
|
||||
# Limit to Opus 4.1 specifically to avoid changing behavior of other Anthropic models
|
||||
if ('claude-opus-4-1' in _model_lower) and (
|
||||
'temperature' in kwargs and 'top_p' in kwargs
|
||||
):
|
||||
# Apply to Opus 4.1 and Sonnet 4 models to avoid API errors
|
||||
if (
|
||||
('claude-opus-4-1' in _model_lower) or ('claude-sonnet-4' in _model_lower)
|
||||
) and ('temperature' in kwargs and 'top_p' in kwargs):
|
||||
kwargs.pop('top_p', None)
|
||||
|
||||
# Add completion_kwargs if present
|
||||
|
||||
@@ -1255,6 +1255,25 @@ def test_opus_41_keeps_temperature_top_p(mock_completion):
|
||||
assert 'top_p' not in call_kwargs
|
||||
|
||||
|
||||
@patch('openhands.llm.llm.litellm_completion')
|
||||
def test_sonnet_4_keeps_temperature_drops_top_p(mock_completion):
|
||||
mock_completion.return_value = {
|
||||
'choices': [{'message': {'content': 'ok'}}],
|
||||
}
|
||||
config = LLMConfig(
|
||||
model='anthropic/claude-sonnet-4-20250514',
|
||||
api_key='k',
|
||||
temperature=0.7,
|
||||
top_p=0.9,
|
||||
)
|
||||
llm = LLM(config, service_id='svc')
|
||||
llm.completion(messages=[{'role': 'user', 'content': 'hi'}])
|
||||
call_kwargs = mock_completion.call_args[1]
|
||||
assert call_kwargs.get('temperature') == 0.7
|
||||
# Anthropic rejects both temperature and top_p together on Sonnet 4; we keep temperature and drop top_p
|
||||
assert 'top_p' not in call_kwargs
|
||||
|
||||
|
||||
@patch('openhands.llm.llm.litellm_completion')
|
||||
def test_opus_4_keeps_temperature_top_p(mock_completion):
|
||||
mock_completion.return_value = {
|
||||
|
||||
Reference in New Issue
Block a user