Skip to content

fix(anthropic): coerce max_tokens to int to fix validation error (#1519) - #1787

Open
carlosjarenom wants to merge 8 commits into
eigent-ai:mainfrom
carlosjarenom:fix/anthropic-max-tokens-1519
Open

fix(anthropic): coerce max_tokens to int to fix validation error (#1519)#1787
carlosjarenom wants to merge 8 commits into
eigent-ai:mainfrom
carlosjarenom:fix/anthropic-max-tokens-1519

Conversation

@carlosjarenom

Copy link
Copy Markdown
Contributor

Changes

  • backend/app/agent/agent_model.py: Added coercion of max_tokens to integer for Anthropic models.

Root Cause

max_tokens can arrive as a string from:

  • Environment variables (all env vars are strings)
  • JSON config from frontend
  • Legacy extra_params

Anthropic API requires max_tokens to be an integer, causing 400 error:

Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'max_tokens: Input should be a valid integer'}}

Fix

Added type coercion with fallback to default (128000) on invalid values:

if "max_tokens" in model_config and isinstance(model_config["max_tokens"], str):
    try:
        model_config["max_tokens"] = int(model_config["max_tokens"])
    except (ValueError, TypeError):
        logger.warning(...)
        model_config["max_tokens"] = 128000

Fixes

…igent-ai#1511)

Groq's API requires strict JSON schemas with 'additionalProperties: false'
on every object type, including nested objects in anyOf/oneOf arrays.
This caused 400 errors for browser_type tool and others.

CAMEL's sanitize_and_enforce_required() already adds this, but we apply
it as a safety net when tools are passed to the agent model.

Fixes: eigent-ai#1511
…ent-ai#1519)

max_tokens can arrive as string from env vars or JSON config.
Anthropic API requires integer, causing 400 'Input should be a valid integer'.

Added coercion with fallback to default 128000 on invalid values.
@4pmtong

4pmtong commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the contribution and for looking into these issues. @carlosjarenom

I found a few blocking points:

  • The Groq change is outside this PR’s description. Also, [BUG] Groq API incompatibility with Browser Agent tool schema #1511 uses Groq through the OpenAI-compatible provider, so the model_platform == "groq" check will not run. On the current CAMEL version, the browser_type schema is already strict. Please remove this change or submit it separately with a failing regression test.
  • The Anthropic conversion only runs when creating the runtime agent. The model settings flow calls /model/validate first, where a string max_tokens can still fail. Please use shared normalization for both runtime and validation.
  • Please do not silently replace an invalid value with 128000, since token limits differ between Anthropic models. Validation should report invalid values clearly, and any runtime fallback should be conservative.
  • Please add regression tests for both the validation and runtime paths.

We’ll keep this PR open for now. If you have time to update it, we’ll be happy to review it again.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants