Sub topic 10
Give the model version and documentation context
This chapter assumes you can run Python and pip, read library documentation, and run unit tests. It also assumes you have read the earlier chapters on version drift and existence checks. Here you will reduce errors before they happen by telling the model what you are running.
Why context helps
A model writes code from patterns in its training data. That data mixes many library versions, and it ends at a cutoff date. If your prompt names no version, the model picks whichever usage it saw most often. That is often an older API, such as DataFrame.append in pandas.
Version context narrows the choice. It does not remove the risk, because the model can still invent a function or ignore your constraint. Treat prompt context as a way to lower the error rate, and keep verifying the output with the checks from earlier chapters.
What to include
Useful context is short and factual:
- The language version and the exact versions of the libraries involved.
- The task and the specific API area, such as "HTTP requests with a timeout".
- A short excerpt from the documentation for your version, when the API is new or unusual.
- A constraint such as "use only functions that appear in the excerpt, and say so if you are unsure."
Avoid pasting your whole dependency list. Extra versions add noise, and the model may mix them up. Name only the packages the task touches.
Generate the version block instead of typing it
Typing versions by hand invites mistakes. This script reads them from the running environment, so the prompt matches what is installed.
import sys
from importlib.metadata import PackageNotFoundError, version
def version_context(packages):
py = sys.version_info
lines = [f"Python {py.major}.{py.minor}.{py.micro}"]
for name in packages:
try:
lines.append(f"{name}=={version(name)}")
except PackageNotFoundError:
lines.append(f"{name}: not installed")
return "\n".join(lines)
print(version_context(["requests", "pandas"]))Run it in the same virtual environment your project uses. A different interpreter can report different versions.
A prompt that carries context
Paste the block into the request, then add the task and the rules.
Environment:
Python 3.12.4
requests==2.32.3
pandas==2.2.2
Task: fetch JSON from an HTTP endpoint and load the "items" list into a DataFrame.
Rules:
- Use only APIs that exist in the versions above.
- Do not use anything removed in pandas 2.0 or deprecated in Python 3.12.
- Set a timeout on every network call.
- If you are not sure a function exists, say so instead of guessing.The last rule gives the model permission to admit uncertainty. That makes a doubtful call visible to you, where otherwise it would sit in the code looking confident.
Add documentation excerpts
For a library the model may not know well, paste the relevant signature or docs section. You can print a signature straight from the installed package:
import inspect
import requests
print(inspect.signature(requests.get))
print(inspect.getdoc(requests.get))Pasting this output ties the model to the real signature in your environment. It works well for small, specific APIs. For a large library, paste only the parts the task needs.
Common mistakes
- Stale context. Versions copied from an old chat or an old lockfile no longer match the environment. Regenerate the block for each session.
- Wrong environment. Reading versions from your system Python while the project runs in a virtual environment gives the model false facts.
- Trusting the excerpt blindly. An excerpt from the "latest" docs page may describe a newer release than yours. Check the version selector on the docs site.
- Skipping verification. Context improves the odds only. Run the existence checks and tests anyway.
Exercise
Exercise
A teammate pastes pandas==1.5.3 into a prompt from an old chat, but the project now runs pandas 2.2.2. What can go wrong, and how do you prevent it?
Show answer
The model may rely on APIs that were removed after 1.5.3 or miss newer ones, such as using DataFrame.append, which pandas 2.0 removed. The generated code can then fail at runtime or give wrong results. Prevent it by generating the version block from the active environment with importlib.metadata.version, regenerating it each session, and still running existence checks and tests on the output.