{"record":{"id":"21f90740e7c39ea1","repo":"zylon-ai/private-gpt","slug":"user-block-i-does-not-end-with-an-assistant-or-t","errorCode":null,"errorMessage":"User block {i} does not end with an assistant or tool message: {block[-1]}","messagePattern":"User block (.+?) does not end with an assistant or tool message: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"private_gpt/components/chat/processors/chat_history/memory/utils/repairs.py","lineNumber":149,"sourceCode":"            raise ValueError(f\"User block {i} is empty.\")\n\n        # Validate the first and last messages in the block\n        if block[0].role != \"user\":\n            raise ValueError(\n                f\"User block {i} does not start with a user message: {block[0]}\"\n            )\n\n        if len(block) == 1:\n            # No more validation needed for a single user message block\n            continue\n\n        # Validate the last message in the block\n        if strict and block[-1].role != \"assistant\":\n            raise ValueError(\n                f\"User block {i} does not end with an assistant message: {block[-1]}\"\n            )\n        elif not strict and block[-1].role not in [\"assistant\", \"tool\"]:\n            raise ValueError(\n                f\"User block {i} does not end with an assistant or tool message: {block[-1]}\"\n            )\n\n        # Validate the tool messages\n        for j, message in enumerate(block[1:-1]):\n            if message.role == \"tool\":\n                if j == 0 or block[j].role != \"assistant\":\n                    raise ValueError(\n                        f\"Tool message {j} in user block {i} does not follow an assistant message: {message}\"\n                    )\n            elif message.role != \"assistant\":\n                raise ValueError(\n                    f\"Message {j} in user block {i} is neither an assistant nor a tool: {message}\"\n                )\n\n\ndef _assert_repair_without_tools(\n    chat_history: list[ChatMessage],","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/zylon-ai/private-gpt/blob/4a030776a31a901ad80b1bf4d7faa2c1a367efbb/private_gpt/components/chat/processors/chat_history/memory/utils/repairs.py#L131-L167","documentation":"Non-strict-mode user-block validation: a multi-message block must end with either an assistant or a tool message. It fires when the block ends with any other role — in practice a user message, meaning two consecutive user turns inside one block.","triggerScenarios":"Calling repair_*(strict=False) on history where a user block's last message is a user message (user sent multiple messages without an assistant reply between them).","commonSituations":"Chat front-ends that let users fire messages rapidly; batch-loaded transcripts with missing assistant responses; message merging not applied before validation.","solutions":["Merge adjacent user messages into one before repair (merge_adjacent_messages exists in the module chain)","Insert a synthetic assistant acknowledgement between consecutive user turns","Drop orphan user turns that never got a response if they are noise"],"exampleFix":"# before\nrepaired = repair_without_tools(chat_history, strict=False)\n\n# after\nfrom private_gpt.components.chat.processors.chat_history.memory.utils.merge import merge_adjacent_messages\nrepaired = repair_without_tools(merge_adjacent_messages(chat_history), strict=False)","handlingStrategy":"validation","validationCode":"from private_gpt.components.chat.processors.chat_history.memory.utils.merge import merge_adjacent_messages\nhistory = merge_adjacent_messages(chat_history)  # collapses consecutive user turns\nblocks = get_user_blocks(history)\nassert all(b[-1].role in (\"assistant\", \"tool\") for b in blocks if len(b) > 1)","typeGuard":"def blocks_end_valid_nonstrict(history: list[ChatMessage]) -> bool:\n    return all(b[-1].role in (\"assistant\", \"tool\") for b in get_user_blocks(history) if len(b) > 1)","tryCatchPattern":"try:\n    repaired = repair_without_tools(chat_history, strict=False)\nexcept ValueError as e:\n    if \"does not end with an assistant or tool message\" in str(e):\n        repaired = repair_without_tools(merge_adjacent_messages(chat_history), strict=False)\n    else:\n        raise","preventionTips":["Always merge adjacent same-role messages before repair","Debounce rapid user messages in the UI so turns don't interleave","Persist an assistant ack even for errored turns"],"tags":["chat-history","validation","repair","message-order"],"backgroundTag":null,"analyzedSha":"4a030776a31a901ad80b1bf4d7faa2c1a367efbb","analyzedAt":"2026-08-15T03:51:26.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}