Skip to content

fix(models): fall back to 4096 max_tokens for default Anthropic config - #4373

Open
BlueX888 wants to merge 1 commit into
camel-ai:masterfrom
BlueX888:fix/prep-anthropic-max-tokens-none-default-config
Open

BlueX888 wants to merge 1 commit into
camel-ai:masterfrom
BlueX888:fix/prep-anthropic-max-tokens-none-default-config

Conversation

@BlueX888

Copy link
Copy Markdown

Related Issue

Closes #4370

Description

Summary

AnthropicModel sends max_tokens=None whenever the user config omits max_tokens, so every call made with the default AnthropicConfig fails with a 400 invalid_request_error from the Anthropic API.

Why (root cause)

PR #3874 changed the max_tokens fallback in AnthropicModel._run/_arun from 4096 to None (camel/models/anthropic_model.py:1156 and camel/models/anthropic_model.py:1301). max_tokens is a required parameter of the Anthropic Messages API: in the pinned anthropic 0.89.0 SDK, messages.create() declares max_tokens: int and enforces it with @required_args, and an explicit None is serialized into the request body as "max_tokens": null rather than being omitted. With the default AnthropicConfig, whose as_dict() drops None values (camel/configs/base_config.py:86), model_config_dict is {}, so the None fallback always applies and every default-config request is rejected.

The goal of #3874 — not capping responses when the user configures their own max_tokens — is preserved: an explicitly configured max_tokens is still passed through unchanged. Only the unset case falls back to an integer again (4096, the value used before #3874 and valid for every Claude model).

Changes

  • camel/models/anthropic_model.py: restore the integer fallback for max_tokens in _run and _arun.
  • test/models/test_anthropic_model.py: add test_run_default_config_sends_int_max_tokens and test_arun_default_config_sends_int_max_tokens, which construct AnthropicModel with the default config (empty model_config_dict) and mock clients, and assert the outgoing request carries an int max_tokens.

Testing (Python 3.12.14, anthropic 0.89.0, pytest 7.4.4)

New regression tests on the unpatched tree (fail as expected):

$ .venv/bin/python -m pytest test/models/test_anthropic_model.py -k "default_config_sends_int_max_tokens"
>       assert isinstance(request_kwargs["max_tokens"], int)
E       assert False
E        +  where False = isinstance(None, int)
FAILED test/models/test_anthropic_model.py::test_run_default_config_sends_int_max_tokens
FAILED test/models/test_anthropic_model.py::test_arun_default_config_sends_int_max_tokens
======================= 2 failed, 54 deselected in 6.02s =======================

With the fix:

$ .venv/bin/python -m pytest test/models/test_anthropic_model.py
============================== 56 passed in 0.93s ==============================

$ .venv/bin/python -m pytest --fast-test-mode test/models/test_anthropic_model.py
46 passed, 10 skipped in 1.63s

$ .venv/bin/python -m pytest test/models/ --ignore=test/models/test_gemini_model.py --ignore=test/models/reward/test_nemotron_model.py
175 failed, 384 passed, 10 skipped

(The two ignored files fail collection in this environment for missing optional heavy dependencies google-genai/torch; the 175 failures are pre-existing "Missing API key" errors unrelated to this change — the same run on the unpatched tree gives 177 failed / 382 passed, the difference being exactly the two new tests.)

$ .venv/bin/python -m ruff check --no-fix camel/models/anthropic_model.py test/models/test_anthropic_model.py
All checks passed!

$ .venv/bin/python -m ruff format --check camel/models/anthropic_model.py test/models/test_anthropic_model.py
2 files already formatted

$ .venv/bin/python -m mypy --namespace-packages camel/models/anthropic_model.py test/models/test_anthropic_model.py
Success: no issues found in 2 source files

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Checklist

max_tokens is a required parameter of the Anthropic Messages API, but
the fallback in AnthropicModel._run/_arun was changed from 4096 to None.
With the default AnthropicConfig (whose as_dict drops None values),
model_config_dict is empty, so requests were sent with max_tokens=None,
which the API rejects with a 400 invalid_request_error. Restore an
integer fallback so AnthropicModel works out of the box.
@coderabbitai

coderabbitai Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 14a957cd-2c96-4205-8fa1-02b85b6369ac

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant