{"record":{"id":"28051f3eac78dee6","repo":"zylon-ai/private-gpt","slug":"condensed-chat-history-exceeds-maximum-length-afte","errorCode":null,"errorMessage":"Condensed chat history exceeds maximum length after applying condensation strategy.","messagePattern":"Condensed chat history exceeds maximum length after applying condensation strategy\\.","errorType":"exception","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"private_gpt/components/chat/processors/chat_history/memory/strategies/condenser.py","lineNumber":825,"sourceCode":"                tokenizer_fn=tokenizer,\n                chat_history=chat_history,\n                max_length=max_length,\n                left_tokens=left_tokens,\n                last_user_tokens=last_user_message_tokens,\n                right_tokens=right_tokens,\n            )\n\n        # After condensation, we ensure that the last user message is preserved\n        # and the chat history is within the maximum length\n        if not any(msg.role == \"user\" for msg in chat_history):\n            raise ValueError(\"No user messages found after condensation.\")\n\n        if not last_user_message:\n            raise ValueError(\"No last user message found after condensation.\")\n\n        tokens = await self._get_messages_tokens(chat_history, tokenizer_fn=tokenizer)\n        if tokens > max_length:\n            raise ValueError(\n                \"Condensed chat history exceeds maximum length after applying condensation strategy.\"\n            )\n\n        return chat_history\n","sourceCodeStart":807,"sourceCodeEnd":830,"githubUrl":"https://github.com/zylon-ai/private-gpt/blob/4a030776a31a901ad80b1bf4d7faa2c1a367efbb/private_gpt/components/chat/processors/chat_history/memory/strategies/condenser.py#L807-L830","documentation":"Raised by the condenser after a condensation strategy has run: the resulting chat history is re-tokenized and still exceeds max_length. This is a post-condition check — the strategy (truncation, summarization, etc.) was applied but did not shrink the history enough. It usually means max_length is smaller than the irreducible content (system prompt + preserved last user message + minimal context).","triggerScenarios":"Calling condense_strategy.get_memory() (or the wrapper that calls _get_messages_tokens afterwards) with a max_length smaller than system messages + last user message tokens; using a strategy that keeps too much context (e.g. keep-N with large N); an extremely long single user message that is preserved verbatim.","commonSituations":"Small context-window models configured via settings; a very large system prompt eating the budget; tokenizer_fn mismatch between what the strategy estimated and the final count; strategy 'retain-all' style configs.","solutions":["Increase max_length so it exceeds system messages + last user message token count","Shrink the system prompt or move static instructions out of the condensed history","Switch to a more aggressive condensation strategy (deeper truncation / summarization)","If the last user message alone exceeds the budget, reject the request earlier with a clear message before condensation runs"],"exampleFix":"# before\ncondensed = await strategy.get_memory(chat_history, max_length=2048)\n\n# after\n# ensure budget > system + last-user tokens\ncondensed = await strategy.get_memory(chat_history, max_length=8192)","handlingStrategy":"validation","validationCode":"system_and_last_user = system_messages + [last_user]\nbaseline = await estimate_token_count(system_and_last_user, tokenizer_fn=tokenizer)\nif baseline >= max_length:\n    raise ConfigError(f\"max_length {max_length} too small; baseline is {baseline}\")","typeGuard":null,"tryCatchPattern":"try:\n    condensed = await strategy.get_memory(chat_history, max_length=max_length)\nexcept ValueError as e:\n    if \"exceeds maximum length\" in str(e):\n        # retry once with a larger budget or fall back to hard truncation\n        condensed = hard_truncate(chat_history, max_length)\n    else:\n        raise","preventionTips":["Assert at startup that max_length exceeds system-prompt + max expected user message tokens","Log token counts before/after condensation to spot budget drift early","Use the same tokenizer_fn for budgeting and for the final check"],"tags":["chat-history","condensation","token-limit","context-window"],"backgroundTag":null,"analyzedSha":"4a030776a31a901ad80b1bf4d7faa2c1a367efbb","analyzedAt":"2026-08-15T03:51:26.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}