LearnThatStack Ace your next interview
AI Coding Assistants · question
Question 6 of 51

What is a hallucinated API or package, and how would you catch one?

beginner
← All AI Coding Assistants questions
Re-explain

A hallucinated API or package is a name that the model invents even though it does not exist. It may suggest a believable method, configuration option, or dependency because the name fits common patterns. The generated code can appear correct until it is compiled or run.

  • Use the compiler, type checker, and language server as the first check.
  • For dynamic languages, run focused tests immediately.
  • Confirm unfamiliar methods in the official documentation for the version your project uses.
  • Before installing a suggested package, inspect its official registry page, repository, maintainers, release history, and usage. Attackers sometimes register names that models often invent.
  • Require human approval for new dependencies, especially when an agent can run package-install commands.
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for AI Coding Assistants One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • What is slopsquatting, and how do you defend against it? Attackers register package names models tend to invent. Check the package's age, maintainers and downloads, and require review for new dependencies.
  • An agent in your setup can run package installs. What guardrails would you put around that? Human approval for installs, an allowlist or internal registry, lockfile diffs in review, and a sandbox without secrets since install scripts can run code.
  • Why are hallucinated methods harder to catch in Python or JavaScript than in Java or TypeScript? No compile step, so the error shows up only when that line runs. Tests that hit that path, plus type hints and a type checker, catch most of these.
  • The method is real, but only in a newer version than the one you use. How do you catch that? Read the docs for the pinned version, trust the installed package's type definitions, and run the build and tests against the lockfile.
  • How would you make the model less likely to invent an API in the first place? Give it real context such as docs, type definitions and existing call sites, and let it run the compiler or tests in a loop to fix its own errors.

What you can say

  1. A hallucinated API or package is a name the model made up. It's a method, config option or dependency that sounds right because it fits common patterns, but it doesn't exist.
  2. The trouble is that the code looks correct, so you usually find out only when you compile or run it.
  3. My first check is the compiler, type checker and language server, which flag an unknown method right away. In Python or JavaScript I run a focused test immediately.
  4. If a method is unfamiliar, I look it up in the official docs for the version our project actually uses.
  5. Packages need more care, because attackers register names that models tend to invent. Before installing one, I check its registry page, repo, maintainers, release history and usage.
  6. It adds some friction, but I'd require a human to approve any new dependency, especially when an agent can run install commands on its own.

Weak answers to avoid

  • Says 'just review the code carefully' A made-up method reads as plausible, so eyes miss it. Name the tools that catch it: compiler, type checker, language server, and a quick test run.
  • Treats it as only a correctness bug A made-up package name can point to a real malicious package someone registered. Mention checking the registry and requiring approval for new dependencies.
  • Trusts the package because it installed A successful install proves the name exists, not that it is the right or safe package. Check the maintainers, release history, repo and usage.
  • Checks the latest docs, not your version A method can exist in one version and not in the one you pin. Look it up in the official docs for your project's version.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the AI Coding Assistants cheatsheet.

← Back to all AI Coding Assistants questions
Pro · $10/mo

44 of 51 AI Coding Assistants answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime