{"record":{"id":"51c53f5d4c3ca08e","repo":"langchain-ai/deepagents","slug":"context-must-be-a-json-object","errorCode":null,"errorMessage":"context must be a JSON object.","messagePattern":"context must be a JSON object\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/code/deepagents_code/offload_api.py","lineNumber":525,"sourceCode":"\n    Returns:\n        Operation id, runtime context, and accumulated hook responses.\n\n    Raises:\n        TypeError: If the payload or a structured field has the wrong shape.\n    \"\"\"\n    if not isinstance(payload, dict):\n        msg = \"Offload request must be a JSON object.\"\n        raise TypeError(msg)\n    operation_id = payload.get(\"operation_id\")\n    context = payload.get(\"context\")\n    responses = payload.get(\"hook_responses\", {})\n    if not isinstance(operation_id, str) or not operation_id:\n        msg = \"operation_id must be a non-empty string.\"\n        raise TypeError(msg)\n    if not isinstance(context, dict):\n        msg = \"context must be a JSON object.\"\n        raise TypeError(msg)\n    if not isinstance(responses, dict):\n        msg = \"hook_responses must be a JSON object.\"\n        raise TypeError(msg)\n    validated_context = {str(key): value for key, value in context.items()}\n    _validate_context(validated_context)\n    return (\n        operation_id,\n        _strip_transport_model_params(validated_context),\n        {str(key): value for key, value in responses.items()},\n    )\n\n\ndef _hydrate_state(values: object) -> _OffloadState:\n    \"\"\"Hydrate serialized checkpoint messages for the compaction service.\n\n    Args:\n        values: State values returned by LangGraph Server.\n","sourceCodeStart":507,"sourceCodeEnd":543,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/code/deepagents_code/offload_api.py#L507-L543","documentation":"The request's `context` field must be a JSON object (dict); it carries per-operation settings like model_context_limit, auto_approve, and hooks_server_events that get validated next by _validate_context. A non-dict context (list, string, null, missing -> None) is rejected before those per-field checks.","triggerScenarios":"Calling offload() with payload={'operation_id': 'x'} (context omitted -> None), {'context': []}, {'context': 'model_context_limit=1'}, or any non-dict object.","commonSituations":"Mistakenly passing a list of context entries, serializing context to a JSON string before embedding it, an optional-context caller passing None where the API requires at least an empty object, or a key-name typo putting settings at the wrong level.","solutions":["Pass {} when there is no context instead of omitting the key or passing None.","If context is a JSON string, decode it: context = json.loads(context_str) and confirm it is a dict.","Ensure settings sit under the context key, not at the payload top level (typo check).","Default at construction: payload['context'] = context or {}."],"exampleFix":"// before\npayload = {\"operation_id\": \"op1\"}  # context missing -> None\n// after\npayload = {\"operation_id\": \"op1\", \"context\": {}}","handlingStrategy":"validation","validationCode":"def ensure_context(payload):\n    if not isinstance(payload.get(\"context\"), dict):\n        payload[\"context\"] = {}\n    return payload","typeGuard":"def has_valid_context(payload) -> bool:\n    return isinstance(payload.get(\"context\"), dict)","tryCatchPattern":"try:\n    state = await offload(thread_id, payload)\nexcept TypeError as exc:\n    if str(exc) == \"context must be a JSON object.\":\n        payload[\"context\"] = {}\n        state = await offload(thread_id, payload)\n    else:\n        raise","preventionTips":["Always include context as at least {} in offload payloads","If context may arrive as JSON text, decode it and assert it is a dict","Keep per-operation settings under context, not at the payload top level","Default with payload.setdefault(\"context\", {})"],"tags":["type-error","validation","payload","json"],"backgroundTag":"invalid-json-payload","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}