{"record":{"id":"02cf6d75b07fe0b4","repo":"langchain-ai/deepagents","slug":"context-key-must-be-a-string-or-null-got-type","errorCode":null,"errorMessage":"context.{key} must be a string or null, got {type(value).__name__}.","messagePattern":"context\\.(.+?) must be a string or null, got (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/code/deepagents_code/offload_api.py","lineNumber":460,"sourceCode":"\n\ndef _validate_context(context: dict[str, Any]) -> None:\n    \"\"\"Check the context fields the offload operation consumes.\n\n    Only the listed keys are type-checked; unknown keys pass through so a\n    newer client can keep talking to this server version.\n\n    Args:\n        context: The request's `context` object.\n\n    Raises:\n        TypeError: If a consumed field has the wrong type, naming the field.\n    \"\"\"\n    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, \"","sourceCodeStart":442,"sourceCodeEnd":478,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/code/deepagents_code/offload_api.py#L442-L478","documentation":"`_validate_context` enforces that each field in `_CONTEXT_STR_OR_NONE_FIELDS` is either a string or null in the operation's `context` object. Any other type raises a `TypeError` naming `context.<key>` and the offending type, before the offload operation payload is built.","triggerScenarios":"Calling an offload API operation with `context={..., \"summarization_model\": 42}` (or a list/bool) where a string-or-null field is expected, e.g. a numeric model name or an unconverted config value.","commonSituations":"JSON/YAML config with the model id accidentally as a number; passing a Python object instead of its string repr; deserialization producing ints where strings are required.","solutions":["Pass the field as a string: `\"summarization_model\": \"gpt-4o-mini\"`, or omit/null it","Convert at the boundary with `str(value)` when the value is known to be name-like","Validate the context dict before calling the API"],"exampleFix":"// before\ncontext = {\"summarization_model\": 4}\n// after\ncontext = {\"summarization_model\": str(4) if not isinstance(4, str) else 4}","handlingStrategy":"validation","validationCode":"for k in (\"summarization_model\",):\n    v = context.get(k)\n    if v is not None and not isinstance(v, str):\n        raise TypeError(f\"context.{k} must be a string or null\")","typeGuard":"def is_str_or_none(v: object) -> TypeGuard[str | None]:\n    return v is None or isinstance(v, str)","tryCatchPattern":"try:\n    payload = _operation_payload(op, context)\nexcept TypeError as e:\n    logging.error(\"invalid context: %s\", e)\n    raise","preventionTips":["Coerce config values to strings at load time","Validate the context dict against a schema before calling the API","Keep model ids quoted in JSON/YAML configs"],"tags":["python","validation","type-error","context","offload"],"backgroundTag":"schema-validation-failed","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}