{"record":{"id":"aec80ca3da3e4811","repo":"HKUDS/DeepTutor","slug":"too-many-sessions-in-one-request-max-max-sessio","errorCode":null,"errorMessage":"Too many sessions in one request (max {_MAX_SESSIONS_PER_REQUEST})","messagePattern":"Too many sessions in one request \\(max (.+?)\\)","errorType":"http","errorClass":"HTTPException","httpStatus":413,"severity":"warning","filePath":"deeptutor/api/routers/imports.py","lineNumber":79,"sourceCode":"    agent_id: str = Field(default=\"\", max_length=256)\n    agent_name: str = Field(default=\"\", max_length=256)\n    sessions: list[ImportedSession] = Field(default_factory=list)\n\n    @field_validator(\"source\")\n    @classmethod\n    def _normalize_source(cls, value: str) -> str:\n        normalized = (value or \"\").strip().lower()\n        if normalized not in _ALLOWED_SOURCES:\n            raise ValueError(f\"Unsupported import source: {value!r}\")\n        return normalized\n\n\n@router.post(\"/chat-history\")\nasync def import_chat_history(payload: ChatHistoryImportRequest) -> dict[str, Any]:\n    if not payload.sessions:\n        raise HTTPException(status_code=400, detail=\"No sessions to import\")\n    if len(payload.sessions) > _MAX_SESSIONS_PER_REQUEST:\n        raise HTTPException(\n            status_code=413,\n            detail=f\"Too many sessions in one request (max {_MAX_SESSIONS_PER_REQUEST})\",\n        )\n\n    store = get_sqlite_session_store()\n    imported = 0\n    skipped = 0\n    results: list[dict[str, Any]] = []\n\n    for incoming in payload.sessions:\n        # Drop content-less rows (e.g. tool-only turns the adapter could not\n        # reduce to text) so the transcript stays a clean human conversation.\n        messages = [m for m in incoming.messages if (m.content or \"\").strip()]\n        if not messages:\n            skipped += 1\n            results.append(\n                {\"external_id\": incoming.external_id, \"imported\": False, \"reason\": \"empty\"}\n            )","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/HKUDS/DeepTutor/blob/3e82f130422a813cdd73c10b21a44e9325f5821a/deeptutor/api/routers/imports.py#L61-L97","documentation":"413 raised by POST /imports/chat-history when payload.sessions exceeds _MAX_SESSIONS_PER_REQUEST, a bulk-import guard preventing oversized single requests.","triggerScenarios":"POSTing an import with more sessions than the constant allows (e.g. thousands of sessions in one body).","commonSituations":"Migrating a large multi-year chat history in a single call; no client-side batching.","solutions":["Read the detail to get the max, then split sessions into batches of at most that size","Add client-side chunking before import","Verify the server constant (or raise it via config if appropriate) in deeptutor/api/routers/imports.py"],"exampleFix":"# before\nresp = post('/imports/chat-history', json={'source':'claude','sessions':all_sessions})\n# after\nMAX = 200\nfor i in range(0, len(all_sessions), MAX):\n    post('/imports/chat-history', json={'source':'claude','sessions':all_sessions[i:i+MAX]})","handlingStrategy":"validation","validationCode":"MAX = 200  # keep in sync with _MAX_SESSIONS_PER_REQUEST\nfor i in range(0, len(sessions), MAX):\n    import_batch(sessions[i:i + MAX])","typeGuard":null,"tryCatchPattern":"resp = client.post('/imports/chat-history', json=payload)\nif resp.status_code == 413:\n    max_n = int(re.search(r'max (\\d+)', resp.text).group(1))\n    for i in range(0, len(sessions), max_n):\n        client.post('/imports/chat-history', json={**payload, 'sessions': sessions[i:i + max_n]})","preventionTips":["Always batch large imports","Read the limit from the 413 detail and adapt dynamically"],"tags":["imports","http-413","payload-limit","batching"],"backgroundTag":"request-too-large","analyzedSha":"3e82f130422a813cdd73c10b21a44e9325f5821a","analyzedAt":"2026-08-27T06:57:25.364Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}