FoundationAgents/MetaGPT · error · ValueError

text too long:{text_length}

Error message

text too long:{text_length}

What it means

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.

Source

Thrown at metagpt/memory/brain_memory.py:150

            return await self._metagpt_summarize(max_words=max_words)

        self.llm = llm
        return await self._openai_summarize(llm=llm, max_words=max_words, keep_language=keep_language, limit=limit)

    async def _openai_summarize(self, llm, max_words=200, keep_language: bool = False, limit: int = -1):
        texts = [self.historical_summary]
        for m in self.history:
            texts.append(m.content)
        text = "\n".join(texts)

        text_length = len(text)
        if limit > 0 and text_length < limit:
            return text
        summary = await self._summarize(text=text, max_words=max_words, keep_language=keep_language, limit=limit)
        if summary:
            await self.set_history_summary(history_summary=summary, redis_key=self.config.redis_key)
            return summary
        raise ValueError(f"text too long:{text_length}")

    async def _metagpt_summarize(self, max_words=200):
        if not self.history:
            return ""

        total_length = 0
        msgs = []
        for m in reversed(self.history):
            delta = len(m.content)
            if total_length + delta > max_words:
                left = max_words - total_length
                if left == 0:
                    break
                m.content = m.content[0:left]
                msgs.append(m)
                break
            msgs.append(m)
            total_length += delta

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Check why _summarize returned empty: inspect the LLM response/logging for the summarizer call.
  2. Reduce memory size (clear/trim history) or raise max_words/limit so summarization succeeds.
  3. Retry the call — transient empty LLM responses are a common cause.
  4. Ensure the LLM used for summarization has a context window larger than the text being summarized.

Example fix

# before
summary = await self._summarize(text=text, max_words=max_words, ...)
if summary:
    ...
raise ValueError(f"text too long:{text_length}")

# after: retry empty summaries once, then fall back to truncation
summary = await self._summarize(text=text, max_words=max_words, ...)
if not summary:
    summary = await self._summarize(text=text, max_words=max_words, ...)
if not summary:
    summary = text[: max_words * 4]  # safe truncation fallback
Defensive patterns

Strategy: fallback

Validate before calling

def summarizable(memory, limit: int) -> bool:
    text = "\n".join([memory.historical_summary or ""] + [m.content for m in memory.history])
    return len(text) < limit or limit <= 0

Try / catch

try:
    summary = await brain_memory.get_summary(max_words=200)
except ValueError as e:
    if str(e).startswith("text too long"):
        # fallback: naive truncation keeps the agent alive
        summary = (brain_memory.historical_summary or "")[:2000]
    else:
        raise

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/fadb6fcf21de62df. Report an issue: GitHub.