HKUDS/nanobot · error · TypeError

must be an object

Error message

{field} must be an object

What it means

Raised by _require_json_object (surfaced through request parsing in handle_chat_completions) when a field expected to be a JSON object is another type — e.g. messages[0].content or an image_url block is a string/array instead of a dict. It is the untrusted-payload shape guard for the OpenAI-compatible API.

Solutions

  1. Send the field as a JSON object (e.g. content as an array of typed parts where required)
  2. Validate request shape at the client and fix the payload structure
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at nanobot/api/server.py:139 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of HKUDS/nanobot@42f37dc4c0 (2026-08-26). Data as JSON: /api/errors/0495f99415907188. Report an issue: GitHub.

Appendix: source

Thrown at nanobot/api/server.py:139

def _response_text(value: Any) -> str:
    """Normalize process_direct output to plain assistant text."""
    if value is None:
        return ""
    if hasattr(value, "content"):
        return str(getattr(value, "content") or "")
    return str(value)


def _as_str(value: object) -> str:
    """Return *value* when it is text, otherwise an empty string."""
    return value if isinstance(value, str) else ""


def _require_json_object(value: object, field: str) -> dict[str, Any]:
    """Validate an object-valued field from an untrusted JSON request."""
    if not isinstance(value, dict):
        raise TypeError(f"{field} must be an object")
    return cast(dict[str, Any], value)


def _require_json_string(value: object, field: str) -> str:
    """Validate a string-valued field from an untrusted JSON request."""
    if not isinstance(value, str):
        raise TypeError(f"{field} must be a string")
    return value


# ---------------------------------------------------------------------------
# SSE helpers
# ---------------------------------------------------------------------------


def _sse_chunk(delta: str, model: str, chunk_id: str, finish_reason: str | None = None) -> bytes:
    """Format a single OpenAI-compatible SSE chunk."""
    payload = {

View on GitHub (pinned to 42f37dc4c0)