CoSQA: 20, 000+ Web Queries for Code Search and Question Answering

Junjie Huang, Duyu Tang, Linjun Shou, Ming Gong, Ke Xu, Daxin Jiang, Nan Duan

Introduction

Natural language code search is a critical tool for modern software development, allowing engineers to find relevant code snippets using plain English. However, a significant gap exists between the training data used for state-of-the-art models and the actual behavior of users. Most existing datasets rely on "pseudo-queries"—such as function documentation or lengthy Stack Overflow questions—which do not capture the concise, often ungrammatical nature of real-world search engine queries (e.g., "python check if path is absolute").

While small-scale datasets of real web queries exist, they typically contain fewer than 1,000 examples, making them suitable only for evaluation rather than training. This paper introduces CoSQA, a large-scale, human-annotated dataset designed to bridge this gap and provide a realistic foundation for training code search and question-answering models.

The CoSQA Dataset

The CoSQA dataset consists of 20,604 pairs of real Bing search queries and Python functions. These queries were specifically filtered for code-search intent. To ensure high data quality, each pair was human-annotated by at least three workers who assigned a binary label indicating whether the code snippet accurately answers the query.

Comparison of CoSQA with existing code search and question answering datasets.

As shown in the comparison above, CoSQA is significantly larger than previous human-annotated web query datasets like CodeXGLUE WebQueryTest. By providing over 20,000 annotated pairs, CoSQA allows models to learn the statistical distribution of real-world developer searches rather than relying on documentation proxies.

Methodology: Code Contrastive Learning (CoCLR)

To maximize the utility of the CoSQA dataset, the authors propose Code Contrastive Learning (CoCLR). This data augmentation technique is designed to sharpen the model's semantic representations by generating additional synthetic training instances.

The CoCLR framework, illustrating query rewriting and contrastive learning between queries and code.

CoCLR employs two primary strategies:

  1. In-Batch Augmentation (IBA): This creates synthetic "negative" examples by pairing a query with an unrelated code snippet from the same training batch. This forces the model to push their vector representations apart in the shared embedding space.
  2. Query-Rewritten Augmentation (QRA): This generates synthetic "positive" examples by slightly modifying the original query—such as switching word positions. Since web queries are often keyword-based and lack strict grammar, these variations preserve the user's intent while artificially expanding the training set.

Experimental Results

The researchers evaluated their approach using a CodeBERT base model on the CodeXGLUE WebQueryTest benchmark. The results demonstrate the power of combining high-quality data with contrastive learning:

  • Code Question Answering: Fine-tuning CodeBERT on CoSQA improved accuracy from 47.80% to 52.87%. Integrating the CoCLR method resulted in a further leap to 63.38%, representing a total improvement of over 15% from the baseline.
  • Code Search: The Mean Reciprocal Rank (MRR) increased from a baseline of 51.29% to 64.66% when using both CoSQA and CoCLR.

Ablation studies further revealed that natural language documentation within the code (docstrings and comments) serves as the most critical semantic bridge. Removing documentation led to the most significant performance drops, confirming that models rely heavily on these human-readable descriptions to link queries to logic.

Conclusion

CoSQA provides the first large-scale training resource based on real-world web queries for Python code search. By moving beyond documentation-based "pseudo-queries," the dataset enables models to better understand how developers actually search for information. Coupled with the CoCLR augmentation technique, this work sets a new state-of-the-art for code search and question answering. Future work will focus on expanding this pipeline to support multiple programming languages beyond Python.

Summary updated

BibTeX
@inproceedings{huang2021cosqa,
  author = {Junjie Huang and Duyu Tang and Linjun Shou and Ming Gong and Ke Xu and Daxin Jiang and Ming Zhou and Nan Duan},
  title = {{CoSQA: 20, 000+ Web Queries for Code Search and Question Answering}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2021},
  doi = {10.18653/v1/2021.acl-long.442},
}
Figure