courtpy
| Version | |
| Status | |
| Documentation | |
| Tools | |
| Compatibility | |
| Stats | |
What is courtpy?
courtpy collects court opinions, turns them into data, and analyzes them. It
is designed for empirical legal research, where the data has to be explained
and the results reproduced. You don't need to know how to code to use courtpy.
Everything is described in a simple settings files and parsing instructions are
located in CSV files (of which many are included out of the box). But if you do
code, courtpy is highly extensible to allow easy data munging, creation of
new fields, and wrapping custom tools.
- Collect opinions from CourtListener, from its free bulk data or its API, or use opinions you downloaded from Lexis-Nexis.
- Parse them into a table, one row for each case, with rules written in spreadsheets (CSV files) that anyone can read and change: the parties and their roles, the court, the judges, the disposition, the issues discussed, the citations, and more.
- Study the judges on each case, with integration of the Federal Judicial Center's biographical data and Judicial Common Space Scores (from fjc-biographies and judicial-common-space). Data about the overall political and judicial context can also be easily added, including Martin-Quinn scores, NOMINATE scores of Congress, the Supreme Court Database, and the Federal Judicial Center's Integrated Database of appeals.
- Analyze the data with amos (which is built into
courtpy), which compares models, preprocessing, and statistical methods, and records everything needed to reproduce the results.
Why use courtpy?
- Transparent. What
courtpyfinds in an opinion comes from rules in CSV files, not from code, so a study's methods can be read, reported, and changed by anyone who can use a spreadsheet. The rules are part of the study, so others can check them. - Reproducible. A whole study (which cases, how they were parsed and coded,
and how they were analyzed) fits in one settings file, and
amosrecords every step and the version of every package.
Getting started
Requirements
courtpy requires Python 3.11 or later. It runs on Linux, macOS, and Windows.
Extracting cases from CourtListener's bulk data needs a good connection and room for the bulk files (about 63 GB for the opinions, clusters, and dockets), unless they are streamed. Downloading with the API needs a free CourtListener account.
Installation
To install courtpy, use pip:
pip install courtpy
To install the latest development version from GitHub instead:
pip install git+https://github.com/WithPrecedent/courtpy
To work on courtpy itself, see the
contribution guide.
Usage
Collecting opinions from CourtListener
CourtListener offers its opinions in two ways, and courtpy uses both. Both save
each case as a JSON file in a folder named for its court (such as
court_listener/ca1/4567890.json), and the parser reads them the same way.
The bulk data is the whole CourtListener database, published as compressed CSV files every three months. It needs no API key and has no limits, so it is the best way to collect many cases. The files are large (the opinions file is more than 50 GB), so extracting cases takes a few hours. The files are kept outside your project so that they can be used again:
import courtpy
courtpy.BulkData().extract(
courts = "federal_circuits",
start_date = "2019-01-01",
end_date = "2019-12-31",
folder = "court_listener")
The API is best for small or very recent sets of cases. It needs an API
key: sign in to CourtListener and open the "API" page of your profile. A free
account can make 5 requests a minute, 50 an hour, and 125 a day (as of 2026).
courtpy asks for 20 cases at a time, waits when CourtListener asks it to, and
saves its place, so a download stopped by the daily limit resumes where it
stopped when it is run again:
courtpy.CourtListener().download("ca1", "2026-09-01", "2026-09-30", "court_listener")
Courts are named by their CourtListener ids (such as ca1, cadc, cafc,
and scotus) or by groups: federal_appellate, federal_circuits, and
supreme_court.
Store your API key once with courtpy key set. See
Keeping your API key secret
for where it is kept and how courtpy finds it.
Using opinions from Lexis-Nexis
Lexis-Nexis downloads many cases into one text file. Divide such files into one file for each case, and then parse them as the source "lexis_nexis":
courtpy.lexis.split("downloads", folder = "lexis_nexis")
table = courtpy.parse("lexis_nexis", source = "lexis_nexis")
Parsing and coding
courtpy.parse reads a folder of saved cases and returns a
pandas.DataFrame, one row for each case, labeled by its id:
table = courtpy.parse("court_listener")
dataset = courtpy.code(table) # parties, case types, and outcomes
courtpy.save_cases(dataset.data, "cases.csv")
The rules are in the CSV files in courtpy's
instructions folder,
with a guide to writing your own. The "federal" rules make more than 250
variables, including:
| Variables | Meaning |
|---|---|
party1, party2 |
The parties on each side of "v." |
party1_appellant, party2_respondent, party1_united_states, ... |
Each party's role, and whether it is the United States. |
court_num |
The circuit (1 to 11, 12 for the D.C. Circuit, 13 for the Federal Circuit, 99 for the Supreme Court). |
panel_judges, panel_size, author, concurring, dissenting |
The judges. |
disposition_reverse, decision_reverse, opinion_reverse, ... |
The disposition: from the header, from the decision that the opinion states ("AFFIRMED."), and from the opinion's words anywhere. |
agency |
The federal agency involved, if any. |
agencies |
Every federal agency that the case names, separated by commas (such as "Internal Revenue Service, United States Tax Court"). |
civil_..., constitutional_..., criminal_..., procedure_..., standard_... |
Issues discussed in the opinion (such as criminal_firearm, civil_titlevii, and standard_de_novo). |
citations_case, citations_statute, ... |
Citations in the opinion. |
published, year, date_filed, word_count |
Other information. |
The coders (which are amos techniques) add type_criminal, each party's
role in the appeal and in the case (such as party2_criminal_defendant), and
the outcomes: outcome_reversal, outcome_party1_won,
outcome_criminal_defendant_won, and others.
Adding the judges and the politics of the day
The rules find the judges' names as an opinion writes them ("LYNCH"). To
study the judges, make a roster once from the Federal Judicial Center's
biographical directory
(three small files, which fjc-biographies
downloads and parses), and more coders can use it. jcs = True adds each
judge's Judicial Common Space score, computed by
judicial-common-space:
courtpy.judges.Roster.from_fjc(jcs = True).save()
dataset = courtpy.code(table, [
"code_parties", "code_case_type", "code_outcome",
"code_panels", "add_politics", "reshape_by_votes"])
Cases, judges, years, and other researchers' data are tables whose rows are different things, so these coders are the mergers and shapers of amos: a merger adds the columns of another table to the rows that it matches, without ever adding or losing a row, and records how many rows it matched.
| Coder | Adds |
|---|---|
merge_biographies |
What the roster says about the judge that each row names, such as the author of each opinion (author_party, author_woman, author_age, ...). |
code_panels |
The judges of each panel who were found in the roster (panel_names and panel_found, and each judge's Federal Judicial Center name and number in judge1 and judge1_nid, judge2 and judge2_nid, and so on) and the panel's composition, such as panel_party (the mean party of the presidents who appointed its judges, from -1 for Democrats to 1 for Republicans), panel_woman, panel_age, and panel_prosecutor. |
reshape_by_votes |
A table with one row for each judge on each case, with the judge's attributes (judge_party, ...), the other judges' (colleagues_party, ...), and the judge's vote (vote_reversal, the outcome or its opposite for a judge who dissented). |
reshape_by_panel |
Undoes reshape_by_votes: the table of votes becomes a table of cases again. |
reshape_by_judge |
A table with one row for each judge, with how many cases the judge decided (cases), the judge's attributes, and the shares of the judge's votes (vote_reversal, judge_dissented, ...). |
add_politics |
The party of the president in the case's year (politics_president_party) and, from a table that courtpy.politics.build_table makes, the Supreme Court's Martin-Quinn scores and the median NOMINATE scores of the Senate and House. |
merge_scdb |
For cases of the Supreme Court, the coding of the Supreme Court Database, such as scdb_issueArea, scdb_decisionDirection, and scdb_partyWinning. |
merge_idb |
For cases of the courts of appeals, the record of the appeal in the Federal Judicial Center's Integrated Database, which fjc-idb reads, such as idb_appeal_type, idb_judgment_date, idb_pro_se_party, and idb_district_judge (and, if asked, the district court's record of the case appealed). |
courtpy downloads the scores and the databases from their sources the first
time they are needed. To study one of these tables by itself (such as each
justice's vote in each case), start with its loader instead: load_roster,
load_scdb, load_idb, or load_politics, or their "download_" twins
(download_biographies, download_scdb, download_idb,
download_politics, and download_jcs), which download the latest files
first. See
examples/reshape_by_votes.ini
for a study of judges' votes.
Describing a whole study in one file
A study is an amos project, described
in one settings file. courtpy adds loaders to amos: load_court_listener,
load_lexis_nexis, and load_cases. Named first in the wrangler, a loader
collects the cases (downloading them, if asked), parses them, and codes them,
so the project needs no other data. See
examples/federal_appeals.ini:
[general]
seed = 43
label = outcome_reversal
[files]
input_folder = data
interim_folder = data
[appeals_project]
appeals_workers = wrangler, analyst, critic
[wrangler]
techniques = load_court_listener, filter_rows, keep_columns, drop_missing
[load_court_listener_parameters]
source = court_listener
download = bulk
courts = federal_circuits
start_date = 2019-01-01
end_date = 2019-12-31
save = cases.csv
reuse = true
[analyst]
design = experiment
criterion = roc_auc
steps = split, model
split_techniques = stratified
model_techniques = baseline, logit, random_forest
[critic]
techniques = scorecard
The loader finds (and saves) the cases in the input folder ("data") and saves
the coded table in the interim folder. With reuse = true, later runs load
that table instead of parsing the cases again.
Run it from a terminal with courtpy run federal_appeals.ini --export, or in
Python:
project = courtpy.Project.create("federal_appeals.ini")
print(project.result.tables["scorecard"])
project.export()
courtpy.Project is amos.Project, so everything in the amos documentation
works.
Commands
| Command | Does |
|---|---|
courtpy key set, show, delete |
Manages your CourtListener API key. |
courtpy download bulk --courts ca1 --start 2019-01-01 --end 2019-12-31 |
Extracts cases from the bulk data. |
courtpy download api --courts ca1 --start 2026-09-01 |
Downloads cases with the API. |
courtpy split FILES --folder lexis_nexis |
Divides Lexis-Nexis files of many cases. |
courtpy parse FOLDER --output cases.csv |
Parses and codes saved cases into a table. |
courtpy rules my_rules.csv |
Checks a file of rules and lists the variables it makes. |
courtpy run study.ini --export |
Runs a whole study. |
Add --help to any command for its options. See the
documentation for a tutorial, the
advanced user guide, a guide to writing rules, and recipes.
Contributing
Contributors are always welcome. Feel free to grab an issue to work on or make a suggested improvement. If you wish to contribute, please read the Contribution Guide and Code of Conduct.
Similar Projects
- CourtListener and the Free Law Project's tools, including eyecite (which finds and resolves legal citations in text) and juriscraper (which collects opinions from court websites).
- amos, which
courtpyuses to analyze the cases it parses.
Citation
If you use courtpy in your research, please cite it. GitHub's "Cite this
repository" button reads the same information from
CITATION.cff.
@software{yung_courtpy,
author = {Yung, Corey Rayburn},
title = {courtpy: Accessible tools for collecting, parsing, and analyzing court opinions},
version = {0.3.2},
year = {2026},
url = {https://github.com/WithPrecedent/courtpy},
license = {Apache-2.0}
}
License
Use of this repository is authorized under the Apache Software License 2.0.
Metadata
Release files for courtpy 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| courtpy-0.3.2.tar.gz | 198.6 kB | Without provenance | Details for courtpy-0.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| courtpy-0.3.2-py3-none-any.whl | Python 3 | none | any | Details for courtpy-0.3.2-py3-none-any.whl |
Total release size: 352.2 kB
Release files / courtpy-0.3.2.tar.gz
| Download URL | courtpy-0.3.2.tar.gz |
|---|---|
| Size | 198.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5726465d329ebd267d3965a433a574e9893283e7b98552c117c3f9cd29ae7023
|
|
BLAKE2b-256 checksum How to use checksums |
a2f1b68dfd7da8918344845a37d951071463a42fbd2141598adb922df0eaaf4b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / courtpy-0.3.2-py3-none-any.whl
| Download URL | courtpy-0.3.2-py3-none-any.whl |
|---|---|
| Size | 153.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7b1394e762256e0c85f75c8fae97a78350a73e77f2e18a3c235f008a60aabedd
|
|
BLAKE2b-256 checksum How to use checksums |
e42922b2e7adc6a18e58f36964d0ae25f8aab857e0279cae948cb88bd4b2ad24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for courtpy 0.3.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.