TL;DR
While Large Language Models (LLMs) excel at completing code when given explicit instructions, they frequently stumble in real-world repositories where clear docstrings are missing. This paper introduces a reasoning-guided framework that trains LLMs to explicitly infer a developer's intent from implicit contextual cues—like preceding code and naming conventions—before generating the function body. By turning intent inference into a structured, step-by-step chain-of-thought process, the method achieves over 25% relative gains in functional correctness (pass@1) across multiple open-source model families.
What Problem Does This Paper Address?
AI-powered coding assistants like GitHub Copilot and Cursor are widely used for line-level and block-level code completion. However, moving up to function-level completion—generating an entire function body from scratch—remains highly unreliable when the code lacks explicit docstrings.
Standard approaches often rely on Retrieval-Augmented Generation (RAG) to pull in surrounding code context, assuming that more context equals better generation. In practice, LLMs struggle to synthesize this raw context into a coherent understanding of what the function is supposed to do. They often default to surface-level pattern matching. For instance, as shown in the motivational example below, a model might blindly serialize data to JSON because it's a common pattern, completely overlooking subtle clues in the preceding code (like a LEGACY_FORMAT_VERSION = "v1" constant) that imply the output should actually be formatted as XML.
Methods
To bridge the gap between raw context and final code, the authors propose a two-part approach: a Reasoning-Based Prompting Framework and an Intent-Interactive Pipeline.
- Reasoning-Based Prompting Framework: Inspired by how human developers read code, the framework forces the LLM to perform a structured, three-step chain-of-thought analysis before writing any code:
- Lexical Cues from Naming: Analyze the file name, function name, and argument types to form an initial hypothesis.
- Semantic Cues from Preceding Code: Summarize what the existing code does, identify key variables/helper functions, and pinpoint the remaining functional "gap."
- Inferring Intention: Synthesize these insights into a descriptive function docstring.
- Supervised Fine-Tuning (SFT): Because prompting alone is inefficient and unreliable for smaller models, the authors used GPT-4o to bootstrap a large-scale dataset containing these intermediate reasoning traces. They then fine-tuned models from the CodeLLaMA and DeepSeekCoder families to internalize this structured reasoning behavior.
- Three-Stage Intent-Interactive Framework: Building on the fine-tuned models, they designed a workflow that allows optional, lightweight human intervention. The model generates the top-$k$ candidate intentions, allowing a developer to quickly select or lightly edit the intent before the final code generation phase.
Datasets
- Training Dataset: The authors curated a new dataset of 40,000 function-level examples extracted from 400 high-quality Python repositories. The dataset was bootstrapped from 50 expert-annotated seed examples and scaled up via few-shot in-context generation using GPT-4o, followed by rigorous automated structural and semantic validation checks.
- Evaluation Benchmarks:
- DevEval: A repository-level benchmark containing Python functions equipped with unit tests. The authors filtered this down to 1,211 valid test cases.
- ComplexCodeEval: Used to test generalization, specifically focusing on a subset of 773 Python functions utilizing third-party libraries created after 2024 to avoid data contamination.
- Java Subset (ComplexCodeEval): Used to evaluate cross-language generalization.
Key Insights and Ideas
- Structured Reasoning vs. Surface Intent: Simply prompting a model to "write a docstring first" yields negligible improvements. The magic lies in the structured chain-of-thought (lexical and semantic analysis) that forces the model to actively diagnose the context before committing to an intent.
- Cross-Language Transferability: Even though the reasoning model was trained exclusively on Python code, the high-level principles of inferring intent from context transferred successfully to Java, yielding consistent performance boosts without any Java-specific fine-tuning.
- Smaller Models as Intent Modules ("Plug-and-Play"): A smaller, fine-tuned model (e.g., CodeLLaMA-7B) can serve as a lightweight "intent generator." When its inferred docstring is fed into a much larger, frozen proprietary model (like GPT-4o or o3-mini), it consistently improves the larger model's final code completion performance.
Key Results
- Substantial Performance Gains: The reasoning-enhanced models achieved over 25% relative gains in pass@1 for both the DeepSeekCoder and CodeLLaMA families. For example, on DevEval, CodeLLaMA-13B's pass@1 score jumped from 18.99% (direct completion) to 27.81%.
- Closing the Oracle Gap: When incorporating lightweight simulated human feedback (selection + editing), the framework narrowed the performance gap to the "Oracle" setting (where the model has access to the true ground-truth docstring) to within 4%, down from a baseline gap of over 15%.
- Complementary to RAG: When tested in repository-level settings alongside Retrieval-Augmented Generation, the reasoning framework provided orthogonal benefits, stacking cleanly on top of RAG baseline improvements.
Limitations and Future Work
- Fixed Reasoning Templates: The framework relies on a rigid template that may not optimally fit all coding paradigms, such as highly asynchronous utilities or low-level system code.
- Informal and Messy Contexts: Real-world repositories often contain partial specifications or misleading comments, which could confuse the semantic analysis phase. The authors plan to explore adaptive prompting to handle these noisy contexts better.
- Language Scope: While cross-language generalization was demonstrated on Java, the training data remains Python-centric. Future work will focus on expanding native support to statically typed and low-level languages.
Summary updated