When LLMs Meet API Documentation: Can Retrieval Augmentation Aid Code Generation Just as It Helps Developers?

Jingyi Chen, Songqiang Chen, Jialun Cao, Jiasi Shen, Shing-Chi CheungarXiv 2025 · DOI

Abstract

Large Language Models (LLMs) demonstrate remarkable proficiency in coding with mainstream libraries but often falter when encountering "long-tail" or niche APIs. This research investigates whether Retrieval-Augmented Generation (RAG) can mitigate this limitation. By evaluating models on a custom dataset of 1,017 APIs across four specialized Python libraries (Polars, Ibis, GeoPandas, and Ivy), the authors find that RAG boosts performance by 83% to 220%. The study highlights that code examples are the most critical component of documentation for LLMs, while traditional keyword-based retrieval (BM25) remains surprisingly effective compared to dense semantic retrievers.

Introduction

While LLMs like GPT-4 and Qwen have "memorized" popular libraries like NumPy through extensive training, they struggle with niche, newly released, or rapidly evolving software. In professional development, these "long-tail" libraries are common, forcing developers to rely on documentation. This paper explores if LLMs can similarly leverage documentation via RAG to master unfamiliar APIs. The core inquiry focuses on which documentation elements—descriptions, parameters, or examples—provide the most utility and how resilient LLMs are to documentation noise.

Methodology

The researchers developed a systematic pipeline to evaluate RAG's impact on code generation for four niche libraries. The study introduced a dataset of 1,017 eligible APIs and 2,031 example codes extracted from official documentation.

Overview of the RAG-based code generation evaluation pipeline, from library selection to execution comparison.

The evaluation framework involved:

  1. Task Construction: Converting documentation examples into "code completion" tasks where the model must provide the correct API invocation based on context and expected output.
  2. Retrieval Scenarios: Comparing a "no-doc" baseline against "best-case" (exact document provided) and "practical" (top-5 retrieved documents) scenarios.
  3. Mutation Testing: Systematically altering documentation (e.g., deleting descriptions or renaming parameters) to identify which components are essential for model success.
  4. Retrieval Comparison: Testing traditional BM25 against dense retrievers like OpenAI's Text3 and GTE.

Results and Key Insights

The study yielded several significant findings regarding how LLMs interact with technical documentation. RAG improved LLM pass rates by 83% to 220% across the four niche libraries compared to the no-doc baseline. Stronger models like Qwen2.5-Coder-32B achieved pass rates between 66% and 82% in practical RAG scenarios.

The Primacy of Code Examples

The most critical insight is that code examples are the "secret sauce" of effective RAG for coding. As shown in the performance metrics, removing descriptive text (DelDesc) or parameter lists (DelParam) had a relatively minor impact compared to the drastic performance drop observed when code examples were deleted (DelExmpl).

Performance degradation across different documentation mutation scenarios for the Ibis library, highlighting the importance of examples.

Retrieval and Resilience

Contrary to trends favoring semantic search, BM25 outperformed dense retrievers, achieving a Recall@5 of 0.72–0.94. This is attributed to the high importance of literal token matching (e.g., specific function names) in programming tasks. Furthermore, LLMs displayed notable "noise tolerance," often successfully generating correct code despite typos or minor errors in the provided documentation, provided the overall context remained clear.

The "Copy-Paste" Trap

A notable failure mode identified was the "copy-paste" trap. Less sophisticated models tended to blindly replicate values from documentation examples (e.g., specific argument settings like padding='VALID') even when the task requirements explicitly demanded different parameters (like padding='SAME').

Conclusion

Retrieval-Augmented Generation is a powerful tool for enabling LLMs to use niche libraries, offering performance gains of up to 220%. For library maintainers, the message is clear: high-quality, executable code examples are the most valuable asset for both human developers and AI assistants. Future work should extend these findings to multi-step API chaining and explore the unique RAG requirements of specialized "reasoning" models.

Summary updated

BibTeX
@misc{chen2025when,
  author = {Jingyi Chen and Songqiang Chen and Jialun Cao and Jiasi Shen and Shing-Chi Cheung},
  title = {{When LLMs Meet API Documentation: Can Retrieval Augmentation Aid Code Generation Just as It Helps Developers?}},
  year = {2025},
  eprint = {2503.15231},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.15231},
}
Figure