{"record":{"id":"5e1ba5b8b56f1927","repo":"zylon-ai/private-gpt","slug":"token-limit-must-be-set-and-greater-than-0","errorCode":null,"errorMessage":"Token limit must be set and greater than 0.","messagePattern":"Token limit must be set and greater than 0\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"private_gpt/components/memory/trimming_memory.py","lineNumber":80,"sourceCode":"        exclude=True,\n    )\n    tokenizer_fn: TokenizerFn = Field(\n        exclude=True,\n    )\n\n    @classmethod\n    def class_name(cls) -> str:\n        \"\"\"Get class name.\"\"\"\n        return \"TrimmingMemory\"\n\n    @model_validator(mode=\"before\")\n    @classmethod\n    def validate_memory(cls, values: dict[str, Any]) -> dict[str, Any]:\n        \"\"\"Validate memory configuration.\"\"\"\n        # Validate token limit\n        token_limit = values.get(\"token_limit\", -1)\n        if token_limit < 1:\n            raise ValueError(\"Token limit must be set and greater than 0.\")\n\n        # Validate tokenizer\n        tokenizer_fn = values.get(\"tokenizer_fn\")\n        if tokenizer_fn is None:\n            # TODO: Replace with a default tokenizer function\n            raise ValueError(\"tokenizer_fn must be provided.\")\n\n        # Validate text splitter\n        text_splitter = values.get(\"text_splitter\")\n        if text_splitter is None:\n            values[\"text_splitter\"] = _default_text_splitter\n\n        # Validate strategy-specific constraints\n        trim_strategy = values.get(\"trim_strategy\", TrimStrategy.LAST)\n        start_on = values.get(\"start_on\")\n        include_system = values.get(\"include_system\", True)\n\n        if start_on and trim_strategy == TrimStrategy.FIRST:","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/zylon-ai/private-gpt/blob/4a030776a31a901ad80b1bf4d7faa2c1a367efbb/private_gpt/components/memory/trimming_memory.py#L62-L98","documentation":"Raised by TrimmingMemory's pydantic model_validator(mode='before') when token_limit is missing or below 1 — values.get('token_limit', -1) means an absent token_limit reads as -1 and fails immediately. This is deliberate fail-fast validation: a trimming memory without a positive budget cannot trim, so construction is rejected instead of silently misbehaving at runtime.","triggerScenarios":"Constructing TrimmingMemory(token_limit=0), TrimmingMemory(token_limit=-100), or omitting token_limit entirely when instantiating the model directly (bypassing from_defaults, which derives a limit from the LLM context window or DEFAULT_TOKEN_LIMIT).","commonSituations":"Direct model instantiation in tests or custom wiring without a limit; config where token_limit is parsed as 0 (e.g. unset env var coerced to int); passing context_window-derived limits from an LLM that reports 0.","solutions":["Pass a positive token_limit (e.g. 2048) when constructing TrimmingMemory.","Prefer from_defaults, which derives token_limit from llm.metadata.context_window * DEFAULT_TOKEN_LIMIT_RATIO or DEFAULT_TOKEN_LIMIT when not given.","Fix the upstream value if context_window is 0 (see error 156's sibling path) — check the LLM metadata source.","Validate token_limit > 0 in your config loader before constructing memory."],"exampleFix":"# before\nmemory = TrimmingMemory(token_limit=0, tokenizer_fn=tok)\n\n# after\nmemory = TrimmingMemory(token_limit=int(llm.metadata.context_window * 0.75), tokenizer_fn=tok)","handlingStrategy":"validation","validationCode":"token_limit = token_limit or int(llm.metadata.context_window * 0.75)\nassert token_limit >= 1, 'token_limit must be positive'","typeGuard":null,"tryCatchPattern":"try:\n    mem = TrimmingMemory(token_limit=tl, tokenizer_fn=tok)\nexcept ValidationError as e:\n    if 'Token limit' in str(e):\n        tl = DEFAULT_TOKEN_LIMIT; mem = TrimmingMemory(token_limit=tl, tokenizer_fn=tok)","preventionTips":["Always derive token_limit via from_defaults (LLM context window) instead of hardcoding.","Treat a 0 context_window from LLM metadata as a bug in the LLM backend and fix it there."],"tags":["memory","validation","pydantic","configuration"],"backgroundTag":null,"analyzedSha":"4a030776a31a901ad80b1bf4d7faa2c1a367efbb","analyzedAt":"2026-08-15T03:51:26.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}