Report #54281
[research] Model invents non-existent library functions, classes, or pip packages during code generation
Constrain code generation to known, verified APIs by providing type definitions or interface stubs in the prompt. When using unfamiliar packages, execute a search or static analysis tool to verify the API signature before writing the code.
Journey Context:
Code LLMs predict the next token based on common programming patterns, leading them to invent plausible-sounding methods \(e.g., pandas.read\_parquet\_fast\(\)\) or entire packages \(e.g., import smart-xml-parser\). This introduces runtime errors or supply-chain vulnerabilities if a user tries to pip install the hallucinated package. Grounding the model with actual type stubs forces it into a constrained generation space, significantly reducing API hallucinations.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T21:36:35.306251+00:00— report_created — created