langchain-ai/deepagents · error · TypeError

context.model_context_limit must be an integer or null, got

Error message

context.model_context_limit must be an integer or null, got {type(limit).__name__}.

What it means

The offload API validates each field of the request's `context` object before executing an operation. `context.model_context_limit` (used for summarization token budgets) must be an int or null; bool is explicitly rejected even though Python bools are ints, because true/false is never a valid token limit. The library raises TypeError early so malformed context never reaches the thread server.

Source

Thrown at libs/code/deepagents_code/offload_api.py:474

    for key in _CONTEXT_STR_OR_NONE_FIELDS:
        value = context.get(key)
        if value is not None and not isinstance(value, str):
            msg = f"context.{key} must be a string or null, got {type(value).__name__}."
            raise TypeError(msg)
    for key in _CONTEXT_DICT_FIELDS:
        value = context.get(key)
        if value is not None and not isinstance(value, dict):
            msg = f"context.{key} must be an object, got {type(value).__name__}."
            raise TypeError(msg)
    limit = context.get("model_context_limit")
    # bool is an int subclass, so exclude it explicitly: JSON `true` is not a
    # token limit.
    if limit is not None and (isinstance(limit, bool) or not isinstance(limit, int)):
        msg = (
            "context.model_context_limit must be an integer or null, "
            f"got {type(limit).__name__}."
        )
        raise TypeError(msg)
    auto_approve = context.get("auto_approve")
    if auto_approve is not None and not isinstance(auto_approve, bool):
        msg = (
            f"context.auto_approve must be a boolean or null, "
            f"got {type(auto_approve).__name__}."
        )
        raise TypeError(msg)
    events = context.get("hooks_server_events")
    if events is not None and (
        not isinstance(events, list)
        or any(not isinstance(event, str) for event in events)
    ):
        msg = "context.hooks_server_events must be a list of strings or null."
        raise TypeError(msg)


def _checkpoint_id(state: Mapping[str, object]) -> str:
    checkpoint = state.get("checkpoint")

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Convert the value to int explicitly before building context: int(value) (guard against bool first).
  2. If it comes from an env var or CLI arg, parse it with int(raw) inside try/except ValueError and default to None on failure.
  3. If the setting is optional, omit the key entirely or pass None instead of a non-numeric placeholder.
  4. Verify you are not passing a boolean toggle (e.g. a summary-enabled flag) into model_context_limit by mistake.

Example fix

// before
context = {"model_context_limit": os.environ["MODEL_LIMIT"]}
// after
raw = os.environ.get("MODEL_LIMIT")
context = {"model_context_limit": int(raw) if raw not in (None, "") else None}
Defensive patterns

Strategy: validation

Validate before calling

def validate_model_context_limit(value):
    if value is None:
        return True
    return isinstance(value, int) and not isinstance(value, bool)
# call before offload: assert validate_model_context_limit(ctx.get("model_context_limit"))

Type guard

def is_int_or_none(value) -> bool:
    return value is None or (isinstance(value, int) and not isinstance(value, bool))

Try / catch

try:
    state = await offload(thread_id, payload)
except TypeError as exc:
    if "model_context_limit" in str(exc):
        payload["context"]["model_context_limit"] = None  # or int(...)
        state = await offload(thread_id, payload)
    else:
        raise

Prevention

When it happens

Trigger: Calling offload() (via _operation_payload -> _validate_context) with context={'model_context_limit': '200000'} (string), 200000.5 (float), True/False (bool), or any non-int object.

Common situations: Reading the limit from an env var or CLI flag (which yields strings), parsing JSON config where the value was quoted, computing the limit with division producing a float, or accidentally passing a boolean flag in the limit field.

Related errors


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