fix: explicit tool result instructions to guide model response

- Changed vague 'Provide your final answer now' to specific per-tool instructions
- read: 'READ THIS FILE CONTENT ALOUD to the user'
- write: 'CONFIRM to the user that the file was created'
- bash: 'SUMMARIZE the output above to answer the user's request'
- Other tools: 'Use the result shown above to answer the user's request'
- Format tool result message with clear 'Tool Result (name):' header and explicit instruction
- This should fix models ignoring tool results or giving generic responses
- All 41 tests passing
This commit is contained in:
2026-02-25 20:25:05 +01:00
parent bd3579737a
commit c46684f03e
+15 -8
View File
@@ -414,15 +414,22 @@ async def handle_chat_completion(
for i, ((tool_name, tool_result), tc) in enumerate(zip(tool_results, tool_calls_parsed)): for i, ((tool_name, tool_result), tc) in enumerate(zip(tool_results, tool_calls_parsed)):
tool_call_id = tc.get("id", f"call_{i}") tool_call_id = tc.get("id", f"call_{i}")
# For the last tool (or all tools), add instruction to stop # Format the tool result message with explicit instructions
if i == len(tool_results) - 1: # This tells the model exactly what to do with the result
tool_message_content = ( if tool_name == "read":
f"{tool_result}\n\n" instruction = "The file contents are shown above. READ THIS FILE CONTENT ALOUD to the user. Do not call additional tools."
f"IMPORTANT: You have received the tool result above. " elif tool_name == "write":
f"DO NOT call any more tools. Provide your final answer now." instruction = "The file has been successfully written. CONFIRM to the user that the file was created with the content shown above. Do not call additional tools."
) elif tool_name == "bash":
instruction = "The command has been executed. SUMMARIZE the output above to answer the user's request. Do not call additional tools."
else: else:
tool_message_content = tool_result instruction = "The tool has completed. Use the result shown above to answer the user's request. Do not call additional tools."
tool_message_content = (
f"Tool Result ({tool_name}):\n"
f"{tool_result}\n\n"
f"INSTRUCTION: {instruction}"
)
messages.append(ChatMessage( messages.append(ChatMessage(
role="tool", role="tool",