holmesgpt icon indicating copy to clipboard operation
holmesgpt copied to clipboard

Final answer is lost when the model's closing turn is empty (analysis comes back empty)

Open mdecalf opened this issue 5 months ago • 1 comments

What happened?

The agentic loop in ToolCallingLLM.call_stream returns terminal_data["content"] — the content of the LLM's final turn — as the answer (LLMResult.result, and therefore ChatResponse.analysis for /api/chat).

Some providers (observed with claude-sonnet-4-6 via litellm) emit their answer in a turn that also carries a trailing tool call (e.g. TodoWrite), and then close the loop with an empty turn (content=None / "", no tool calls). The loop terminates on that empty turn, so the empty content is returned and the real answer the model already produced is dropped.

Concretely, in the logs the model clearly synthesises an answer:

AI: ## Summary
... <full answer> ...

…but /api/chat returns analysis = "". The investigation did run (tool calls executed, results returned); only the final summarisation turn came back empty.

What did you expect to happen?

The answer the model produced should be returned, not silently discarded. An empty closing turn after a substantive answer should not erase that answer.

How can we reproduce it (as minimally and precisely as possible)?

  1. Ask /api/chat a question that triggers a multi-step investigation (tool calls), with claude-sonnet via litellm.
  2. Occasionally the model emits its final summary in a turn that also requests one last tool call (e.g. TodoWrite), then produces an empty closing turn (content=null, no tool calls).
  3. call_stream yields ANSWER_END with content = response_message.content from that empty turn → result / analysis is empty, even though the summary was generated one turn earlier.

Anything else we need to know?

Related: this is the same family as #1676. #1789 stopped the empty content from crashing /api/chat (analysis is now coerced to "" instead of raising a 500), but the answer is still lost — clients receive an empty response and may pointlessly re-run the whole investigation (extra latency + cost).

Proposed fix in #2184: track the last non-empty assistant text across iterations and fall back to it when the terminating turn has no text.

mdecalf avatar Jun 11 '26 20:06 mdecalf

🔗 Related PRs

HolmesGPT/holmesgpt#1973 - Fix empty user message appended on tool approval resume [merged] HolmesGPT/holmesgpt#2097 - Auto-deny orphaned tool calls to prevent LLM failures [merged]


📝 Issue Planner

Check the box below or use the @coderabbitai plan command to generate an implementation plan and prompts that you can use with your favorite coding assistant.

  • [ ] Create Plan

🧪 Issue enrichment is currently in open beta.

You can configure auto-planning by selecting labels in the issue_enrichment configuration.

To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

issue_enrichment:
  auto_enrich:
    enabled: false

💬 Have feedback or questions? Drop into our discord!

coderabbitai[bot] avatar Jun 11 '26 20:06 coderabbitai[bot]

This appears to hit ToolCallingLLM.call_stream choosing the terminal empty model turn as the returned answer. The concrete failure mode is that a prior assistant turn can contain both the full summary and a trailing tool call, while the closing turn has content=None or "" and no tool calls. For a PR, I would start by preserving the last non-empty assistant content across the tool loop and using it when the terminal turn is empty.

saisharan0103 avatar Jul 12 '26 22:07 saisharan0103