{"record":{"id":"2298f48adb64398d","repo":"TencentCloud/TencentDB-Agent-Memory","slug":"resp-text-or-http-resp-status-code-returned-a","errorCode":null,"errorMessage":"<resp.text or \"HTTP {resp.status_code} returned a non-JSON response\">","messagePattern":"<resp\\.text or \"HTTP (.+?) returned a non-JSON response\">","errorType":"error_code","errorClass":"TDAMError","httpStatus":null,"severity":"error","filePath":"sdk/memory-core/python/tencentdb_agent_memory/_v3_http.py","lineNumber":48,"sourceCode":"    if not api_key or not api_key.strip():\n        raise ParamError(\"api_key must be provided\")\n    if not service_id or not service_id.strip():\n        raise ParamError(\"service_id must be provided\")\n    if isinstance(timeout, bool) or not isinstance(timeout, (int, float)) or timeout <= 0:\n        raise ParamError(\"timeout must be a positive number\")\n\n\ndef _decode_response(resp: httpx.Response) -> dict:\n    header_request_id = (\n        resp.headers.get(\"x-qcloud-transaction-id\")\n        or resp.headers.get(\"x-trace-id\")\n        or \"\"\n    )\n    try:\n        envelope = resp.json()\n    except ValueError as exc:\n        message = resp.text or f\"HTTP {resp.status_code} returned a non-JSON response\"\n        raise TDAMError(resp.status_code if resp.is_error else -1, message, header_request_id) from exc\n\n    if not isinstance(envelope, dict):\n        raise TDAMError(-1, \"API response must be a JSON object\", header_request_id)\n\n    code = envelope.get(\"code\")\n    if resp.is_error or code != 0:\n        effective_code = code if isinstance(code, int) and code != 0 else resp.status_code\n        payload = envelope.get(\"data\")\n        details = payload if isinstance(payload, dict) else None\n        raise TDAMError(\n            code=effective_code,\n            message=str(envelope.get(\"message\") or f\"HTTP {resp.status_code}\"),\n            request_id=str(envelope.get(\"request_id\") or header_request_id),\n            details=details,\n        )\n\n    result = envelope.get(\"data\") or {}\n    if not isinstance(result, dict):","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/3efcd317b84146d6a08518ac0f7ee7c8a8d200ec/sdk/memory-core/python/tencentdb_agent_memory/_v3_http.py#L30-L66","documentation":"_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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the TDAMError message: it contains the raw response body revealing what actually returned (HTML page, empty, auth wall)","Verify base_url points at the correct API host and not a gateway/console URL","Check upstream service health if the body is a 502/504 HTML error page","Confirm no corporate proxy/VPN is intercepting and rewriting responses"],"exampleFix":"# before\nclient = TDAMClient(base_url=\"https://example.com/console\")  # returns HTML\n# after\nclient = TDAMClient(base_url=\"https://api.example.com/v3\")  # returns JSON envelope","handlingStrategy":"try-catch","validationCode":"# preflight: ensure endpoint speaks JSON\nimport requests\nr = requests.get(base_url.rstrip('/') + '/health', timeout=5)\nif 'application/json' not in r.headers.get('content-type', ''):\n    raise RuntimeError(\"endpoint does not return JSON; check base_url\")","typeGuard":"def is_tdam_error(e: BaseException) -> bool:\n    from tencentdb_agent_memory import TDAMError\n    return isinstance(e, TDAMError)","tryCatchPattern":"try:\n    resp = client.post(path, payload)\nexcept TDAMError as e:\n    if e.code > 0 and \"<html\" in (e.message or \"\").lower():\n        # gateway/HTML error page: check service health / base_url\n        raise UpstreamUnavailable from e\n    raise","preventionTips":["Verify base_url points at the JSON API host, not a console/gateway page","Inspect the TDAMError message — it contains the raw non-JSON body","Watch for 502/504 HTML pages indicating the upstream service is down","Confirm no intercepting proxy rewrites responses"],"tags":["python","http","json","parsing"],"backgroundTag":"non-json-response","analyzedSha":"3efcd317b84146d6a08518ac0f7ee7c8a8d200ec","analyzedAt":"2026-09-01T05:44:22.276Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}