bytedance/deer-flow · error · ValueError

mem0 top_k must be in [1, 1000]

Error message

mem0 top_k must be in [1, 1000]

What it means

Raised by Mem0Config.from_backend_config when memory.backend_config.top_k is outside [1, 1000] after int() coercion. top_k caps both the default search breadth and the number of memories injected by get_context, so 0 (disable) or >1000 (unbounded retrieval) are both rejected.

Source

Thrown at backend/packages/harness/deerflow/agents/memory/backends/mem0/config.py:103

            api_key_env=str(cfg.get("api_key_env", "MEM0_API_KEY")),
            base_url=str(cfg.get("base_url", "https://api.mem0.ai")).rstrip("/"),
            allow_insecure_http=allow_insecure_http,
            top_k=int(cfg.get("top_k", 8)),
            score_threshold=float(cfg.get("score_threshold", 0.1)),
            max_injection_chars=int(cfg.get("max_injection_chars", 12000)),
            timeout_seconds=float(cfg.get("timeout_seconds", 10.0)),
            startup_policy=str(cfg.get("startup_policy", "fail_fast")),
            read_policy=str(failure_policy.get("read", "fail_open")),
            write_policy=str(failure_policy.get("write", "log_and_drop")),
        )
        if config.startup_policy not in _STARTUP_POLICIES:
            raise ValueError(f"mem0 startup_policy must be one of {sorted(_STARTUP_POLICIES)}")
        if config.read_policy not in _READ_POLICIES:
            raise ValueError(f"mem0 failure_policy.read must be one of {sorted(_READ_POLICIES)}")
        if config.write_policy not in _WRITE_POLICIES:
            raise ValueError(f"mem0 failure_policy.write must be one of {sorted(_WRITE_POLICIES)}")
        if not 1 <= config.top_k <= 1000:
            raise ValueError("mem0 top_k must be in [1, 1000]")
        if not 0.0 <= config.score_threshold <= 1.0:
            raise ValueError("mem0 score_threshold must be in [0, 1]")
        if config.max_injection_chars <= 0:
            raise ValueError("mem0 max_injection_chars must be positive")
        if config.timeout_seconds <= 0:
            raise ValueError("mem0 timeout_seconds must be positive")
        if not config.api_key_env.strip():
            raise ValueError("mem0 api_key_env must be a non-empty env var name")
        parsed_base_url = urlsplit(config.base_url)
        if parsed_base_url.scheme not in {"http", "https"} or not parsed_base_url.netloc:
            raise ValueError("mem0 base_url must be an absolute http:// or https:// URL")
        if parsed_base_url.scheme == "http" and not config.allow_insecure_http:
            raise ValueError("mem0 base_url must use https:// because it carries the API key; set allow_insecure_http: true only for trusted local development")
        return config

    def resolve_api_key(self) -> str:
        """Read the API key from the configured environment variable."""
        key = os.environ.get(self.api_key_env, "").strip()

View on GitHub (pinned to 1dd6ba1acb)

Solutions

  1. Set top_k to a value within 1..1000 (default 8 is a sane starting point)
  2. To disable memory injection entirely, use memory.injection_enabled: false rather than top_k: 0
  3. If you genuinely need >1000 memories per turn, narrow the query/score_threshold instead — 1000 is a hard guardrail

Example fix

# before (config.yaml)
memory:
  injection_enabled: true
  backend_config:
    top_k: 0   # out of range

# after
memory:
  injection_enabled: false   # the supported way to disable injection
  backend_config:
    top_k: 8
Defensive patterns

Strategy: validation

Validate before calling

def top_k_ok(backend_config: dict) -> bool:
    try:
        return 1 <= int((backend_config or {}).get("top_k", 8)) <= 1000
    except (TypeError, ValueError):
        return False

Type guard

def is_valid_top_k(v: object) -> bool:
    return isinstance(v, int) and not isinstance(v, bool) and 1 <= v <= 1000

Prevention

When it happens

Trigger: top_k: 0 (often written intending to 'disable' memory — not supported), top_k: 5000, or a negative value in backend_config. Non-integer strings that still coerce (e.g. "8") are fine.

Common situations: Trying to disable memory injection via top_k instead of memory.injection_enabled: false; tuning retrieval very high for a big corpus; a templated default of 0 slipping through.

Related errors


AI-assisted analysis of bytedance/deer-flow@1dd6ba1acb (2026-08-14). Data as JSON: /api/errors/3f8c6dbff05c1311. Report an issue: GitHub.