ITADN

HistoryTrimmer alternation validation rejects consecutive user messages, breaking RAG context + embedding-input pattern

#533Closedviicslen 创建于 2026-04-07
V
viicslencommented
## Summary After changes in vendor/neuron-core/neuron-ai/src/Chat/History/HistoryTrimmer.php that enforce strict user/assistant alternation, NeuronAI now rejects histories containing two consecutive user messages. This breaks a previously working RAG pattern where: 1. The first user message carries extra runtime context (not intended for similarity embedding generation). 2. The second user message is the actual query intended for embedding generation. 3. Both messages are sent to the provider as chat history. ## Current behavior Given history ending with: - user: contextual instruction / runtime notes - user: final query for retrieval + inference NeuronAI fails before inference with: ``` NeuronAI\Exceptions\ChatHistoryException: Invalid message sequence at position X: expected role assistant, got user ``` ## Expected behavior Consecutive user messages should be allowed, at least optionally, for workflows where: - only the last message should drive retrieval embedding input, - but prior user-only context should still be included in provider-visible history. *In my opinion, this kind of sequence validation is better handled at the application level (or made opt-in), so developers can choose the constraints that fit their workflow.* ## Why this matters For RAG systems, this is a useful separation-of-concerns pattern: - message A = extra context/control hints, - message B = retrieval query text. Enforcing strict alternation removes this capability unless we artificially inject assistant messages, which changes semantics and adds noise. ## Minimal reproduction (conceptual) ```php $agent->chat([ new UserMessage('Additional runtime context not meant for embedding generation'), new UserMessage('Actual user query to use for retrieval'), ]); ``` **Result:** alternation validation exception before inference. --- ### Environment - NeuronAI package: `v3.3.0` - History backend: `EloquentChatHistory`
关闭于 2026-04-09 6 条评论