When Copying Is Hard: Copy-Constrained Decoding for Exact Span Reproduction
Jinghui Zhang, Lang Gao, Zongfang Liu, Ruihong Zeng, Zirui Song, Rui Yan, Kentaro Inui, Xiuying Chen
NeurIPS page · Open on the map
Category: LLM Reasoning, Memory & RAG › LLM Memory and Retrieval Mechanisms · Type: new method · Keywords: exact span reproduction, copy-constrained decoding, LLM copying, benchmark, API keys
Areas: Code & Coding Agents › Code Verification › Verified code generation; Evaluation & Benchmarks › Language Model and Agent Benchmarks › Software Engineering Benchmarks
Summary
CopyGen is a lightweight copy-constrained decoding plugin that decomposes copying into three decisions-when to copy, how far to copy, and when to stop-and improves exact span reproduction by 35% on a new benchmark without model fine-tuning.
Explained to a 5-year-old
Sometimes computers have trouble copying a piece of text exactly when they see it. They might change a letter or stop too early, which can cause problems. We made a small helper that tells the computer when to copy, how much to copy, and when to stop. This helper makes the computer copy the right words more often, like when you need to write a secret code. It helps the computer be more accurate without having to teach it new things.
Explained to a 5-year-old, longer
When we ask a computer to copy a piece of text, it should repeat it exactly as it appears. Sometimes the computer makes mistakes and changes a letter or stops before finishing. This can be a big problem when we need to write secret information like passwords or API keys. In this work, we looked at how often computers fail at copying and found they make many errors. We created a new tool that helps the computer decide when to start copying, how many words to copy, and when to stop. The tool watches each word as it is being copied and makes sure it matches the original. If the word does not match, the tool tells the computer not to continue. This way, the computer only copies the longest correct piece of text it can find. The tool works with different types of computers, like those used for reading contracts, scientific papers, and code. By using this tool, the computer copies correctly much more often than before. It also makes the copying process faster, so we don't have to wait as long. Overall, the tool makes computers more reliable when we need exact copies of text.
For a technical reader
The paper addresses a common problem where large language models often fail to copy a target string verbatim, even when it appears in the context. To solve this, the authors introduce CopyGen, a lightweight plugin that works with frozen models and decomposes copying into three decisions: when to copy, how far to copy, and when to stop. CopyGen predicts a tentative copy length, then verifies each token, adjusting the model’s logits to encourage or suppress continuation, and finally emits the longest valid prefix. On a new benchmark covering contracts, scientific literature, code, and random sequences, CopyGen improves exact match by 35% without fine-tuning and adds another 4% when combined with supervised fine-tuning. The approach also speeds up generation, making it practical for real-world tasks like embedding exact API keys in generated code.
Problem
LLMs often fail to reproduce a target span verbatim even when it appears in the context, drifting or stopping incorrectly, which limits source grounding and boundary control.
Method
The approach decomposes copying into three decisions-when to copy, how far to copy, and when to stop-by first entering copy mode, predicting a tentative copy length, and then verifying the generated span token by token while adaptively adjusting continuation logits to emit the longest valid prefix.
Results
The approach improves average exact match by 35% relative on three LLM backbones across the four benchmark tasks without model fine-tuning, and further improves by 4% when combined with SFT while accelerating generation.
Contributions
A new benchmark for exact span reproduction and a plugin that controls copying at decode time, enabling reliable verbatim span generation without model fine-tuning.
Practical relevance
It enables reliable embedding of exact strings like API keys in code generation, improving security and correctness.
This page was written by language models from the title and abstract of each paper, and the answers were checked by two other models. It can be wrong: check the paper itself. Suggest a correction