{"record":{"id":"4dbdc61e6b31d8c2","repo":"langchain-ai/deepagents","slug":"context-model-context-limit-must-be-an-integer-or","errorCode":null,"errorMessage":"context.model_context_limit must be an integer or null, got {type(limit).__name__}.","messagePattern":"context\\.model_context_limit must be an integer or null, got (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/code/deepagents_code/offload_api.py","lineNumber":474,"sourceCode":"    for key in _CONTEXT_STR_OR_NONE_FIELDS:\n        value = context.get(key)\n        if value is not None and not isinstance(value, str):\n            msg = f\"context.{key} must be a string or null, got {type(value).__name__}.\"\n            raise TypeError(msg)\n    for key in _CONTEXT_DICT_FIELDS:\n        value = context.get(key)\n        if value is not None and not isinstance(value, dict):\n            msg = f\"context.{key} must be an object, got {type(value).__name__}.\"\n            raise TypeError(msg)\n    limit = context.get(\"model_context_limit\")\n    # bool is an int subclass, so exclude it explicitly: JSON `true` is not a\n    # token limit.\n    if limit is not None and (isinstance(limit, bool) or not isinstance(limit, int)):\n        msg = (\n            \"context.model_context_limit must be an integer or null, \"\n            f\"got {type(limit).__name__}.\"\n        )\n        raise TypeError(msg)\n    auto_approve = context.get(\"auto_approve\")\n    if auto_approve is not None and not isinstance(auto_approve, bool):\n        msg = (\n            f\"context.auto_approve must be a boolean or null, \"\n            f\"got {type(auto_approve).__name__}.\"\n        )\n        raise TypeError(msg)\n    events = context.get(\"hooks_server_events\")\n    if events is not None and (\n        not isinstance(events, list)\n        or any(not isinstance(event, str) for event in events)\n    ):\n        msg = \"context.hooks_server_events must be a list of strings or null.\"\n        raise TypeError(msg)\n\n\ndef _checkpoint_id(state: Mapping[str, object]) -> str:\n    checkpoint = state.get(\"checkpoint\")","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/code/deepagents_code/offload_api.py#L456-L492","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Convert the value to int explicitly before building context: int(value) (guard against bool first).","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.","If the setting is optional, omit the key entirely or pass None instead of a non-numeric placeholder.","Verify you are not passing a boolean toggle (e.g. a summary-enabled flag) into model_context_limit by mistake."],"exampleFix":"// before\ncontext = {\"model_context_limit\": os.environ[\"MODEL_LIMIT\"]}\n// after\nraw = os.environ.get(\"MODEL_LIMIT\")\ncontext = {\"model_context_limit\": int(raw) if raw not in (None, \"\") else None}","handlingStrategy":"validation","validationCode":"def validate_model_context_limit(value):\n    if value is None:\n        return True\n    return isinstance(value, int) and not isinstance(value, bool)\n# call before offload: assert validate_model_context_limit(ctx.get(\"model_context_limit\"))","typeGuard":"def is_int_or_none(value) -> bool:\n    return value is None or (isinstance(value, int) and not isinstance(value, bool))","tryCatchPattern":"try:\n    state = await offload(thread_id, payload)\nexcept TypeError as exc:\n    if \"model_context_limit\" in str(exc):\n        payload[\"context\"][\"model_context_limit\"] = None  # or int(...)\n        state = await offload(thread_id, payload)\n    else:\n        raise","preventionTips":["Parse limits from env/CLI with int() and fail fast on ValueError","Never pass bools as numeric settings; keep flags and limits in separate fields","Validate context dicts with a shared helper before any offload call","Prefer omitting optional keys over placeholder non-numeric values"],"tags":["type-error","validation","python","offload-api"],"backgroundTag":"type-validation-failed","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}