Sub topic 7
When generated code brings license obligations
This chapter assumes you know what permissive and copyleft licenses are, you can read a diff in code review, and you have used an AI coding assistant. It is general education, not legal advice. The law differs by country and is still changing.
How obligations reach generated code
An assistant does not copy files on purpose. It predicts text. Most of the time the output is new. Sometimes it matches existing code closely, especially for well-known algorithms, boilerplate, or snippets that appear many times in public repositories. When that happens, the license of the original code may come with it.
A license attaches to the original work. If your generated code is a copy or a close adaptation of a licensed work, using it can trigger that license's conditions. This holds whether you typed the code, pasted it from a website, or received it from a tool.
What each license family asks for
| License family | Typical examples | What it can require if code is reused |
|---|---|---|
| Permissive | MIT, BSD, Apache-2.0 | Keep the copyright and license notice. Apache-2.0 also requires a notice of changes and carries a patent grant. |
| Weak copyleft | LGPL, MPL-2.0 | Share changes to the covered files or library under the same license. |
| Strong copyleft | GPL, AGPL | Distribute derived work under the same license. AGPL extends this to network use. |
| No license | Public repo with no license file | No permission is granted by default. Reuse is risky. |
The most common failure with permissive code is dropped attribution. The most serious is copyleft code ending up in a closed-source product.
How contamination happens
Three conditions usually need to hold together:
- The output reproduces a protected part of an existing work. Short, obvious, or purely functional fragments often are not protected expression.
- The source has a license with conditions you have not met.
- You distribute or deploy the result in a way the license covers.
Condition 1 is the hard one. Courts have not settled how much similarity matters, so treat any long verbatim match as a real risk.
Signals to look for in review
- Code with an unusual style or naming that does not fit your codebase.
- Comments naming an author, a project, or a license.
- A long block that looks like a known library function.
- Identifiers or strings that point to another project.
Many assistants offer a setting that blocks or flags suggestions matching public code. Turn it on where it exists, and check what it actually covers in the tool's documentation.
Practical steps
- Search a distinctive line or two of the output in a code search engine.
- Run a license scanner over the repository. These tools match snippets against known projects, so they catch more than a dependency list does.
- If you find a match, either comply with the license or rewrite the part yourself without looking at the original.
- Note the check in the pull request, as the provenance chapter describes.
Here is a header for a file that you have confirmed and attribute correctly:
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2026 Example Corp.
def clamp(value: float, low: float, high: float) -> float:
return max(low, min(value, high))The header states the license your project uses for this file. It does not remove obligations from any code that was copied in. If part of the file came from an MIT project, you must still keep that project's notice.
Common errors
- Assuming that a tool's output is license-free because the tool generated it.
- Removing notices from matched code to make it look original.
- Relying on a license scanner that only checks dependencies, not snippets.
- Treating a tool's indemnity offer as a replacement for review. Indemnity is covered in the next chapter.
Exercise
An assistant generates a 40-line function. A code search shows it is nearly identical to a function in a GPL-3.0 library. Your product is closed-source and shipped to customers. What are your options?
Show answer
Do not merge it as is. You can comply with the GPL, which would mean releasing the derived work under GPL terms, or rewrite the function independently without copying the original. Rewriting is the usual choice for a closed-source product. Record the check in the pull request.
Exercise
Why does adding an SPDX header to a generated file not settle the license question?
Show answer
The header only states the license your project applies. It cannot remove conditions that come from existing code the file reproduces, such as attribution notices or copyleft terms.