zylon-ai/private-gpt · error · ValueError

The last user message exceeds the maximum length allowed.

Error message

The last user message exceeds the maximum length allowed.

What it means

ValueError from the condenser's main condense path. It tokenizes left messages, the last user message, and right messages; if the last user message alone exceeds max_length, no amount of condensing can help (the last user message is always preserved by design), so it fails fast rather than looping.

Source

Thrown at private_gpt/components/chat/processors/chat_history/memory/strategies/condenser.py:777

        (
            left_messages,
            last_user_message,
            right_messages,
        ) = await asyncio.to_thread(self._split_conversation, chat_history.copy())

        # Select LLM and tokenizer
        model_id: str | None = kwargs.get("model_id")
        llm = self.llm_component.get_llm(model_id)
        tokenizer = self.llm_component.get_tokenizer(model_id)

        left_tokens, last_user_message_tokens, right_tokens = await asyncio.gather(
            self._get_messages_tokens(left_messages, tokenizer_fn=tokenizer),
            self._get_messages_tokens(last_user_message, tokenizer_fn=tokenizer),
            self._get_messages_tokens(right_messages, tokenizer_fn=tokenizer),
        )

        if last_user_message_tokens > max_length:
            raise ValueError(
                "The last user message exceeds the maximum length allowed."
            )
        if left_tokens + last_user_message_tokens + right_tokens <= max_length:
            return chat_history

        # Decide in which direction we will go
        left_token_percentage = left_tokens / (
            left_tokens + right_tokens + last_user_message_tokens
        )
        right_token_percentage = right_tokens / (
            left_tokens + right_tokens + last_user_message_tokens
        )
        diff_tokens = right_token_percentage - left_token_percentage

        # If there exists a significant difference in left token distribution,
        # we will perform condensation from the left side
        if diff_tokens <= 0.1:
            chat_history = await self._condense_from_left(

View on GitHub (pinned to 4a030776a3)

Solutions

  1. Increase max_length so it accommodates the largest legitimate user message.
  2. Pre-process oversized user messages: summarize, chunk, or move document content into retrieval instead of the prompt.
  3. Validate incoming message size at the API boundary and reject with a clear 413-style error before condensing.
  4. Catch the ValueError and respond to the client asking to shorten the prompt.

Example fix

// before
user_msg = open('huge_file.txt').read()   # 50k tokens
await condenser.condense(history)          # ValueError

// after
user_msg = summarize_or_chunk(open('huge_file.txt').read())
await condenser.condense(history)
Defensive patterns

Strategy: validation

Validate before calling

last_user_tokens = await estimate_token_count(last_user_message, tokenizer_fn=tokenizer)
if last_user_tokens > max_length:
    raise HTTPException(413, f"Prompt too large: {last_user_tokens} tokens > budget {max_length}")

Type guard

def user_message_fits(msg: ChatMessage, max_length: int, tokenizer) -> bool:
    import asyncio
    return asyncio.get_event_loop().run_until_complete(estimate_token_count(msg, tokenizer_fn=tokenizer)) <= max_length

Try / catch

try:
    condensed = await condenser.condense(history)
except ValueError as e:
    if "last user message exceeds" in str(e):
        raise HTTPException(413, "Please shorten your message") from e
    raise

Prevention

When it happens

Trigger: A single huge user prompt (e.g. pasted document or long code) whose token count > condense max_length; max_length misconfigured to a value smaller than a typical user turn; tokenizer change inflating counts.

Common situations: Users pasting entire files into chat; context budget set near the model's output token limit rather than input limit; RAG flows injecting retrieved chunks into the user message.

Related errors


AI-assisted analysis of zylon-ai/private-gpt@4a030776a3 (2026-08-15). Data as JSON: /api/errors/f665caea0d6deafd. Report an issue: GitHub.