TencentCloud/TencentDB-Agent-Memory · error · TDAMError

<resp.text or "HTTP {resp.status_code} returned a non-JSON r

Error message

<resp.text or "HTTP {resp.status_code} returned a non-JSON response">

What it means

_decode_response in the Python SDK expects the API to return a JSON envelope; when resp.json() raises ValueError (invalid or empty body), it raises TDAMError with the server's HTTP status (or -1 if the response was not an HTTP error) and the raw body text as the message, falling back to a generic non-JSON description. This surfaces gateway/proxy garbage responses instead of crashing on a parse error.

Source

Thrown at sdk/memory-core/python/tencentdb_agent_memory/_v3_http.py:48

    if not api_key or not api_key.strip():
        raise ParamError("api_key must be provided")
    if not service_id or not service_id.strip():
        raise ParamError("service_id must be provided")
    if isinstance(timeout, bool) or not isinstance(timeout, (int, float)) or timeout <= 0:
        raise ParamError("timeout must be a positive number")


def _decode_response(resp: httpx.Response) -> dict:
    header_request_id = (
        resp.headers.get("x-qcloud-transaction-id")
        or resp.headers.get("x-trace-id")
        or ""
    )
    try:
        envelope = resp.json()
    except ValueError as exc:
        message = resp.text or f"HTTP {resp.status_code} returned a non-JSON response"
        raise TDAMError(resp.status_code if resp.is_error else -1, message, header_request_id) from exc

    if not isinstance(envelope, dict):
        raise TDAMError(-1, "API response must be a JSON object", header_request_id)

    code = envelope.get("code")
    if resp.is_error or code != 0:
        effective_code = code if isinstance(code, int) and code != 0 else resp.status_code
        payload = envelope.get("data")
        details = payload if isinstance(payload, dict) else None
        raise TDAMError(
            code=effective_code,
            message=str(envelope.get("message") or f"HTTP {resp.status_code}"),
            request_id=str(envelope.get("request_id") or header_request_id),
            details=details,
        )

    result = envelope.get("data") or {}
    if not isinstance(result, dict):

View on GitHub (pinned to 3efcd317b8)

Solutions

  1. Read the TDAMError message: it contains the raw response body revealing what actually returned (HTML page, empty, auth wall)
  2. Verify base_url points at the correct API host and not a gateway/console URL
  3. Check upstream service health if the body is a 502/504 HTML error page
  4. Confirm no corporate proxy/VPN is intercepting and rewriting responses

Example fix

# before
client = TDAMClient(base_url="https://example.com/console")  # returns HTML
# after
client = TDAMClient(base_url="https://api.example.com/v3")  # returns JSON envelope
Defensive patterns

Strategy: try-catch

Validate before calling

# preflight: ensure endpoint speaks JSON
import requests
r = requests.get(base_url.rstrip('/') + '/health', timeout=5)
if 'application/json' not in r.headers.get('content-type', ''):
    raise RuntimeError("endpoint does not return JSON; check base_url")

Type guard

def is_tdam_error(e: BaseException) -> bool:
    from tencentdb_agent_memory import TDAMError
    return isinstance(e, TDAMError)

Try / catch

try:
    resp = client.post(path, payload)
except TDAMError as e:
    if e.code > 0 and "<html" in (e.message or "").lower():
        # gateway/HTML error page: check service health / base_url
        raise UpstreamUnavailable from e
    raise

Prevention

When it happens

Trigger: post() or get() receives a response whose body is not valid JSON — HTML error pages from a gateway (502/504), empty body with error status, gzip/charset mismatches, or hitting a wrong endpoint that returns text.

Common situations: Reverse proxy or load balancer intercepting the request and returning an HTML error page; wrong base_url pointing at a non-API host; API down and returning empty 5xx bodies; corporate proxy injecting blocks pages.

Related errors


AI-assisted analysis of TencentCloud/TencentDB-Agent-Memory@3efcd317b8 (2026-09-01). Data as JSON: /api/errors/2298f48adb64398d. Report an issue: GitHub.