langchain-ai/deepagents · error · ValueError

cli_max_retries must be >= 0, got {self.cli_max_retries}

Error message

cli_max_retries must be >= 0, got {self.cli_max_retries}

What it means

`ModelResult.__post_init__` raising ValueError because `cli_max_retries` is a non-None negative int. The `--max-retries` flag value must be zero or positive; None means 'not set'.

Source

Thrown at libs/code/deepagents_code/config.py:5581

        Raises:
            TypeError: If `model_retries` or `cli_max_retries` is a `bool`.
            ValueError: If `model_retries` or `cli_max_retries` is negative.
        """
        if isinstance(self.model_retries, bool):
            msg = f"model_retries must be an int, got {self.model_retries!r}"
            raise TypeError(msg)
        if self.model_retries < 0:
            msg = f"model_retries must be >= 0, got {self.model_retries}"
            raise ValueError(msg)
        if isinstance(self.cli_max_retries, bool):
            msg = (
                f"cli_max_retries must be None or an int, got {self.cli_max_retries!r}"
            )
            raise TypeError(msg)
        if self.cli_max_retries is not None and self.cli_max_retries < 0:
            msg = f"cli_max_retries must be >= 0, got {self.cli_max_retries}"
            raise ValueError(msg)

    def apply_to_runtime_state(self) -> None:
        """Commit this result's metadata to global `runtime_state`."""
        state = _get_runtime_state()
        state.model_name = self.model_name
        state.model_provider = self.provider
        state.model_context_limit = self.context_limit
        state.model_unsupported_modalities = self.unsupported_modalities


def _apply_profile_overrides(
    model: BaseChatModel,
    overrides: dict[str, Any],
    model_name: str,
    *,
    label: str,
    raise_on_failure: bool = False,
) -> None:

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Clamp before construction: `max(0, value)`.
  2. Pass None when the flag was not set instead of a negative default.
  3. Fix the producer of the value (config/CLI plumbing) to enforce non-negativity.

Example fix

// before
ModelResult(..., cli_max_retries=-2)
// after
ModelResult(..., cli_max_retries=max(0, requested_retries))
Defensive patterns

Strategy: validation

Validate before calling

if cli_max_retries is not None and cli_max_retries < 0:
    cli_max_retries = 0  # clamp before ModelResult(...)

Try / catch

try:
    result = ModelResult(..., cli_max_retries=v)
except ValueError:
    result = ModelResult(..., cli_max_retries=max(0, v))

Prevention

When it happens

Trigger: Constructing `ModelResult(...)` with a negative `cli_max_retries` (e.g. -1). Argparse's `non_negative_int` normally blocks this at the CLI, so it indicates a value injected programmatically or via config plumbing.

Common situations: Programmatic re-resolution for a different provider passing along a badly computed flag value, tests with sentinel negatives, config parsers accepting negative numbers.

Related errors


AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29). Data as JSON: /api/errors/acffb658b3b19a53. Report an issue: GitHub.