Detecting AI-generated Python code

Python is the first language in most programming courses, and the language chat assistants write most fluently. That combination makes introductory Python assignments the place where undisclosed AI use is most common and, with the right reading, often the easiest to recognise. This guide covers the Python-specific habits of ChatGPT, Claude and Copilot, which ones matter, and how to review a flagged submission without being unfair to a capable beginner.

Checks every file against classmates, the web and AI, in 65+ languages.

Why Python is a special case

Three things make AI use in Python assignments both tempting and visible.

  • Models write excellent Python. Python is heavily represented in the data models learn from, so generated Python is idiomatic, complete and runs first time. A typical introductory exercise is a single prompt away from a full answer.
  • Introductory Python is short. A 30-line program leaves little room for an individual style, which makes peer similarity scores noisy. It also means a single sophisticated habit stands out.
  • Courses introduce features in a known order. You know when the class met functions, exceptions, comprehensions and classes. Code that uses features ahead of that schedule, fluently and consistently, is informative.

Python habits of AI assistants

These are the patterns reviewers see most often in generated Python. None is proof. Their value comes from appearing together and from not matching what the course has taught.

HabitWhat it looks likeWeight in an intro course
Type hints everywheredef f(items: List[int]) -> Optional[str]:, often with from typing importModerate, if not taught
A docstring on every functionOne-line or Google-style docstrings with Args and Returns sectionsWeak to moderate
A guarded entry point in a tiny scriptdef main() plus if __name__ == "__main__": for ten lines of logicModerate, if not taught
Validation nobody asked fortry/except ValueError around input, empty-list checks, custom error messagesModerate
Advanced formattingf-strings with specifiers like :.2f or :>10Weak
Library choices beyond the coursepathlib, dataclasses, collections.Counter, argparse for a simple taskModerate to strong
Solving a neighbouring problemCode that handles the general case well but ignores a course-specific rule in the specificationStrong
Conversational leftoversComments such as # Example usage: or # You can modify this to...Strong

Claude and ChatGPT produce whole programs in this style. Copilot and Cursor's inline completion leave a patchier trace: a student's own loop with a suspiciously complete helper function beside it. See detecting Copilot and Cursor code.

An illustration

Same student, same kind of task Average of grades typed by the user
Illustration
Week 3 lablab3.py
1nums = input("Enter grades: ").split()2total = 03for n in nums:4 total = total + int(n)5print("Average is", total / len(nums))
Week 6 submissiongrades.py
1from typing import List23def calculate_average(grades: List[float]) -> float:4 """Return the average of a list of grades."""5 if not grades:6 raise ValueError("Grades list cannot be empty")7 return sum(grades) / len(grades)89def main() -> None:10 try:11 raw = input("Enter grades separated by spaces: ")12 grades = [float(g) for g in raw.split()]13 print(f"Average: {calculate_average(grades):.2f}")14 except ValueError as e:15 print(f"Error: {e}")1617if __name__ == "__main__":18 main()
Type hints taught: noExceptions taught: week 8Shared mistakes with classmates: none

The second file is better Python, and nothing in it is wrong. What stands out is the distance from the first: typing imports, a docstring, a guarded main(), exception handling and a format specifier, none of which the course had reached. That distance is a reason to ask the student to talk through the code, not a finding.

What is not a sign of AI

  • Consistent formatting. Black, Ruff and editor auto-format give every student identical layout.
  • Pythonic idioms on their own. enumerate, list comprehensions and with open(...) appear in every tutorial. A student who read ahead will use them.
  • Good variable names. Many courses grade on them.
  • Matching a classmate. That is a copying question, answered by a peer comparison, not an AI question.
  • Prior experience. Some students arrive having written Python for years. Their week-one code will already look professional, which is why comparing a student with their own earlier work matters more than comparing them with the class.

How AI detection for Python works

A person can hold a handful of signals in mind; a detector weighs hundreds consistently across a whole class. Codequiry's AI detection is trained on source code, including large amounts of Python, and judges how each file is written: structure, naming, comment patterns, error handling and many features that are hard to name. It returns a likelihood per file, lists the reasons, and marks the passages that drove the result.

It runs in the same check as the plagiarism search, so for every Python file you see three separate answers: does it match a classmate, does it match public code on the web, GitHub or Stack Overflow, and does it read as generated. Upload the class as a ZIP, or .py files and notebooks exported to Python. More on the plagiarism side in the Python plagiarism checker guide, and on how scores are produced in how AI detectors work.

Check a Python assignment for AI

An AI likelihood for every file with its reasons, plus classmate and web matching in the same run.

Reviewing a flagged Python file

  1. Check the policy. Was AI allowed for explanation or debugging? Was disclosure required, and is there one?
  2. Read the reasons and flagged lines. Do they point at the core logic, or at boilerplate every student writes?
  3. Lay the file beside the student's earlier labs. Feature use, naming habits, the kind of mistakes they make.
  4. Run it on an edge case from your specification. Generated code often handles general cases well and misses the course-specific rule.
  5. Ask for a walkthrough and a small change. "Why the try block here? Make it accept commas as separators." An author does this quickly.
  6. Decide on the whole picture and record what you relied on. The general method is in how to tell if code is AI generated.

Frequently asked questions

How can you tell if Python code was written by ChatGPT?

Look for habits that do not fit the course: type hints and typing imports before they were taught, a docstring on every function, a main() function behind an if __name__ == "__main__" guard in a short script, broad input validation, and f-strings with format specifiers. Each is weak alone. Together, and against the student's earlier work, they are worth a conversation.

Are type hints a sign of AI-generated Python?

In an introductory course that has not taught them, consistent type hints across a submission are a moderate signal, because chat assistants add them by default. In a course that requires them, or for an experienced programmer, they mean nothing.

Can an AI detector check Python code?

Yes, if it is trained on code. Codequiry's AI detection reads Python files and reports a likelihood for each, with the reasons behind it, in the same check that compares students with each other and searches the web, GitHub and Stack Overflow.

Does Copilot-written Python look different from ChatGPT-written Python?

Usually. ChatGPT and Claude tend to produce complete, heavily commented programs in one voice. Copilot completes lines and blocks inside the student's own structure, so the generated parts are mixed with the student's style and the overall signal is weaker.

Is it unfair to flag beginners who write clean Python?

It would be, if a flag were treated as a finding. Some beginners write tidy, idiomatic code, and formatters such as Black make everyone's layout identical. A flag should only start a review that includes the student's explanation of the code.

What Python assignments are hardest to complete with AI?

Ones tied to course-specific context: a dataset handed out in class, a library or helper module written for the course, constraints that change each term, and tasks that build on the student's own earlier code.

Check Python for AI-written code

A likelihood and reasons for every .py file, alongside classmate, web, GitHub and Stack Overflow matching.

Start a check Pay as you go with credits. Comparing files is free.