NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

CodeScholar: Growing Idiomatic Code Examples

Manish Shetty, Koushik Sen, Ion StoicaarXiv 2023

Abstract

Programmers often search for usage examples for API methods. A tool that could generate realistic, idiomatic, and contextual usage examples for one or more APIs would be immensely beneficial to developers. Such a tool would relieve the need for a deep understanding of the API landscape, augment existing documentation, and help discover interactions among APIs. We present CodeScholar, a tool that generates idiomatic code examples demonstrating the common usage of API methods. It includes a novel neural-guided search technique over graphs that grows the query APIs into idiomatic code examples. Our user study demonstrates that in 70% of cases, developers prefer CodeScholar generated examples over state-of-the-art large language models (LLM) like GPT3.5. We quantitatively evaluate 60 single and 25 multi-API queries from 6 popular Python libraries and show that across-the-board CodeScholar generates more realistic, diverse, and concise examples. In addition, we show that CodeScholar not only helps developers but also LLM-powered programming assistants generate correct code in a program synthesis setting.

Introduction

When learning a new library, programmers often find that official documentation provides either "toy" examples or an overwhelming list of parameters without real-world context. While Large Language Models (LLMs) like GPT-3.5 can generate code, they frequently "hallucinate" non-existent arguments or provide generic, non-idiomatic snippets that do not reflect how experienced developers actually use those APIs in production. Existing code search tools also fall short: they either return too many results without ranking them by "naturalness" or focus on single API queries, failing to show how multiple APIs interact in common patterns. CodeScholar aims to bridge this gap by providing concise, frequent, and representative "idiomatic" examples.

Methodology: Growing Idiomatic Code

The authors treat the search for idiomatic code as a frequent subgraph mining problem. Their approach involves several key innovations to transform raw source code into searchable, idiomatic patterns.

Simplified Abstract-Syntax Trees (SAST)

To make code graphs manageable for machine learning, CodeScholar introduces the Simplified Abstract-Syntax Tree (SAST). This abstraction strips away the "noise" of a full AST while retaining critical node type and structure information. The process involves adding range information, collapsing redundant nodes, and labeling the resulting structure to focus on API interactions rather than syntax minutiae.

The transformation pipeline from a standard AST to a Simplified AST (SAST).

Instead of generating code from scratch, CodeScholar starts with "seed" snippets containing the query APIs. It then "grows" these snippets by iteratively adding nodes from the "frontier" (adjacent nodes in the source corpus) that maximize an idiomaticity metric. This metric balances frequency (how often the pattern appears) against completeness (minimizing incomplete subtrees).

The neural-guided search process growing a query (json.load) into a complete, idiomatic code block.

This search is powered by Neural Subgraph Matching, adapting a technique called NeuroMatch. Using Graph Neural Networks (GNNs), CodeScholar embeds code neighborhoods into a high-dimensional space using "order embeddings." This allows for incredibly fast, approximate subgraph searches across a massive corpus.

Evaluation and Key Results

The tool was evaluated using a source corpus mined from ~85,000 public GitHub repositories, resulting in approximately 1.4 million Python functions. The evaluation focused on 60 single-API and 25 multi-API queries across popular libraries like pandas, numpy, and torch.

  • Human Preference: In a user study with 21 developers, CodeScholar was preferred over GPT-3.5 in 68.3% of cases. Participants noted that CodeScholar examples felt more "grounded" and "meaningful."
  • Realism (EMD): Using Earth Mover’s Distance (EMD) to compare tool-generated code vs. real-world code distributions, CodeScholar achieved a lower (better) score of 0.39 compared to GPT-3.5's 0.47.
  • Boosting LLMs: When used as a retriever for a Retrieval-Augmented Generation (RAG) system, CodeScholar helped GPT-3.5 improve its performance on the ODEX benchmark, leading to a 50% relative improvement in pass@1 accuracy.

Conclusion

CodeScholar demonstrates that idiomaticity is a function of frequency and structure. By identifying the "sweet spot" between Reusability (frequency), Expressivity (behavioral depth), and Diversity, it provides examples that are more useful than those generated by general-purpose LLMs. Future work includes expanding the tool beyond Python and using LLMs to further "polish" the idiomatic snippets retrieved by the search engine.

Summary updated

BibTeX
@misc{shetty2023codescholar,
  author = {Manish Shetty and Koushik Sen and Ion Stoica},
  title = {{CodeScholar: Growing Idiomatic Code Examples}},
  year = {2023},
  eprint = {2312.15157},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2312.15157},
}
Figure