From e65385c15166380336dd4bd94da99e8f28182b9e Mon Sep 17 00:00:00 2001 From: Paul Kompfner Date: Mon, 8 Sep 2025 13:55:09 -0400 Subject: [PATCH] Tweak the Mistral-specific context messages fixup logic to handle the (mostly academic) possibility of a "tool" message appearing at the end --- src/pipecat/services/mistral/llm.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pipecat/services/mistral/llm.py b/src/pipecat/services/mistral/llm.py index eb0e82f51..559c7eb05 100644 --- a/src/pipecat/services/mistral/llm.py +++ b/src/pipecat/services/mistral/llm.py @@ -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": " "})