LLM Code Generation via Validated Example Voting
While successful, large language models (LLMs) are prone to hallucinations and subtle errors in code generation. To enhance them, approaches either rely on heavy fine-tuning or LLM guidance which is prone to accumulated errors. Inspired by counterexample-guided inductive synthesis (CEGIS), we observe that input-output examples provide a lightweight and more reliable approach to validate code generation: returning examples is simpler for LLMs and can be automatically validated through execution. However, unlike CEGIS which relies on an exact oracle, LLMs are noisy and a single query may be unreliable. We introduce validated example voting, where the oracle is queried multiple times, each response is validated through execution, and correctness is decided by majority vote. We then present COGVEX, which wraps an LLM synthesizer with this concept to provide robust code generation. COGVEX also relies on validated counterexamples to early reject future incorrect programs. We prove that COGVEX enhances the robustness of LLM code generation and that its success probability depends on the probability that the oracle returns validated examples, which tends to be high. Experimental results show that COGVEX outperforms a recent inference-time code generation algorithm by +4.3% on average and up to +10.8%, while being 8× cheaper, and improves the pass@1 of strong-reasoning LLMs by +14.3%.