{"record":{"id":"5bfad4d8c9714a96","repo":"rohitg00/ai-engineering-from-scratch","slug":"chunk-tokens-must-be-0","errorCode":null,"errorMessage":"chunk_tokens must be > 0","messagePattern":"chunk_tokens must be > 0","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/87-end-to-end-safety-gate/code/mock_llm_stream.py","lineNumber":62,"sourceCode":"    r\"\\bbegin\\s+your\\s+answer\\s+with\\b\",\n    r\"\\bsure[, ]\\s*here\\s+is\\b\",\n]\n\n\ndef _persona(prompt: str) -> str:\n    low = prompt.lower()\n    for pat in _REFUSE_PATTERNS:\n        if re.search(pat, low):\n            return \"attacker-honest\"\n    for pat in _LAZY_PATTERNS:\n        if re.search(pat, low):\n            return \"attacker-lazy\"\n    return \"clean\"\n\n\ndef stream(prompt: str, chunk_tokens: int = 4) -> Iterator[str]:\n    if chunk_tokens <= 0:\n        raise ValueError(\"chunk_tokens must be > 0\")\n    persona = _persona(prompt)\n    if persona == \"attacker-honest\":\n        text = REFUSAL\n    elif persona == \"attacker-lazy\":\n        text = HARMFUL_CONTINUATION\n    else:\n        text = BENIGN_REPLY\n    tokens = text.split()\n    for i in range(0, len(tokens), chunk_tokens):\n        yield \" \".join(tokens[i : i + chunk_tokens]) + \" \"\n\n\ndef persona_for(prompt: str) -> str:\n    return _persona(prompt)\n","sourceCodeStart":44,"sourceCodeEnd":77,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/87-end-to-end-safety-gate/code/mock_llm_stream.py#L44-L77","documentation":"stream yields the simulated model response in chunks of chunk_tokens tokens; a non-positive chunk size cannot slice the text into pieces and would produce empty or infinite chunk loops, so it is rejected before streaming begins.","triggerScenarios":"Thrown at phases/19-capstone-projects/87-end-to-end-safety-gate/code/mock_llm_stream.py:62 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass a positive chunk size (default 4)","Clamp user input: chunk_tokens = max(1, int(chunk_tokens))","Validate streaming config parameters when initializing the client"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}