← Latest papers
💻 computer science

Reducing Hallucinations in LLM-Generated Code via Semantic Triangulation

This paper introduces "semantic triangulation," a framework that reduces LLM code hallucinations by generating and cross-verifying solutions to dissociative problem variants, thereby significantly improving the selection of correct programs across multiple benchmarks and models compared to existing proxy-based methods.

Original authors: Yihan Dai, Sijie Liang, Haotian Xu, Peichu Xie, Sergey Mechtaev

Published 2026-03-31
📖 4 min read☕ Coffee break read

Original authors: Yihan Dai, Sijie Liang, Haotian Xu, Peichu Xie, Sergey Mechtaev

Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer

Imagine you are a detective trying to solve a crime. You have a lineup of suspects (these are the AI-generated code snippets). Your goal is to find the one innocent person (the correct code) among a crowd of liars (the buggy code).

The Problem: The "Colluded Alibi"

Usually, when we ask an AI to write code, it might make mistakes. These mistakes aren't random; they are often hallucinations—the AI confidently invents something that sounds right but is wrong.

If you ask the AI to write the same code 10 times, it might make the exact same mistake 9 times.

  • Old Method (Plurality Voting): You ask all 10 versions, "Who is innocent?" If 9 of them say, "I did it this way," and only 1 says, "I did it that way," you pick the group of 9.
  • The Flaw: This is like asking a group of suspects who have already met in a back room to agree on a fake alibi. They all tell the same lie, so the "majority" vote picks the liar, not the truth.

Other methods try to check the code by asking the AI to write its own test cases or rules. But since the AI wrote the rules and the code, it's like asking a suspect to write their own witness statement. They will just write a story that makes their lie look true.

The Solution: Semantic Triangulation

The authors of this paper introduce a new strategy called Semantic Triangulation. Think of it as a detective who doesn't just ask the suspects for their story, but changes the entire game to expose the liars.

Here is how it works, using a simple analogy:

1. The "Dissociative" Transformation (Changing the Angle)

Instead of asking the AI to solve the problem directly, the system asks it to solve a completely different version of the same problem.

  • Original Task: "Here is a recipe (input); please bake a cake (output)."
  • The Twist: "Here is a cake (output); please write the recipe that made it (inverse)."

Why do this? Because the AI has to use a different part of its brain to solve the inverse problem.

  • If the AI hallucinates (makes up a fake recipe) for the first task, it's unlikely to accidentally hallucinate the exact same fake recipe when asked to reverse-engineer the cake.
  • The "liars" (bugs) get confused because the angle of attack is totally different.

2. The "Bijection" (The Perfect Match)

The system ensures that for every correct cake, there is exactly one correct recipe, and vice versa. It's a one-to-one match.

  • If the AI gets the cake right, it must get the recipe right to match.
  • If the AI gets the cake wrong, the recipe it generates won't match the cake it made.

3. The "Cross-Check" (Triangulation)

The system generates:

  1. A solution to the original problem (The Cake).
  2. A solution to the inverse problem (The Recipe).
  3. It checks: Does this Recipe actually make this Cake?
  • If they match: It's a strong signal that both are likely correct. The AI didn't just guess; it understood the relationship between the two.
  • If they don't match: The AI is hallucinating. The system discards the code.

Why This is a Big Deal

The paper tested this on some of the hardest coding puzzles available.

  • The "Low Probability" Win: Sometimes, an AI only gets the right answer 7% of the time. Old methods would give up or pick the wrong answer 93% of the time. This new method found the correct answer in those rare cases by cross-checking with the inverse problem.
  • The "Inexact" Win: Some problems have multiple correct answers (e.g., "Write a script that sorts a list" could be done in many ways). Old methods get confused here. This method handles them perfectly because it checks the logic of the relationship, not just the specific output.

The Bottom Line

Semantic Triangulation is like a detective who stops asking suspects for their stories and instead asks them to solve a puzzle that requires a completely different skill set. If the suspect can solve the puzzle and their story matches the solution, they are probably telling the truth. If they can't, they are caught in a lie.

This allows us to trust AI-generated code much more, even when the AI is struggling, by forcing it to prove its work from a different, unexpected angle.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →