← Latest papers
💬 NLP

StoryCoder: Narrative Reformulation for Structured Reasoning in LLM Code Generation

StoryCoder is a narrative reformulation framework that transforms code generation problems into coherent natural language stories containing task overviews, constraints, and test cases, which significantly improves LLM performance by guiding models toward correct algorithmic strategies and reducing implementation errors.

Original authors: Geonhui Jang, Dongyoon Han, YoungJoon Yoo

Published 2026-04-17
📖 4 min read☕ Coffee break read

Original authors: Geonhui Jang, Dongyoon Han, YoungJoon Yoo

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 trying to teach a brilliant but very literal robot how to solve a complex puzzle.

If you give the robot a standard, dry instruction like, "Calculate the sum of the max and min of all subsequences with at most k elements," the robot might get confused. It sees a list of disconnected rules and numbers, like a pile of scattered puzzle pieces. It tries to guess how they fit together, often making mistakes or getting stuck in a loop.

This is the problem the paper StoryCoder tries to solve.

The Core Idea: Turning a Manual into a Movie Script

The authors realized that humans are great at solving problems when they can tell a story about them. We don't just see "constraints"; we see "rules of the world." We don't just see "input/output"; we see "the beginning and end of a journey."

StoryCoder is a new method that takes a boring, technical coding problem and rewrites it as a coherent narrative (a story) before the robot tries to solve it.

Think of it like this:

  • The Old Way: You hand the robot a spreadsheet of rules. It has to figure out the logic on its own.
  • The StoryCoder Way: You hand the robot a short movie script. The script explains who the characters are, what the rules of their world are, and what happens in a specific scene. The robot then writes the code as if it were directing that movie.

How It Works (The Three-Act Structure)

The system doesn't just rewrite the text; it structures the story into three specific acts, guided by the type of logic the problem needs:

  1. The Setup (Task Overview): Instead of saying "Input is an array," the story says, "Elara the adventurer has a bag of magical shards..." This sets the scene and explains the goal in a way that makes sense emotionally and logically.
  2. The Rules of the World (Constraints): Instead of "1 ≤ n ≤ 100,000," the story says, "Elara's magical bag can only hold up to 100,000 shards, no matter how heavy they are." This turns abstract math limits into physical limits the robot can "visualize."
  3. The Scene (Example Input/Output): Instead of "Input: [1, 2, 3], Output: 24," the story shows a specific scene: "The Oracle gives Elara three shards worth 1, 2, and 3. She calculates the power and finds it equals 24."

Why Does This Work?

The paper argues that Large Language Models (LLMs) are trained on massive amounts of books, movies, and stories. They are experts at understanding narratives. However, they are often bad at parsing dry, fragmented technical instructions.

By wrapping the math in a story, the model:

  • Connects the dots: It sees how the "bag size" (constraint) limits the "shards" (input).
  • Picks the right tool: The story helps the model realize, "Oh, this is a 'Dynamic Programming' problem because Elara has to make a series of choices to get the best result."
  • Avoids shortcuts: Without the story, the model might try a lazy, slow solution. The story forces it to think through the logic step-by-step, like a character in a novel.

The Results: A Superpower for Robots

The researchers tested this on 11 different AI models (both open-source and big commercial ones) using three famous coding challenges (like a digital Olympics for programmers).

The Magic Numbers:

  • On average, the models got 18.7% better at solving problems correctly when using StoryCoder.
  • On the hardest problems, the improvement was even bigger.
  • The models made fewer "silly" mistakes (like writing code that runs forever) and wrote cleaner, more organized code.

The Catch: The Genre Matters

The paper also found something fascinating: The story has to fit the problem.

If you tell a math problem as a "Fantasy Adventure," it works great. But if you force a "Legal Contract" or a "Funeral Obituary" style onto a math problem, the robot gets confused and performs worse. The "flavor" of the story must match the logic of the problem.

The Bottom Line

StoryCoder is like a translator that speaks "Human Story" and "Robot Code." It realizes that to get a robot to think clearly, you sometimes have to stop talking like a computer and start talking like a storyteller.

By turning a dry list of rules into a coherent adventure, we help the AI build a better mental map of the problem, leading to smarter, more accurate code. It proves that sometimes, the best way to solve a logical puzzle is to tell a good story about it.

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 →