{"record":{"id":"e6b4a3f84f9644b3","repo":"rohitg00/ai-engineering-from-scratch","slug":"max-len-must-be-2-to-fit-inst-and-resp","errorCode":null,"errorMessage":"max_len must be >= 2 to fit INST and RESP","messagePattern":"max_len must be >= 2 to fit INST and RESP","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/39-instruction-tuning-sft/code/main.py","lineNumber":66,"sourceCode":"        needed but always keeps the RESP marker plus at least one response\n        token so SFT collation never produces fully-masked labels.\"\"\"\n        if max_len < 3:\n            raise ValueError(\"max_len must be >= 3 to fit INST, RESP, and one response token\")\n        inst_bytes = list(instruction.encode(\"utf-8\", errors=\"ignore\"))\n        resp_bytes = list(response.encode(\"utf-8\", errors=\"ignore\"))\n        # Reserve 2 control tokens + at least 1 response byte.\n        max_inst = max_len - 3\n        inst_bytes = inst_bytes[:max_inst]\n        ids = [self.INST_ID] + inst_bytes + [self.RESP_ID]\n        resp_start = len(ids)\n        ids.extend(resp_bytes[: max_len - len(ids)])\n        return ids, resp_start\n\n    def encode_prefix(self, instruction: str, max_len: int) -> List[int]:\n        \"\"\"Encode just the instruction prefix for generation. Always keeps the\n        RESP marker so the model sees the same boundary as during training.\"\"\"\n        if max_len < 2:\n            raise ValueError(\"max_len must be >= 2 to fit INST and RESP\")\n        inst_bytes = list(instruction.encode(\"utf-8\", errors=\"ignore\"))[: max_len - 2]\n        ids = [self.INST_ID] + inst_bytes + [self.RESP_ID]\n        return ids\n\n    def decode_response(self, ids: Sequence[int]) -> str:\n        \"\"\"Decode a generated response, dropping specials.\"\"\"\n        chunk = bytes(i for i in ids if i < 256)\n        return chunk.decode(\"utf-8\", errors=\"replace\")\n\n\n# ---------------------------------------------------------------------------\n# Tiny GPT\n# ---------------------------------------------------------------------------\n\n\nclass CausalSelfAttention(nn.Module):\n    def __init__(self, hidden: int, heads: int, max_len: int):\n        super().__init__()","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/39-instruction-tuning-sft/code/main.py#L48-L84","documentation":"Error \"max_len must be >= 2 to fit INST and RESP\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/39-instruction-tuning-sft/code/main.py:66 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}