{"record":{"id":"fadb6fcf21de62df","repo":"FoundationAgents/MetaGPT","slug":"text-too-long-text-length","errorCode":null,"errorMessage":"text too long:{text_length}","messagePattern":"text too long:(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/memory/brain_memory.py","lineNumber":150,"sourceCode":"            return await self._metagpt_summarize(max_words=max_words)\n\n        self.llm = llm\n        return await self._openai_summarize(llm=llm, max_words=max_words, keep_language=keep_language, limit=limit)\n\n    async def _openai_summarize(self, llm, max_words=200, keep_language: bool = False, limit: int = -1):\n        texts = [self.historical_summary]\n        for m in self.history:\n            texts.append(m.content)\n        text = \"\\n\".join(texts)\n\n        text_length = len(text)\n        if limit > 0 and text_length < limit:\n            return text\n        summary = await self._summarize(text=text, max_words=max_words, keep_language=keep_language, limit=limit)\n        if summary:\n            await self.set_history_summary(history_summary=summary, redis_key=self.config.redis_key)\n            return summary\n        raise ValueError(f\"text too long:{text_length}\")\n\n    async def _metagpt_summarize(self, max_words=200):\n        if not self.history:\n            return \"\"\n\n        total_length = 0\n        msgs = []\n        for m in reversed(self.history):\n            delta = len(m.content)\n            if total_length + delta > max_words:\n                left = max_words - total_length\n                if left == 0:\n                    break\n                m.content = m.content[0:left]\n                msgs.append(m)\n                break\n            msgs.append(m)\n            total_length += delta","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/memory/brain_memory.py#L132-L168","documentation":"BrainMemory.get_summary returns existing text if it is under limit; otherwise it summarizes and stores the summary. The ValueError('text too long:{N}') is raised only when the summarizer returned an empty/None result while the raw text exceeded the limit — i.e. summarization failed silently and there is nothing valid to return.","triggerScenarios":"Historical summary + full message history exceeds limit, and _summarize yields '' or None (LLM returned empty content, LLM call failed and was swallowed, or max_words constraint produced empty output).","commonSituations":"Very long conversation memory with an LLM that returns empty responses under token pressure; misconfigured summarize LLM; exceeding context window so the helper returns nothing.","solutions":["Check why _summarize returned empty: inspect the LLM response/logging for the summarizer call.","Reduce memory size (clear/trim history) or raise max_words/limit so summarization succeeds.","Retry the call — transient empty LLM responses are a common cause.","Ensure the LLM used for summarization has a context window larger than the text being summarized."],"exampleFix":"# before\nsummary = await self._summarize(text=text, max_words=max_words, ...)\nif summary:\n    ...\nraise ValueError(f\"text too long:{text_length}\")\n\n# after: retry empty summaries once, then fall back to truncation\nsummary = await self._summarize(text=text, max_words=max_words, ...)\nif not summary:\n    summary = await self._summarize(text=text, max_words=max_words, ...)\nif not summary:\n    summary = text[: max_words * 4]  # safe truncation fallback","handlingStrategy":"fallback","validationCode":"def summarizable(memory, limit: int) -> bool:\n    text = \"\\n\".join([memory.historical_summary or \"\"] + [m.content for m in memory.history])\n    return len(text) < limit or limit <= 0","typeGuard":null,"tryCatchPattern":"try:\n    summary = await brain_memory.get_summary(max_words=200)\nexcept ValueError as e:\n    if str(e).startswith(\"text too long\"):\n        # fallback: naive truncation keeps the agent alive\n        summary = (brain_memory.historical_summary or \"\")[:2000]\n    else:\n        raise","preventionTips":["Trim history periodically so summarization input stays well under the limit.","Monitor summarizer LLM responses for empty output and retry once."],"tags":["memory","summarization","llm","brain-memory"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}