0tokens

Apply for AI Grants India

Financial support for innovators building the future of AI in India.

Apply now

Chat · hallucinated imports detection

Hallucinated Imports Detection in AI Code: A Practical Guide

  1. aigi

    AI-generated code can look complete while importing a package that was never published, a module that belongs to another ecosystem, or an API that does not exist in the stated version. Hallucinated imports detection is the process of finding these invented or invalid dependencies before they break builds, create security exposure, or silently alter application behaviour.

    The problem is especially relevant for teams using coding assistants to generate Python, JavaScript, Java, or infrastructure code. A plausible-looking import may pass a superficial review yet fail only in a deployment environment. In regulated or high-impact systems, the consequences can extend beyond a failed build: an engineer may install a similarly named malicious package, or a fallback implementation may produce incorrect results.

    What counts as a hallucinated import?

    An import is hallucinated when AI-generated code refers to a dependency, module, symbol, or version that cannot be verified in the target environment. Common examples include:

    • A Python statement such as from fastapi.security import OAuthHandler when that symbol is not part of the installed FastAPI version.
    • A JavaScript import from a package name that is not registered in npm.
    • A valid package paired with a fictional submodule or undocumented function.
    • A dependency that exists publicly but is incompatible with the project’s runtime, operating system, or lockfile.
    • A local import whose file path, case, or package structure does not exist in the repository.

    This is different from an ordinary syntax error. The code may be syntactically valid and may even resemble established documentation. Detection therefore needs both machine checks and disciplined review.

    Why detection matters

    The immediate cost is developer time: broken builds, repeated prompts, and debugging detours. The larger risks are more serious:

    • Supply-chain attacks: Developers may install a similarly named package to satisfy an invented import. This is a known typosquatting and dependency-confusion risk.
    • Unreliable releases: A generated dependency can work on one machine and fail in CI, a container, or an Indian cloud region with a different base image.
    • Security and compliance gaps: Unapproved packages can bypass Software Bill of Materials (SBOM), licence, and vulnerability review.
    • Incorrect model or product behaviour: A replacement library may expose subtly different defaults, affecting financial, healthcare, or operational decisions.
    • Maintenance debt: Unsupported packages make future upgrades and incident response harder.

    The same principle applies to AI systems that generate detection pipelines. Teams working on open-source malware detection using machine learning, for example, should verify every imported scanner, feature library, and model wrapper rather than trusting generated code because it appears technically sophisticated.

    A practical detection workflow

    1. Parse imports before executing code

    Start with language-aware parsers or linters. For Python, inspect the abstract syntax tree and collect import and from ... import ... statements. For JavaScript and TypeScript, analyse ES modules, CommonJS calls, and dynamic imports. Avoid executing untrusted generated code merely to discover missing dependencies.

    Flag:

    • Imports that are not declared in requirements.txt, pyproject.toml, package.json, pom.xml, or the equivalent manifest.
    • Relative paths that resolve outside the repository or point to missing files.
    • Duplicate packages with conflicting names.
    • Dynamic imports assembled from unchecked user input.

    2. Verify package identity and availability

    Resolve each external import against the project’s configured registry, not an arbitrary public registry. Confirm the exact package name, publisher, version, licence, release date, and repository. A package being available on PyPI or npm does not prove that it is the intended dependency.

    Use lockfiles and hashes wherever possible. In CI, install with commands that enforce reproducibility, such as Python constraints files or npm’s frozen lockfile mode. Do not “fix” an import by installing the first similarly named result returned by search.

    3. Check symbols against installed versions

    Package-level validation is insufficient. An AI assistant may identify a real library but invent a class or function. Import checks should therefore run in a clean environment and verify the requested symbol:

    • Create an isolated virtual environment or container.
    • Install only locked dependencies.
    • Compile or type-check the project.
    • Import modules and inspect expected symbols.
    • Run a minimal smoke test for critical integrations.

    For typed code, TypeScript, mypy, pyright, Java compilation, and IDE language servers can catch many invalid members before runtime. Generated API clients should be checked against the actual service schema or SDK version.

    4. Compare claims with authoritative documentation

    Search the package’s official documentation, source repository, and release notes. Prefer versioned documentation and primary sources over blog posts or snippets. Record evidence for non-obvious dependencies in the pull request so reviewers can reproduce the decision.

    This is also where teams should distinguish an unavailable import from an internal module. In Indian startups with several services, a module may be valid only inside a private monorepo. A repository-wide dependency graph and ownership metadata prevent internal names from being mistaken for public packages.

    5. Test in a clean, production-like environment

    A developer laptop may hide errors through globally installed packages or cached builds. Run generated code in a fresh container that matches production’s Python or Node version, operating system, CPU architecture, and environment variables. Rebuild from the lockfile rather than copying an existing virtual environment.

    For computer-vision workloads, hardware differences matter too. Teams deploying efficient real-time object detection on low-power hardware should validate imports inside the actual ARM, edge, or GPU image; a dependency that loads on x86 development machines may not support the target device.

    Controls for CI and code review

    A reliable pipeline should treat hallucinated imports as a supply-chain and correctness check, not just a lint warning. Useful controls include:

    • Block undeclared dependencies and unresolved imports.
    • Run dependency resolution with network access restricted to approved registries.
    • Generate an SBOM and scan packages for known vulnerabilities and licences.
    • Require human approval for new dependencies, native extensions, and post-install scripts.
    • Use pre-commit hooks for syntax, type, import, and secret checks.
    • Preserve build logs showing the exact interpreter, package manager, and lockfile.
    • Add regression tests whenever an AI-generated import caused an incident.

    A review checklist should ask: Does the package exist? Is this the correct publisher? Does the symbol exist in the pinned version? Is the dependency necessary? Can the code run in a clean production image?

    Handling a detected hallucination

    Do not immediately substitute a package with a similar name. First identify the intended capability, then choose one of three safe responses:

    1. Replace the import with a verified API from an approved dependency.
    2. Implement the small required function locally if that reduces supply-chain risk.
    3. Remove the feature and ask the model to regenerate against supplied documentation.

    Quarantine generated code that has already introduced an unverified package. Review package-manager logs, lockfile changes, network activity, and CI artefacts. If the package was installed on a developer or production system, treat it as a potential security event and rotate exposed credentials where appropriate.

    Measuring the process

    Track unresolved-import rate, time to remediation, new dependency approval time, failed clean-environment builds, and the percentage of AI-generated pull requests passing without manual dependency changes. These measures show whether controls improve delivery rather than simply adding friction.

    As of 2026, the strongest practice is a layered one: constrain generation with repository context, verify imports statically, resolve dependencies reproducibly, test in isolation, and keep a human accountable for approval. No single scanner can establish that generated code is safe or correct.

    FAQ

    Can a hallucinated import be detected without running the code?
    Often, yes. AST parsing, manifest comparison, registry checks, type checking, and documentation verification can identify many cases without executing untrusted code. Runtime smoke tests are still useful for version and environment-specific failures.

    Should teams allow AI tools to install packages automatically?
    Generally no. Package installation should use approved registries, lockfiles, vulnerability scanning, and human review. Automatic installation increases the chance of dependency confusion and typosquatting.

    Does a successful import prove the code is correct?
    No. A package or symbol may exist but behave differently from the generated code’s assumptions. Tests, API-contract checks, and production-like validation remain necessary.

    How does this apply to AI and computer vision projects?
    The same controls apply to model libraries, data connectors, hardware runtimes, and internal utilities. For example, teams building real-time anomaly detection in surveillance video AI should validate both software imports and model/runtime compatibility on deployment hardware.

    Apply for AI Grants India

    If your Indian startup is building developer-security tooling, trustworthy AI infrastructure, or automated dependency verification, explore funding and support through AI Grants India. Strong applications should show a defined risk, measurable validation method, pilot users, and a credible path from prototype to deployment.

    Last updated 24 September 2026

AIGI may be inaccurate. Replies seeded from the guide above.