Sub topic 7
Checking that packages and APIs are real
This chapter assumes you can read a Git diff, use a package manager such as pip, and run a Python script. Its subject is one failure mode: AI assistants sometimes use packages, functions, or parameters that do not exist, or that exist but do not do what the code assumes.
Why this failure happens
A language model predicts text that looks right. It has no live index of what a package actually exposes. It can therefore write a call like requests.get_json(url) that reads naturally but is not part of the library. Three problems recur:
- Invented API. The package exists, but the function, method, or argument does not.
- Invented package. The import names a package that is not published.
- Wrong version. The API existed in an older release, was renamed, or was removed.
The second problem is also a security risk. Attackers can register a plausible but unpublished package name and wait for someone to install it. This is called slopsquatting. Installing a package only because an AI named it is a supply-chain risk.
Step 1: List what is new
Find the imports and dependency changes in the diff.
git diff -U0 | grep -E '^\+\s*(import|from)\s'
git diff -- requirements.txt pyproject.toml package.jsonAny package you do not already use needs a deliberate decision. A new line in a dependency file is a change to your supply chain, so review it as carefully as code.
Step 2: Check that the package is real and trusted
Before installing, look the package up on its official registry. Check the following:
- The exact name matches the project you intend. Look for typos and near-duplicates.
- It has a release history, not a single recent upload.
- It links to a real source repository with recent activity.
- The maintainers and download counts fit a package people actually use.
For Python, pip index versions <name> shows whether a package is published. It does not show whether the package is trustworthy, so still check the project page.
Step 3: Check the API against your installed version
Do not rely on the AI's memory. Ask the installed library directly. The script below checks that a name exists and prints its signature.
import importlib
import inspect
def check_api(module_name: str, attr: str) -> None:
module = importlib.import_module(module_name)
target = getattr(module, attr, None)
if target is None:
print(f"MISSING: {module_name}.{attr}")
return
try:
print(f"OK: {module_name}.{attr}{inspect.signature(target)}")
except (TypeError, ValueError):
print(f"OK: {module_name}.{attr} (no signature available)")
check_api("json", "loads")
check_api("json", "load_string")The first call prints the signature of json.loads. The second prints MISSING: json.load_string, because that function was never part of the standard library.
Also check the following:
- The official documentation for your installed version, not just the latest one.
- The package's changelog for deprecations and removals.
- Whether a type checker flags the call. Tools such as mypy or pyright report unknown attributes, which catches many invented functions cheaply.
Step 4: Confirm behavior, not just existence
A real function can still be used wrongly. Check argument order, default values, return types, and error behavior. A short test that calls the function with known input and asserts the result is stronger evidence than reading the documentation alone.
Exercise
An AI-written diff adds import fastjsonx and calls fastjsonx.parse_strict(data). The script runs after you install the package with pip. List the checks you still need before accepting the change.
Show answer
Installing without an error only shows that some package with that name is published. Check the registry page for a release history, a real source repository, and trusted maintainers, and confirm the name is not a typo-squat or slopsquat of a known package. Then confirm that parse_strict exists in the installed version, for example with getattr, inspect.signature, or the documentation. Check its arguments and return type, and add a test that calls it with known input. If you decide to keep the package, pin it in the dependency file deliberately.