Sub topic 4
Set constraints: libraries, style, scope, and limits
This chapter assumes you can write and run Python, you have used pytest or a similar test runner, and you have already written a prompt with a clear goal and context. Here you add constraints: explicit limits on what the assistant may produce.
Why constraints matter
A goal says what the code should do. A constraint says what the code must not do, or what it must stay inside. Without constraints, the assistant fills each gap with a common default. It might import a third-party package you don't have, rewrite a whole file when you wanted one function, or pick a style that clashes with your project.
The assistant cannot see your project rules unless you state them. Each constraint removes one guess.
Four kinds of constraint
| Kind | What it controls | Example |
|---|---|---|
| Libraries | Which packages and language versions are allowed | "Use only the standard library." |
| Style | Naming, typing, comments, formatting | "Add type hints; no comments; follow PEP 8." |
| Scope | Which code may change | "Change only `parse_date`; do not touch other functions." |
| Limits | Performance, size, and behavior bounds | "Run in O(n) time; no more than 20 lines." |
Libraries
Name what is allowed, not only what is forbidden. "Use only the standard library" is clearer than "don't use extra packages". If you depend on a specific package, give its version, because APIs change between versions.
Style
State the rules your reviewers would enforce. If your project has a linter or formatter, say so: "Must pass ruff with default settings." Mention the Python version too, since features such as match statements need 3.10 or later.
Scope
Scope constraints protect code that already works. When you ask for a bug fix, say what must stay unchanged. Asking for "only a unified diff" also makes it easy to see whether the assistant stayed in bounds.
Limits
Limits cover performance, size, and edge-case behavior. State them in measurable terms. "Fast" cannot be tested, but "handles a 1 million character string in under one second" can.
A worked example
Here the constraints from the spine's slugify prompt are marked out, so you can see each one at work.
Write a Python 3.10 function slugify(text: str) -> str that converts an article title into a URL slug.
Context: it will be used in a small blog tool; titles are plain ASCII.
Constraints: use only the standard library; lowercase the result; replace each run of non-alphanumeric characters with a single hyphen; no leading or trailing hyphens.
Return only the function, with no explanation.- Library constraint: "use only the standard library".
- Behavior constraints: lowercase, one hyphen per run, no edge hyphens.
- Scope constraint: "Return only the function".
A plausible response satisfies all of them:
import re
def slugify(text: str) -> str:
text = text.strip().lower()
text = re.sub(r"[^a-z0-9]+", "-", text)
return text.strip("-")The code uses only re, which ships with Python. Because the constraints were explicit, you can check each one by reading the code.
How constraints interact
Constraints can conflict. "Use only the standard library" and "handle Unicode transliteration of any language" pull against each other, because the standard library has limited support for transliteration. If two constraints conflict, the assistant will usually satisfy one and silently drop the other. Review your list for conflicts before sending.
Too many constraints cause a different problem. A long list of minor rules makes the important ones easier to miss. Put the rules that matter most first, and drop ones you would not enforce in code review.
Common mistakes
- Vague limits. "Make it efficient" gives the assistant nothing to aim at. Give a complexity or a timing.
- Only negative rules. "Don't use regex" leaves the replacement open. Say what to use instead, or accept that the assistant picks.
- Unstated versions. Without a Python version, the assistant may use syntax your interpreter rejects.
- Unchecked constraints. The assistant may claim to follow a rule and break it. Verify with tools, such as a linter for style or an import check for libraries.
Checking that constraints were met
Some constraints can be tested mechanically. This script reports any imports outside the standard library, which verifies the "standard library only" rule for a file. It needs Python 3.10 or later.
import ast
import sys
def imported_modules(path: str) -> set[str]:
with open(path, encoding="utf-8") as f:
tree = ast.parse(f.read())
names = set()
for node in ast.walk(tree):
if isinstance(node, ast.Import):
names.update(alias.name.split(".")[0] for alias in node.names)
elif isinstance(node, ast.ImportFrom) and node.module and node.level == 0:
names.add(node.module.split(".")[0])
return names
if __name__ == "__main__":
outside = imported_modules(sys.argv[1]) - sys.stdlib_module_names
print("Non-stdlib imports:", sorted(outside) or "none")Run it as python check_imports.py slugify_module.py. For the slugify function above it prints Non-stdlib imports: none.
Exercise
You ask an assistant to fix a bug in one function, and it returns a whole file with renamed variables and reordered functions. Which kind of constraint was missing, and how would you word it?
Show answer
A scope constraint was missing. For example: "Change only the function parse_date. Do not rename, reorder, or edit anything else. Return only a unified diff."
Exercise
Rewrite this prompt so its constraints can be checked: "Write a fast function to dedupe a list, keep it clean, and don't use anything weird."
Show answer
One answer: "Write a Python 3.10 function dedupe(items: list[str]) -> list[str] that removes duplicates and keeps the first occurrence order. Use only the standard library. Add type hints and pass ruff with default settings. It must run in O(n) time. Return only the function." Each rule can be tested by a linter, an import check, or a timing test.