Improper construction of the message list (ChatClientLlmOperations.buildInitialMessagesForToolLoop)
There is a slight non-standard message sequencing in the current implementation.
According to the OpenAI documentation, placing a system message in the middle of a dialogue can lead to instruction drift. This often causes the model to prioritize the final instructions while losing track of the foundational rules set at the start.
Furthermore, this structure poses a risk to inference reliability. Other models (such as DeepSeek, which I am currently using) may fail to produce normal results or even trigger validation errors when the chronological flow is disrupted.
It is better to consolidate these instructions into a single system prompt at the beginning. This ensures a consistent persona and maintains robust reasoning logic across different models.
https://github.com/embabel/embabel-agent/blob/main/embabel-agent-api/src/main/kotlin/com/embabel/agent/spi/support/springai/ChatClientLlmOperations.kt
/**
* Build initial messages for the tool loop, including system prompt contributions and schema.
*/
private fun buildInitialMessagesForToolLoop(
promptContributions: String,
messages: List<Message>,
schemaFormat: String? = null,
): List<Message> {
return buildList {
if (promptContributions.isNotEmpty()) {
add(EmbabelSystemMessage(promptContributions))
}
addAll(messages)
if (schemaFormat != null) {
add(EmbabelSystemMessage(schemaFormat)) <-- the second system message
}
}
}
Good point. Will also lead to better prompt caching.