Tweak the Mistral-specific context messages fixup logic to handle the (mostly academic) possibility of a "tool" message appearing at the end

This commit is contained in:
Paul Kompfner
2025-09-08 13:55:09 -04:00
parent 0bb7df7a6b
commit e65385c151

View File

@@ -84,9 +84,11 @@ class MistralLLMService(OpenAILLMService):
# Ensure all tool responses are followed by an assistant message
assistant_insert_indices = []
for i, msg in enumerate(fixed_messages[:-1]):
if msg.get("role") == "tool" and not fixed_messages[i + 1].get("role") == "assistant":
assistant_insert_indices.append(i + 1)
for i, msg in enumerate(fixed_messages):
if msg.get("role") == "tool":
# If this is the last message or the next message is not assistant
if i == len(fixed_messages) - 1 or fixed_messages[i + 1].get("role") != "assistant":
assistant_insert_indices.append(i + 1)
for idx in reversed(assistant_insert_indices):
fixed_messages.insert(idx, {"role": "assistant", "content": " "})