HKUDS/nanobot · error · TypeError
must be a string
Error message
{field} must be a string What it means
Raised by _require_json_string when a field that must be text (e.g. messages[0].content[].text or image_url.url) is not a str in the untrusted JSON body of the OpenAI-compatible API. Guards against null/number/object values where prose or a URL is required.
Solutions
- Send the field as a JSON string
- Fix null or nested-object values in the request payload
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at nanobot/api/server.py:146 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/e61145216c3ea07d.
Report an issue: GitHub.
Appendix: source
Thrown at nanobot/api/server.py:146
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 = {
"id": chunk_id,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model,
"choices": [
{
"index": 0,View on GitHub (pinned to 42f37dc4c0)