{"record":{"id":"120d5ac05812c945","repo":"iflytek/astron-agent","slug":"api-request-failed-status-result","errorCode":null,"errorMessage":"API request failed: {status} - {result}","messagePattern":"API request failed: (.+?) - (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"core/knowledge/infra/ragflow/ragflow_client.py","lineNumber":345,"sourceCode":"    for attempt in range(max_retries):\n        try:\n            session = await _get_session()\n            url = urljoin(config[\"base_url\"], endpoint)\n\n            if files:\n                form_data = await _create_file_form_data(files)\n                result, status = await _send_file_request(\n                    session, method, url, form_data, config\n                )\n            else:\n                result, status = await _send_json_request(\n                    session, method, url, data, config\n                )\n\n            logger.debug(f\"{method} {endpoint} - Status: {status}\")\n\n            if status != 200:\n                raise Exception(f\"API request failed: {status} - {result}\")\n\n            return result\n\n        except (aiohttp.ClientConnectionError, RuntimeError) as e:\n            if _is_session_closed_error(e):\n                await _handle_session_error(attempt, max_retries, e)\n                continue\n            else:\n                raise e\n        except Exception as e:\n            logger.error(f\"Request failed: {method} {endpoint} - {e}\")\n            logger.error(f\"Request URL: {url}\")\n            if data:\n                logger.error(f\"Request data: {data}\")\n            raise\n\n    # This should never be reached due to exceptions being raised\n    raise Exception(\"All retry attempts failed\")","sourceCodeStart":327,"sourceCodeEnd":363,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/knowledge/infra/ragflow/ragflow_client.py#L327-L363","documentation":"_make_request is the shared HTTP client for the RAGFlow REST API in ragflow_client.py. After performing the request it checks the status code: anything other than HTTP 200 raises Exception(f\"API request failed: {status} - {result}\"), embedding the status and the parsed response body so callers can see the server-side reason.","triggerScenarios":"Calling retrieval, retrieval_with_dataset, list_datasets, create_dataset, update_dataset, or update_document when the RAGFlow server answers with a non-200 status — e.g. 401 for a bad token, 404 for an unknown endpoint/dataset, 4xx/5xx validation or server errors.","commonSituations":"Expired or wrong RAGFLOW_API_TOKEN (401); requesting a dataset/document id that does not exist (404); malformed payload rejected by the API (400); RAGFlow version mismatch where the endpoint path changed; RAGFlow server 5xx during overload.","solutions":["Read the status and body embedded in the message: 401 → fix RAGFLOW_API_TOKEN; 404 → verify ids/endpoint paths; 400 → fix request payload","Confirm RAGFLOW_BASE_URL points to the correct RAGFlow version's API (path compatibility)","Validate the dataset/document id exists via list_datasets before update/retrieval calls","Check RAGFlow server logs for the corresponding 4xx/5xx to get the server-side error detail"],"exampleFix":"# before\nawait update_dataset(dataset_id, {\"name\": new_name})  # id from stale cache -> 404\n# after\ndatasets = await list_datasets()\nif any(d['id'] == dataset_id for d in datasets):\n    await update_dataset(dataset_id, {\"name\": new_name})","handlingStrategy":"try-catch","validationCode":"async def ensure_dataset_exists(client, dataset_id: str) -> bool:\n    datasets = await client.list_datasets()\n    return any(d.get('id') == dataset_id for d in datasets)","typeGuard":null,"tryCatchPattern":"try:\n    result = await retrieval(dataset_id, query)\nexcept Exception as e:\n    if str(e).startswith('API request failed:'):\n        status = str(e).split(' - ')[0].rsplit(' ', 1)[-1]\n        if status == '401':\n            refresh_ragflow_token()\n        elif status == '404':\n            handle_missing_dataset(dataset_id)","preventionTips":["Keep RAGFLOW_API_TOKEN valid and rotated before expiry","Verify ids exist before update/retrieval calls","Pin RAGFLOW_BASE_URL to a RAGFlow version whose API paths you test against","Monitor RAGFlow server 4xx/5xx rates"],"tags":["http","api","ragflow","rest"],"backgroundTag":"http-error-response","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}