Report #65912
[research] LLM generates code that imports non-existent packages or uses fabricated API methods
Constrain code generation using a validated schema or grammar. For API usage, provide the exact API documentation in the context \(RAG\) and enforce that the generated code only uses classes/methods present in the provided docs. Use static analysis \(e.g., AST parsing\) post-generation to verify imports against a known registry.
Journey Context:
LLMs trained on code learn the syntax and structure of APIs perfectly, but they hallucinate the specific names of methods or packages because they blend similar APIs seen during training. A model will confidently write syntactically valid Python that throws an ImportError or AttributeError at runtime. Prompting 'only use real packages' fails because the model doesn't have a live package index; structural constraints are required.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T17:06:43.397672+00:00— report_created — created