Sub topic 7
When Generated Code Brings License Obligations
This chapter covers how an AI assistant's output can carry open-source license duties into your codebase, and how to spot and handle that. It is general education, not legal advice. The details depend on the jurisdiction and the facts.
What this chapter assumes
You should already know the following:
- How an AI coding assistant produces suggestions.
- The basic difference between permissive and copyleft licenses, covered earlier in this tutorial.
- That models can sometimes reproduce text from their training data.
- How to read a Git diff and take part in code review.
The core idea
A license is a permission that comes with conditions. If you copy protected code from a project, you accept those conditions, whether or not you meant to. An AI assistant can be the route by which the copied code reaches you.
Most generated code is not copied from anywhere. The model produces new text that fits your prompt and context. In some cases the output matches existing code closely. Then the same rules apply as if you had pasted it from a repository yourself.
The risk is therefore about the output, not the tool. Ask two questions about any piece of generated code:
- Does it reproduce protected expression from an identifiable source?
- If it does, what are the conditions of that source's license?
How an obligation enters your repository
Three situations produce obligations. They differ in how likely they are and in how easy they are to notice.
- Verbatim reproduction. The model emits a function, file, or comment block that matches an existing project. This is most likely with distinctive code that appears many times in public repositories, or with code that is large and unusual. Headers, license text, and author comments sometimes come along. That makes it easy to notice.
- Close paraphrase. Names and formatting change, but the structure, ordering, and logic follow one source. A copyright claim could still reach this. It is hard to notice because no text matches.
- Convergent solutions. Two implementations look alike because the problem allows only a few sensible solutions. Short, functional, or standard code is often not protected expression at all. Examples include a clamp function, a standard algorithm, or a required API call sequence. In these cases there may be nothing to obey.
Size and originality matter. A one-line idiom raises few concerns. A 200-line module with distinctive structure and comments raises many.
What the licenses ask of you
Once you have copied protected code, the license sets your duties. The table shows the usual obligations of common licenses. Read the actual license text for any real case.
| License family | Typical examples | Main conditions when you reuse the code |
|---|---|---|
| Permissive, minimal | MIT, BSD-2-Clause, BSD-3-Clause | Keep the copyright notice and license text with copies or substantial portions. |
| Permissive, with patent terms | Apache-2.0 | Keep the license and any NOTICE content, state significant changes, and accept the patent grant terms. |
| File-level copyleft | MPL-2.0 | Modified files stay under MPL and their source must be available. Other files in your project can keep their own license. |
| Library copyleft | LGPL-2.1, LGPL-3.0 | Changes to the library stay under the LGPL. Linking has extra conditions, such as letting users relink. |
| Strong copyleft | GPL-2.0, GPL-3.0 | If you distribute a work that incorporates the code, the combined work is generally offered under the GPL with source. |
| Network copyleft | AGPL-3.0 | Like the GPL, and the duty to offer source can also apply when users interact with the software over a network. |
Two points about the table need care.
First, even permissive licenses have a condition: attribution. If generated code reproduces MIT code and you ship it without the notice, you are out of compliance. You do not need to open-source anything, but you have broken the license terms.
Second, copyleft duties usually start at distribution, not at private use. Code that runs only inside your company is treated differently from code you ship to customers or publish. The AGPL is the main exception, because it covers interaction over a network. Whether your product counts as a combined or derivative work depends on how the code is linked, and that is a legal question.
Why copyleft is the hard case
A single reproduced block under the GPL can raise a question about the whole program. Suppose your proprietary service includes a GPL-licensed routine that the assistant reproduced. If you distribute the program, the GPL might require you to license the combined work under the GPL, or you would have to remove the routine. Which outcome applies depends on how closely the code is integrated and on the license version.
Cleanup often costs more than prevention. You may have to find where the code spread, rewrite it, and recheck every release that contained it.
Other sources of obligations
Code licenses are not the only conditions to think about.
- Documentation and Q&A text. Code posted on Stack Overflow is licensed under Creative Commons Attribution-ShareAlike. A model that reproduces such text may bring attribution and share-alike duties with it.
- Dependencies. An assistant may suggest a library with a license your project cannot accept, such as a GPL library in a closed-source product. This is not copying, but it is an obligation the suggestion creates.
- Vendored files. Generated code that includes data files, fonts, or schemas may carry their own terms.
- Existing headers. Code reproduced from a project can include an old license header that conflicts with the license you use. Leaving it in place or deleting it are both mistakes if you have not checked.
Common mistakes
- Treating tool output as automatically yours to license. Ownership of the output and the conditions on any copied part are separate questions. A tool's terms may say you own the output. They do not remove third-party conditions on reproduced code.
- Deleting headers to clean things up. If the code is protected and you keep it, removing notices makes the problem worse.
- Relying on a "no match" result as proof. Matching tools only compare against what they know. A clean result lowers risk and does not remove it.
- Ignoring dependency suggestions. The license of a suggested package matters as much as the license of any pasted text.
- Waiting until release. Old commits are hard to audit. Checking at review time is cheaper.
A review workflow for suspicious output
You cannot trace every line, so concentrate on code that stands out. Triggers include large blocks, unusual structure, comments that sound like another project, and license text or author names in the output.
To check for a match, take a distinctive line or comment from the block and search for it in a code search engine. You can also run your organization's license scanner. If the assistant has a setting that blocks or flags suggestions matching public code, use it. Such filters vary between tools, so check what yours does and does not cover.
A simple automated check
Automation catches the visible cases: reproduced headers and license text. The script below scans the files you pass it for common license markers and fails if it finds one that does not belong to your own project. Run it in CI on changed files.
import re
import sys
from pathlib import Path
PROJECT_LICENSE = "Apache-2.0"
OWN_HOLDERS = ("Example Corp.",)
MARKERS = [
re.compile(r"SPDX-License-Identifier:\s*(\S+)", re.I),
re.compile(r"Copyright\s*(\(c\)|©)\s*\d{4}", re.I),
re.compile(r"GNU (Affero |Lesser )?General Public License", re.I),
re.compile(r"Permission is hereby granted", re.I),
re.compile(r"Licensed under the", re.I),
]
def is_foreign(line: str) -> bool:
spdx = MARKERS[0].search(line)
if spdx:
return spdx.group(1) != PROJECT_LICENSE
if any(holder in line for holder in OWN_HOLDERS):
return False
return any(pattern.search(line) for pattern in MARKERS[1:])
def scan(path: Path):
text = path.read_text(encoding="utf-8", errors="ignore")
for number, line in enumerate(text.splitlines(), start=1):
if is_foreign(line):
yield number, line.strip()
def main(paths: list[str]) -> int:
found = False
for name in paths:
path = Path(name)
if not path.is_file():
continue
for number, line in scan(path):
print(f"{path}:{number}: {line}")
found = True
return 1 if found else 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))Run it with python check_license_markers.py $(git diff --name-only origin/main). It reports every line that looks like a foreign license notice, so a person can look at the context.
What to do when you find a match
- Stop the spread. Do not merge. If the code is already merged, find every branch and release that contains it.
- Identify the source and license. Record the repository, file, version, and license text.
- Choose a fix. You can remove the code, rewrite it without looking at the source, or comply with the license. Compliance means keeping notices, adding attribution, and meeting any copyleft terms. Compliance is only realistic when the license is compatible with how you distribute.
- Document the decision. Put a note in the pull request or provenance record so the reasoning survives.
- Escalate when needed. Involve legal counsel if the code shipped to customers, if the license is copyleft, or if you received a complaint.
For a rewrite to count as independent, the person writing it should work from the requirements, not from the matched code. A rewrite that follows the original line by line is still a copy.
Limits to keep in mind
- No tool or filter guarantees that generated code is free of third-party material.
- Courts have not settled all of the questions about training data, memorization, and output similarity. Outcomes depend on jurisdiction and facts.
- Tool vendors sometimes offer indemnity. That shifts some financial risk. It does not remove your duties or the technical work of cleaning up code, and its conditions often require you to have used the tool's filters. The tool-terms chapter covers this.
Exercise
An assistant generates a 150-line parser for a niche file format. The code includes a comment block that begins "Copyright (c) 2014 Jordan Lee" and a line saying "Permission is hereby granted, free of charge...". Your project is proprietary and you distribute it to customers. What should you do before merging?
Show answer
The comment block and permission text are the MIT license notice, so the code very likely reproduces an existing project. Do not delete the notice and merge. Find the source and confirm the license is MIT. MIT is compatible with a proprietary product, but it requires you to keep the copyright notice and license text with the code. So either keep the code with its notice and record the attribution, or rewrite the parser independently from the format specification. Document the decision in the pull request.
Exercise
A teammate says: "The vendor's terms say we own all output, so any license text in generated code can be removed." Give two reasons this is wrong.
Show answer
First, an output ownership clause covers the relationship between you and the vendor. It does not change third-party rights in code that the output reproduces. Second, removing notices from protected code that you keep and distribute breaks conditions such as the MIT and Apache-2.0 requirements to keep notices, and it hides the origin of the code from later review. If the code is copyleft, deleting the header does not remove the obligations either.
Exercise
Why is a GPL-licensed block in a proprietary product usually a bigger problem than an MIT-licensed block of the same size?
Show answer
The MIT license asks only that you keep the notice and license text. The GPL can require that a distributed work incorporating the code be offered under the GPL with source. That could conflict with a proprietary license, so you would have to remove or replace the code, or change how you license the product.