langchain-ai/deepagents · error · TypeError

model_retries must be an int, got {self.model_retries!r}

Error message

model_retries must be an int, got {self.model_retries!r}

What it means

`ModelResult.__post_init__` validation raising TypeError because `model_retries` is a bool. Bools are rejected because `True` would silently read as a retry budget of 1 (`bool` is an `int` subclass), hiding a caller bug.

Source

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

        the retry resolver itself, so a bad value here signals a caller
        constructing `ModelResult` by hand with a budget the retry middleware
        could not honor. `bool` is rejected for the same reason
        `_model_max_retries` and `CodeModelRetryMiddleware.__init__` reject it:
        `True` would silently read as a budget of one.

        `cli_max_retries` gets the same gate. It is the field that carries the
        explicit flag onward to a re-resolution for a different provider, and
        it was the one budget field in dcode without the check -- which is the
        argument for a single validated budget type rather than a ninth copy of
        this predicate.

        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

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Pass an integer (e.g. `model_retries=3`), not True/False.
  2. If the value comes from config, coerce it with an int converter that rejects bools before constructing ModelResult.
  3. Use `0` to explicitly disable retries instead of `False`.

Example fix

// before
ModelResult(model=m, model_name="claude-sonnet-4-5", provider="anthropic", model_retries=True)
// after
ModelResult(model=m, model_name="claude-sonnet-4-5", provider="anthropic", model_retries=1)
Defensive patterns

Strategy: type-guard

Validate before calling

def is_retry_budget(v: object) -> bool:
    return isinstance(v, int) and not isinstance(v, bool) and v >= 0
assert is_retry_budget(model_retries)

Type guard

def is_int_not_bool(v: object) -> TypeGuard[int]:
    return isinstance(v, int) and not isinstance(v, bool)

Try / catch

try:
    result = ModelResult(..., model_retries=retries)
except TypeError:
    retries = int(retries) if not isinstance(retries, bool) else DEFAULT_MODEL_RETRIES
    result = ModelResult(..., model_retries=retries)

Prevention

When it happens

Trigger: Constructing `ModelResult(...)` by hand with `model_retries=True` or `model_retries=False` instead of an integer. Normal config paths coerce via `non_negative_int`/`_coerce_max_retries`, so this only fires on direct construction.

Common situations: Programmatic construction of ModelResult in tests or custom tooling, config values parsed from TOML/YAML as booleans passed through uncoerced.

Related errors


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