{"record":{"id":"50f8ec2c451f294e","repo":"langchain-ai/langchain","slug":"maxsize-must-be-greater-than-0","errorCode":null,"errorMessage":"maxsize must be greater than 0","messagePattern":"maxsize must be greater than 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/caches.py","lineNumber":198,"sourceCode":"    \"\"\"\n\n    def __init__(self, *, maxsize: int | None = None) -> None:\n        \"\"\"Initialize with empty cache.\n\n        Args:\n            maxsize: The maximum number of items to store in the cache.\n\n                If `None`, the cache has no maximum size.\n\n                If the cache exceeds the maximum size, the oldest items are removed.\n\n        Raises:\n            ValueError: If `maxsize` is less than or equal to `0`.\n        \"\"\"\n        self._cache: dict[tuple[str, str], RETURN_VAL_TYPE] = {}\n        if maxsize is not None and maxsize <= 0:\n            msg = \"maxsize must be greater than 0\"\n            raise ValueError(msg)\n        self._maxsize = maxsize\n\n    def lookup(self, prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None:\n        \"\"\"Look up based on `prompt` and `llm_string`.\n\n        Args:\n            prompt: A string representation of the prompt.\n\n                In the case of a chat model, the prompt is a non-trivial\n                serialization of the prompt into the language model.\n            llm_string: A string representation of the LLM configuration.\n\n        Returns:\n            On a cache miss, return `None`. On a cache hit, return the cached value.\n        \"\"\"\n        return self._cache.get((prompt, llm_string), None)\n\n    def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None:","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/caches.py#L180-L216","documentation":"`BaseCache` subclass `InMemoryCache.__init__` (libs/core/langchain_core/caches.py) validates its `maxsize` argument: passing a non-None value <= 0 raises `ValueError('maxsize must be greater than 0')`. It is a fail-fast constructor check so the eviction logic never sees an invalid bound.","triggerScenarios":"Instantiating `InMemoryCache(maxsize=0)` or a negative maxsize; computing maxsize from config/env (e.g. `maxsize=int(os.environ.get('CACHE_MAX', 0))`) where the default or parse yields 0; passing `-1` intending 'unlimited'","commonSituations":"Env-var driven configuration defaulting to 0 when unset; porting code from LRU implementations where -1 means unbounded; YAML/TOML config typos.","solutions":["Pass a positive integer: `InMemoryCache(maxsize=1000)`","Pass `maxsize=None` for an unbounded cache instead of 0 or -1","If maxsize comes from config, normalize before constructing: `maxsize = int(v) if v and int(v) > 0 else None`","Add a unit test asserting the constructor raises for 0 and negative values"],"exampleFix":"# before\ncache = InMemoryCache(maxsize=int(os.environ.get(\"CACHE_MAXSIZE\", \"0\")))\n\n# after\nraw = os.environ.get(\"CACHE_MAXSIZE\")\ncache = InMemoryCache(maxsize=int(raw) if raw and int(raw) > 0 else None)","handlingStrategy":"validation","validationCode":"def to_maxsize(raw: str | int | None) -> int | None:\n    if raw in (None, ''):\n        return None\n    n = int(raw)\n    return n if n > 0 else None  # or raise on invalid config\n\ncache = InMemoryCache(maxsize=to_maxsize(os.environ.get('CACHE_MAXSIZE')))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat None as 'unbounded'; never 0 or -1","Centralize config parsing so invalid maxsize values fail loudly at startup","Unit-test the constructor contract: 0 and negatives raise"],"tags":["cache","validation","constructor","configuration"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}