Sub topic 2
Why generated code drifts from the library you have installed
This chapter assumes you can read Python, install packages with pip, and run a test suite. It explains where version drift comes from, so the checks in later chapters make sense.
A model learns a snapshot
A language model learns from text collected up to a training cutoff. That text includes documentation, tutorials, forum answers, and public repositories. The model then answers questions for months or years after the cutoff. Meanwhile the libraries keep releasing new versions.
Your project runs a specific version of each library. The model has no direct view of it. It writes code that looks like what it saw most often, and that is not always what your installed version accepts.
Why old patterns win
Drift comes from more than the calendar. Three effects make it worse.
- Volume. A function that existed for ten years appears in many more examples than the replacement released last spring. The model reproduces the common pattern.
- Stale copies. Blog posts and answers are rarely updated. Code that was correct in 2018 still looks correct on the page.
- Blended versions. The model does not store one library version at a time. It can mix a method from one release with an argument style from another. The result can match no real version.
Two common drift cases
Removal is the clearest case. The DataFrame.append method was deprecated in pandas 1.4 and removed in pandas 2.0. Older code calls it in a loop, and assistants still produce it.
import pandas as pd
rows = [{"id": 1}, {"id": 2}]
# Outdated: DataFrame.append was removed in pandas 2.0.
# df = pd.DataFrame()
# for row in rows:
# df = df.append(row, ignore_index=True)
# Current: build the frame in one step, or use pd.concat.
df = pd.DataFrame(rows)
print(df)Deprecation is the quieter case. The old call still runs, so nothing fails until a later release removes it.
from datetime import datetime, timezone
# Deprecated since Python 3.12: naive UTC timestamp.
# now = datetime.utcnow()
# Preferred: timezone-aware timestamp.
now = datetime.now(timezone.utc)
print(now.tzinfo)utcnow() also has a behavior problem. It returns a naive datetime, which carries no timezone, so the old code can be subtly wrong as well as old.
Drift is not the same as invention
Keep these two failures apart, because the checks differ.
| Failure | The symbol | How it appears |
|---|---|---|
| Invented API | Never existed in any version | `AttributeError` on first call |
| Outdated usage | Existed in some version | Works on one version, fails or warns on another |
| Wrong signature | Exists, but arguments never matched | `TypeError`, or silently ignored options |
An outdated call is harder to catch than an invented one. A reviewer who sees df.append(...) may recognize it as real, because it was real. It passes the "looks familiar" test, and that is the test you cannot trust.
Why it fails late
Python resolves attribute names when the line runs, not when the file loads. A call to a removed method sits in the file until some code path reaches it. A rarely used branch, such as error handling or an export step, can hide the problem for weeks.
Deprecated calls are worse in one respect. Python hides DeprecationWarning by default outside the code that triggers it directly, so the signal may never reach you. Later chapters show how to surface it.
What this means for your workflow
You cannot fix drift by asking the model to "be careful". The model has no reliable way to know which version you run. The checks have to compare the generated code with your actual environment. The rest of this tutorial builds that routine, starting with finding the exact versions you are running.
Exercise
A teammate pastes assistant-generated code that calls df.append(row, ignore_index=True) inside a loop. It works on their laptop but raises AttributeError in CI. Name the most likely cause, and say whether this is an invented API, outdated usage, or a wrong signature.
Show answer
The laptop has pandas older than 2.0, where DataFrame.append still exists, and CI has pandas 2.0 or later, where it was removed. This is outdated usage. The method was real in earlier versions, so it is not invented, and the arguments matched the old signature, so it is not a wrong signature. The fix is to build the frame from the list of rows with pd.DataFrame(rows) or use pd.concat.