AI Coding in Practice: Generating a Complex Desktop Tool with Zero Errors
This article examines why generated code often fails to become usable desktop software, and how to close that gap. Using a batch audio extraction tool as a sample, it covers writing engineering-grade prompts with five categories of constraints, the thread model that keeps a GUI responsive, a robustness checklist for handling malformed inputs, and a three-step verification process. The goal is a repeatable path to structurally complete, runnable, and maintainable tool projects.
1. Why “Generated Code” Is Not the Same as Usable Software
Code generation has improved quickly over the past two years, yet the gap developers hit in real projects is still obvious. A few dozen lines of script may pass on the first try in a demo, but the moment the target is a desktop tool with a graphical interface that has to handle files and external dependencies, syntax errors, missing dependencies, and frozen interfaces appear frequently.
The reason is not mysterious. The complexity of a desktop tool lies not in algorithms but in the boundaries: differences between file formats, path anomalies, insufficient permissions, external programs that are not installed, and the relationship between long-running tasks and the UI thread.
So the maturity of a model or an AI coding toolset cannot be judged by whether it can complete a function. It has to be judged by whether it can deliver a structurally complete, runnable, and maintainable project skeleton when requirements are vague and constraints are numerous. This article uses a “batch audio extraction tool for video files” as the sample and discusses the general technical points of such tasks.
2. Stating Requirements Clearly: An Engineering Approach to Prompts
Most failures come not from limited model capability but from requirements expressed too broadly. “Write a conversion tool” and “which formats are supported, how progress is reported, what happens on errors” are separated by a whole set of engineering conventions. A reusable requirements template contains five categories of information:
- Tech stack constraints: language, GUI framework, whether third-party libraries are allowed;
- Input/output contract: supported extensions, export formats, naming rules;
- Interaction completeness: import, start, cancel, exit, log viewing;
- Non-functional requirements: responsiveness, recoverability, portability;
- Acceptance criteria: zero-error compilation, no crash on empty directories or corrupted files.
An example prompt (illustrative):
Build a batch audio track extraction tool with Python and the standard-library tkinter:1) Support common video extensions and export MP3 / WAV;2) Provide file and directory import, format selection, and start and exit buttons;3) Run conversion in a background thread; the main thread only refreshes the UI;4) Show overall progress and the current file status; log failures and continue;5) Append a sequence number automatically to duplicate output names; check at startup whether the external transcoder is available;6) Do not depend on uncommon third-party libraries; include run instructions.
The more specific the constraints, the less the model has to guess, and the higher the chance of passing on the first attempt.
3. The Technical Root Cause of a Frozen UI: The Thread Model
“A smooth-running interface” is the pitfall beginners fall into most easily. GUI frameworks generally require all interface updates to happen on the main thread, while transcoding is a time-consuming task. If the transcoding loop is written directly into a button callback, the main thread is occupied for a long time, the window cannot repaint or respond, and the result looks like a freeze.
The correct approach is a producer–consumer model: the main thread collects user actions, and worker threads execute tasks and send progress back through a queue.
import queue, threadingq = queue.Queue()def worker(tasks, out_dir):for i, path in enumerate(tasks, 1):# Call the external transcoder; illustrative onlyok = transcode(path, out_dir)q.put(("progress", i, len(tasks), path, ok))q.put(("done",))def poll(root, bar, status):try:while True:msg = q.get_nowait()if msg[0] == "progress":_, i, n, path, ok = msgbar["value"] = i * 100 / nstatus.config(text=f"{i}/{n} {path} {'OK' if ok else 'FAILED'}")else:status.config(text="All done")breakexcept queue.Empty:root.after(100, poll, root, bar, status)
There are three key points: tasks run on a background thread; interface updates return to the main thread through a queue; and timed polling is used instead of blocking waits. If the task volume is large, a thread pool can limit concurrency so the disk and processor are not saturated and the whole system does not slow down.
4. A Robustness Checklist: Making the Tool Survive Real Usage
Real users will import empty directories, corrupted files, extremely long paths, and directories without write permission. A deliverable tool should at least cover:
- Dependency pre-check: verify at startup that the external transcoder exists, and give clear guidance when it is missing instead of failing silently;
- Input validation: filter by extension, skip unsupported items, and count them;
- Naming strategy: append a sequence number or timestamp to duplicate output names to prevent overwriting;
- Error isolation: a single file failure does not affect the whole run, and errors are written to a log;
- Cancellation: provide a stop button that sends a cancel flag to the worker thread;
- Resource release: close child processes and file handles promptly.
def safe_out_path(out_dir, name):base, ext = name.rsplit(".", 1)candidate = f"{base}.{ext}"idx = 1while os.path.exists(os.path.join(out_dir, candidate)):candidate = f"{base}_{idx}.{ext}"idx += 1return candidate
5. Verification Is Not Optional: Compile, Dry Run, and Sample Regression
After code is generated, always run three verification steps: first a syntax compilation check; then a dry run with a few sample files while watching progress and logs; finally, construct abnormal samples (empty files, wrong extensions, read-only directories) to verify the fallbacks. Only when the “exception paths” also pass can you talk about zero-error delivery.
6. Turning Practice into Team Standards
For individual developers, maintain a requirements template and an acceptance checklist. For teams, the template can be fixed as an internal standard and combined with the general container platform, logging service, and monitoring and alerting capabilities on Baidu AI Cloud, bringing tool usage and failure rates into an observability system and forming a “generate–verify–observe–iterate” loop. The value of a tool lies not in how impressive a single generation looks, but in passing acceptance reliably every time.
