Abstract
Large Language Models (LLMs) often struggle with code generation for unfamiliar or newly released APIs, a challenge known as library-oriented code generation. While Retrieval-Augmented Generation (RAG) provides documentation, it frequently fails on complex tasks requiring multiple API calls due to documentation ambiguity and cascading hallucinations. We present ExploraCoder, a training-free framework that mimics human exploratory programming. By decomposing complex requirements into subtasks and executing experimental code in a sandbox, ExploraCoder leverages execution feedback to guide final solution generation. Our results demonstrate significant performance improvements over standard RAG and agentic baselines on complex multi-API benchmarks.
Introduction
Modern LLMs excel at generating code for libraries present in their pretraining data. However, their performance degrades significantly when encountering "unseen" APIs—those that are private, updated, or released after the model's knowledge cutoff. The standard solution, Retrieval-Augmented Generation (RAG), involves fetching relevant documentation to augment the prompt. While effective for simple tasks, RAG struggles with complex requirements that necessitate chaining multiple unfamiliar APIs. Documentation is often insufficient to resolve ambiguities regarding return types or data formats, leading to early hallucinations that propagate through the autoregressive generation process.
Existing methods like iterative self-repair or agentic loops attempt to fix errors after a full script is generated. However, debugging a complex, multi-API script with multiple interacting errors is often intractable for LLMs.
The ExploraCoder Framework
ExploraCoder addresses these challenges by adopting an "Exploratory Programming" paradigm. Instead of attempting to generate a complete solution in one step, the framework follows a structured, step-wise workflow designed to gather practical knowledge through execution.
The workflow consists of four primary stages:
- Task Planning: The LLM analyzes a high-level library summary to decompose the user's requirement into a sequence of simpler API invocation subtasks ($t_1, t_2, \dots, t_n$).
- API Recommendation: For each subtask, a retriever identifies and fetches the most relevant API documentation.
- Chain of API Exploration (CoAE): This core component involves generating and executing small "experimental" code snippets for each subtask. The LLM observes the execution output (e.g., object types, data shapes) or error messages. This "experience" is carried forward to inform subsequent subtasks.
- Final Solution Generation: The LLM synthesizes the final, complete code solution using the trace of successful, verified API interactions.
An enhanced version, ExploraCoder*, incorporates intermediate self-debugging. If an experimental snippet fails, the model attempts to fix it immediately, preventing error accumulation before proceeding to the next subtask.
Experimental Evaluation
To evaluate ExploraCoder, the researchers utilized the Torchdata library, which was largely unseen by models like GPT-3.5 and GPT-4-0613. They tested on three benchmarks:
- Torchdata-Github: 50 tasks from real-world projects (3-8 APIs per task).
- Torchdata-Manual: A new, highly complex benchmark of 100 tasks requiring 8-14 distinct APIs, representing the longest API sequences in current execution-based evaluations.
- MonkBeatEval: A benchmark using pseudo-libraries to test generalizability to completely fabricated APIs.
Results and Discussion
ExploraCoder demonstrates substantial improvements over existing state-of-the-art methods. On the Torchdata-Manual benchmark, which specifically targets long-chain API generation, ExploraCoder and its self-debugging variant (ExploraCoder*) significantly outperformed both standard RAG and reactive agentic frameworks.
As shown in the results, ExploraCoder achieved a 24.26% pass@10* rate on Torchdata-Manual, while standard RAG (DocPrompting) achieved only 4.68%. Agentic frameworks like ReAct and KnowAgent, which rely on reactive planning, scored 7.43% and 13.51% respectively. This highlights the effectiveness of structured, proactive exploration over reactive debugging.
Key insights from the evaluation include:
- Execution Feedback is Crucial: Observing actual return types and data formats resolves documentation ambiguities that text alone cannot.
- Prevention is Better Than Cure: Debugging small, 3-line snippets during exploration is far more effective than trying to untangle a large, hallucinated script.
- Efficiency: Despite its multi-step nature, ExploraCoder is more token-efficient than many agentic baselines because it avoids chaotic debugging loops.
Conclusion
ExploraCoder provides a robust framework for library-oriented code generation by mimicking the human developer's process of tinkering and exploration. By grounding the generation process in execution feedback at each step, it successfully bridges the gap between static documentation and practical API usage. Future work will focus on developing intelligent early-termination mechanisms for unsolvable tasks and reducing reliance on high-quality initial documentation.
Summary updated