langchain-ai/deepagents · error · TypeError

context.{key} must be an object, got {type(value).__name__}.

Error message

context.{key} must be an object, got {type(value).__name__}.

What it means

`_validate_context` enforces that each field in `_CONTEXT_DICT_FIELDS` is either an object (dict) or null. Any other type raises a `TypeError` naming `context.<key>` and the received type, since these context fields hold nested JSON objects.

Source

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

    Only the listed keys are type-checked; unknown keys pass through so a
    newer client can keep talking to this server version.

    Args:
        context: The request's `context` object.

    Raises:
        TypeError: If a consumed field has the wrong type, naming the field.
    """
    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 (

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Pass an actual dict: `json.loads(raw)` instead of the raw JSON string
  2. Null the field out if no nested data is needed
  3. Validate the context structure before invoking the API

Example fix

// before
context = {"metadata": '{"run": "abc"}'}
// after
import json
context = {"metadata": json.loads('{"run": "abc"}')}
Defensive patterns

Strategy: validation

Validate before calling

for k in context_dict_fields:
    v = context.get(k)
    if v is not None and not isinstance(v, dict):
        raise TypeError(f"context.{k} must be an object")

Type guard

def is_dict_or_none(v: object) -> TypeGuard[dict | None]:
    return v is None or isinstance(v, dict)

Try / catch

try:
    payload = _operation_payload(op, context)
except TypeError as e:
    logging.error("invalid context: %s", e)
    raise

Prevention

When it happens

Trigger: Calling an offload API operation with a dict-typed context field set to a string, list, or number, e.g. `context={"metadata": "[]"}` or a JSON-encoded string passed instead of the parsed object.

Common situations: Passing a JSON string that was never `json.loads`-ed into a dict; config files where a nested object was flattened to a string; lists supplied where mappings are required.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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