infiniflow/ragflow · error · TypeError

Querit API response must be a JSON object.

Error message

Querit API response must be a JSON object.

What it means

TypeError raised by the Querit search tool when the parsed JSON body of the Querit search API response is not a JSON object (dict) — e.g. the endpoint returned a JSON array, string, number, or null. It is the first of three shape checks applied to response_data before results are extracted.

Source

Thrown at agent/tools/querit.py:170

        values = {
            name: kwargs[name] if name in kwargs else getattr(self._param, name)
            for name in (
                "count",
                "chunks_per_doc",
                "site_include",
                "site_exclude",
                "time_range",
                "country_include",
                "language_include",
            )
        }

        try:
            _validate_search_inputs(**values)
            payload = _build_payload(query, **values)
            response_data = self._search(payload, api_key)
            if not isinstance(response_data, dict):
                raise TypeError("Querit API response must be a JSON object.")

            result_container = response_data.get("results", {})
            if not isinstance(result_container, dict):
                raise TypeError("Querit API response field results must be an object.")
            results = result_container.get("result", [])
            if not isinstance(results, list):
                raise TypeError("Querit API response field results.result must be an array.")

            reference_results = [item for item in results if isinstance(item, dict)]
            if reference_results:
                self._retrieve_chunks(
                    reference_results,
                    get_title=lambda item: _querit_text(item.get("title")),
                    get_url=lambda item: _querit_text(item.get("url")),
                    get_content=lambda item: _querit_text(item.get("snippet")),
                    get_score=lambda _item: 1,
                )
            else:

View on GitHub (pinned to 554fb1133a)

Solutions

  1. Replay the request with curl and inspect the raw body — confirm it starts with '{'.
  2. If the API now returns a top-level array, wrap or adapt it to the documented {"results": {"result": [...]}} envelope (or pin the API version you coded against).
  3. If the body is an HTML/text error from a gateway, fix the upstream failure (502/503) rather than the client.
  4. In tests, fix fixtures/mocks to return an object literal.

Example fix

# before (mock returning an array)
responses.add(responses.POST, "https://api.querit.com/search", json=[{"title": "x"}])

# after
responses.add(responses.POST, "https://api.querit.com/search", json={"results": {"result": [{"title": "x"}]}})
Defensive patterns

Strategy: type-guard

Validate before calling

import json
from typing import Any
def querit_search_response_ok(body: str) -> bool:
    try:
        data = json.loads(body)
    except json.JSONDecodeError:
        return False
    return isinstance(data, dict)

Type guard

def is_querit_search_payload(v: Any) -> bool:
    return (
        isinstance(v, dict)
        and isinstance(v.get("results", {}), dict)
        and isinstance(v["results"].get("result", []), list)
    )

Try / catch

try:
    out = querit_search._invoke(query=q)
except TypeError as e:
    if "JSON object" in str(e):
        log.error("Querit contract violation: %s", e); raise
    raise

Prevention

When it happens

Trigger: Querit API replying with a bare JSON array (e.g. a legacy /results endpoint), an HTML error page that happened to parse as a JSON scalar, or a proxy returning 'null' on a 502. Any self._search() return value where isinstance(response_data, dict) is False triggers it.

Common situations: Querit backend version drift changing the response envelope from {"results": ...} to a top-level list; API gateways converting errors into JSON strings like "Service Unavailable"; mocking the endpoint in tests with json=[...] instead of json={...}.

Related errors


AI-assisted analysis of infiniflow/ragflow@554fb1133a (2026-08-15). Data as JSON: /api/errors/05158ba1f525e4bf. Report an issue: GitHub.