Skip to content

fix: avoid sending both temperature and top_p to Anthropic API#4471

Merged
whysosaket merged 2 commits intomainfrom
fix/anthropic-default-top-p
Mar 23, 2026
Merged

fix: avoid sending both temperature and top_p to Anthropic API#4471
whysosaket merged 2 commits intomainfrom
fix/anthropic-default-top-p

Conversation

@utkarsh240799
Copy link
Copy Markdown
Contributor

Description

Anthropic's API rejects requests that include both temperature and top_p with:

anthropic.BadRequestError: `temperature` and `top_p` cannot both be specified for this model.

This PR fixes the default Anthropic LLM configuration so it works out of the box:

  • Default top_p to None in AnthropicConfig instead of 0.1, so it is not sent alongside temperature
  • Override _get_common_params() in AnthropicLLM to prefer temperature and drop top_p when both are set (handles the BaseLlmConfig conversion path where top_p=0.1 is inherited)

The fix is scoped entirely to the Anthropic provider — no other providers are affected.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Added tests/llms/test_anthropic.py with 5 test cases:

Test What it verifies
test_default_config_omits_top_p AnthropicConfig() defaults top_p to None
test_generate_response_does_not_send_top_p_by_default API call includes only temperature, not top_p
test_generate_response_sends_top_p_alone_when_no_temperature When only top_p is set (temperature=None), top_p is sent
test_both_set_prefers_temperature_over_top_p When both are explicitly set, temperature wins and top_p is dropped
test_base_config_conversion_does_not_send_both BaseLlmConfig (which defaults both) converted to AnthropicConfig does not send both
pytest tests/llms/test_anthropic.py -v
# 5 passed

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Maintainer Checklist

🤖 Generated with Claude Code

Anthropic rejects requests with both temperature and top_p set. Default
top_p to None in AnthropicConfig and override _get_common_params to
prefer temperature when both are present.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@whysosaket
Copy link
Copy Markdown
Member

Tests are failing

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@utkarsh240799
Copy link
Copy Markdown
Contributor Author

@themavik Thanks for the quick review!

It's called indirectly through the inheritance chain.
generate_response() # anthropic.py:85
→ self._get_supported_params() # base.py:67 (inherited)
→ self._get_common_params() # base.py:95 calls this
→ AnthropicLLM._get_common_params() # our method overrides base class version

@whysosaket whysosaket merged commit bda5b72 into main Mar 23, 2026
8 checks passed
@whysosaket whysosaket deleted the fix/anthropic-default-top-p branch March 23, 2026 15:30
jamebobob pushed a commit to jamebobob/mem0-vigil-recall that referenced this pull request Mar 29, 2026
…i#4471)

Co-authored-by: utkarsh240799 <utkarsh240799@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

Default LLM config is not working for Anthropic

2 participants