From 8aeee44368b08f54be9c04c11811b35373155072 Mon Sep 17 00:00:00 2001 From: Catherine Renelle <32781029+catrenelle@users.noreply.github.com> Date: Wed, 11 Feb 2026 21:44:39 -0500 Subject: [PATCH] Add logging and retry-without-tools for empty AI responses When the model returns empty content with page context present, the 15 tool definitions likely overwhelm the model's attention. Now on empty response, the request is retried with tools stripped so the model focuses on answering from the page data. Also added diagnostic logging for page context receipt and empty response details. Co-Authored-By: Claude Opus 4.6 --- src/Purrse.Api/Services/ChatService.cs | 35 +++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Purrse.Api/Services/ChatService.cs b/src/Purrse.Api/Services/ChatService.cs index 87f41d6..cc1afda 100644 --- a/src/Purrse.Api/Services/ChatService.cs +++ b/src/Purrse.Api/Services/ChatService.cs @@ -171,6 +171,13 @@ public class ChatService : IChatService if (!string.IsNullOrEmpty(request.PageContext)) { userContent = $"{request.Message}\n\n[Current page data]\n{request.PageContext}"; + _logger.LogInformation("Page context attached — {Length} chars, preview: {Preview}", + request.PageContext.Length, + request.PageContext.Length > 200 ? request.PageContext[..200] + "..." : request.PageContext); + } + else + { + _logger.LogInformation("No page context attached to message"); } ollamaMessages.Add(new OllamaToolMessage { Role = "user", Content = userContent }); @@ -248,9 +255,31 @@ public class ChatService : IChatService } else { - var rawContent = string.IsNullOrWhiteSpace(response?.Message?.Content) - ? "I'm sorry, I couldn't process that request." - : response!.Message!.Content; + var rawContent = response?.Message?.Content; + + // If the model returned empty content and page context is present, + // the tool definitions may be overwhelming the model. Retry without + // tools so it can focus on answering from the page data. + if (string.IsNullOrWhiteSpace(rawContent) && !string.IsNullOrEmpty(request.PageContext) && i == 0) + { + _logger.LogWarning("Model returned empty content with page context present — retrying without tools"); + var retryRequest = new OllamaToolChatRequest + { + Model = modelName, + Messages = ollamaMessages, + Stream = false, + Tools = null // drop tools so the model focuses on the page data + }; + var retryResponse = await CallOllamaAsync(ollamaUrl, retryRequest); + rawContent = retryResponse?.Message?.Content; + } + + if (string.IsNullOrWhiteSpace(rawContent)) + { + _logger.LogWarning("Model returned empty content (no tool calls). ToolCalls: {ToolCalls}, Done: {Done}", + response?.Message?.ToolCalls?.Count ?? -1, response?.Done); + rawContent = "I'm sorry, I couldn't process that request."; + } // If the model responded without calling any tools on the first attempt // AND there are no prior tool results AND no page context data, it