Downloads court opinions from CourtListener and reads them.
CourtListener (https://www.courtlistener.com) is a free legal research site
run by the Free Law Project. CourtListener downloads its opinions in two
ways (its "method"):
- "bulk" (the default) extracts them from the bulk data files, which need no key and have no limits, but are very large and are updated every three months.
- "api" downloads them with the REST API, which needs an API key (see
courtpy.secrets). It is best for small or very recent sets of cases, because CourtListener limits how many requests an account can make (a free account can make 5 a minute, 50 an hour, and 125 a day, as of 2026). The client waits when asked to, saves its place, and resumes where it stopped when it is run again.
Both save each case as a JSON file named for its CourtListener cluster id, in
a folder named for its court (such as "courtlistener/ca1/4567890.json"),
which read_case turns into a Case for parsing.
The bulk data is published every three months (on the last day of March, June, September, and December), in a public bucket: https://com-courtlistener-storage.s3-us-west-2.amazonaws.com/list.html?prefix=bulk-data/
Five files hold what courtpy needs, and every one is always read, so that each case is put in the court of its docket:
| File | Size (2026) | Holds |
|---|---|---|
| courts | < 1 MB | Each court's name and jurisdiction. |
| dockets | about 5 GB | Each case's court, docket number, and dates. |
| opinion-clusters | about 2.5 GB | Each decision's parties, judges, date, and status. |
| citations | about 130 MB | Each decision's reporter citations. |
| opinions | about 55 GB | The text of each opinion. |
They are read in that order, keeping the cases of the courts and dates asked
for, and each case is saved in the same form as the API's, so the same parser
reads both. Every case is saved with its whole docket (all of its fields but
those in options._DOCKET_SKIPPED), so the dockets file is read twice: first
to find which dockets belong to the courts, and again, once the cases are
known, to keep the dockets of those cases alone. Reading the opinions file
takes hours, so it is worth extracting every court and year you may need at
once.
The files are read with amos.loaders.read_chunks, which decompresses and
parses them on every core and keeps only the rows asked for. By default, they
are downloaded first (to a folder outside of any project, see
courtpy.utilities.data_folder) with amos.loaders.download_file and kept,
so that they can be used again and an interrupted download resumes. With
stream = True, they are read straight from the bucket instead, which needs
no disk space but starts over if the connection fails.
Contents
DOWNLOAD_METHODS: the ways that CourtListener.download can download.
CourtListener: downloads cases from CourtListener's bulk data or API.
CourtListenerSource: reads cases saved by CourtListener.
CourtListenerError: raised when the API returns an error.
RateLimitError: raised when the API asks for a longer pause than allowed.
add_opinions: adds opinions to a saved case.
case_path: returns the path of a saved case.
citation_strings: returns citations as text.
is_complete: returns whether a case was saved with its opinions.
opinion_role: classifies an opinion as majority, concurrence, or dissent.
opinion_text: returns the plain text of an opinion.
read_case: reads a saved case for parsing.
save_case: saves a case.
slim_opinion: keeps one field of an opinion's text, to save space.
logger: the module's logger, which reports the progress of downloads,
of reading the bulk files, and pauses asked for by CourtListener.
CourtListener(api_key=None, api_url=options._API_URL, bulk_url=options._BULK_URL, bulk_folder=None, bulk_date=None, stream=False, min_interval=options._MIN_INTERVAL, max_wait=options._MAX_WAIT, timeout=options._TIMEOUT, session=None)
dataclass
¶
Downloads cases from CourtListener's bulk data or its REST API.
download takes the "method" to download with: "bulk" (the default),
which extracts the cases from the bulk data files and needs no key, or
"api", which downloads them with the REST API (version 4).
Every request to the API includes the API key, which is found with
courtpy.secrets.get_api_key when the first request is made if it was
not passed, so the bulk data can be used without a key. Requests to the
API are spaced at least min_interval seconds apart. If CourtListener
asks for a pause (an HTTP 429 response), the client waits as long as it
is asked, up to max_wait seconds. A longer pause (such as the daily
limit) raises a RateLimitError, and download can be run again later
to resume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str | None
|
a CourtListener API key. Defaults to |
None
|
api_url
|
str
|
address of the API. Defaults to |
_API_URL
|
bulk_url
|
str
|
address of the bucket of bulk data. Defaults to
|
_BULK_URL
|
bulk_folder
|
Path | str | None
|
folder to keep the downloaded bulk files in. Defaults
to |
None
|
bulk_date
|
str | None
|
the date of the bulk files to use, as YYYY-MM-DD. Defaults
to |
None
|
stream
|
bool
|
whether to read the bulk files straight from the bucket
instead of downloading them. Defaults to |
False
|
min_interval
|
float
|
fewest seconds between requests to the API. Defaults to
|
_MIN_INTERVAL
|
max_wait
|
float
|
most seconds to wait when the API asks for a pause.
Defaults to |
_MAX_WAIT
|
timeout
|
float
|
seconds to wait for a response. Defaults to
|
_TIMEOUT
|
session
|
Any
|
an object with a |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
requests_made |
int
|
number of requests made to the API by this client. |
_courts |
dict[str, dict[str, Any]]
|
information about each court that has been looked up (see
|
_last_request |
float
|
when the last request was made, from
|
bulk_dates(kind='opinions')
¶
Returns the dates of the bulk files of one kind in the bucket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
"courts", "dockets", "opinion-clusters", "citations", or "opinions". Defaults to "opinions". |
'opinions'
|
Returns:
| Type | Description |
|---|---|
list[str]
|
The dates, from oldest to newest. |
Source code in src/courtpy/sources/courtlistener.py
court(court)
¶
Returns information about a court.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
court
|
str
|
a CourtListener court id, such as "ca1". |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The court's "id", "full_name", "short_name", "citation_string", and "jurisdiction" (such as "F" for federal appellate courts). |
Source code in src/courtpy/sources/courtlistener.py
download(courts, start_date=None, end_date=None, folder='courtlistener', *, method='bulk', max_cases=None, dockets=False, refresh=False)
¶
Downloads the cases of courts filed between two dates.
With "bulk", the bulk files are read in turn (see the module's documentation), and the cases that were already saved with their opinions are skipped. Every case is saved with its docket.
With "api", each page of cases (opinion clusters) takes one request, and their opinions take one more (or more, if they have many opinions). Cases that were already saved (with their opinions) are skipped, and the place in each court's list of cases is saved, so running a download again resumes it. Running a finished download again takes one request for each court and saves any cases added to CourtListener since.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
courts
|
str | Iterable[str]
|
CourtListener court ids (such as "ca1") or names of groups of them (such as "federal_appellate"). |
required |
start_date
|
str | None
|
earliest date filed, as YYYY-MM-DD. Defaults to |
None
|
end_date
|
str | None
|
latest date filed, as YYYY-MM-DD. Defaults to |
None
|
folder
|
Path | str
|
folder to save the cases in. Defaults to "courtlistener". |
'courtlistener'
|
method
|
str
|
"bulk" or "api" (see |
'bulk'
|
max_cases
|
int | None
|
most cases to save: in all with "bulk", and in each
court with "api". Defaults to |
None
|
dockets
|
bool
|
with "api", whether to also download each case's docket
(every field but those in |
False
|
refresh
|
bool
|
whether to download cases that were already saved.
Defaults to |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
RateLimitError
|
if CourtListener asks for a pause longer than
|
Returns:
| Type | Description |
|---|---|
list[Path]
|
The paths of the cases saved (or updated) in this run. |
Source code in src/courtpy/sources/courtlistener.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
download_case(cluster_id, folder='courtlistener')
¶
Downloads one case by its CourtListener cluster id.
This takes four requests: the case, its opinions, its docket (which names its court), and its court (unless another case from the same court was downloaded by this client).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cluster_id
|
int | str
|
the id of the opinion cluster (the number in the case's CourtListener address, after "/opinion/"). |
required |
folder
|
Path | str
|
folder to save the case in. Defaults to "courtlistener". |
'courtlistener'
|
Returns:
| Type | Description |
|---|---|
Path
|
The path of the saved case. |
Source code in src/courtpy/sources/courtlistener.py
fetch_bulk_file(kind)
¶
Downloads one bulk file, resuming a download that was interrupted.
The file is downloaded with amos.loaders.download_file, which
writes it beside its path (with ".part" added) until it is whole, so
a file in the bulk folder is always complete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
"courts", "dockets", "opinion-clusters", "citations", or "opinions". |
required |
Raises:
| Type | Description |
|---|---|
OSError
|
if the download stopped before the whole file arrived (run it again to resume). |
HTTPError
|
if the file cannot be downloaded. |
Returns:
| Type | Description |
|---|---|
Path
|
The path of the downloaded file. |
Source code in src/courtpy/sources/courtlistener.py
fetch_pdf_headers(paths, *, refresh=False, delay=None, workers=4)
¶
Saves the first page of each case's PDF, from its court's website.
CourtListener's text of many opinions starts after their header, so
it lacks the panel ("Before: FERNANDEZ, GOULD, and M. SMITH, Circuit
Judges"), and its list of judges may give only their last names,
which two judges of a court can share. The court's own PDF of the
opinion has the header on its first page. For each saved case, the
PDF at its first opinion's "download_url" is downloaded, the text of
its first page is saved in the case's file as "pdf_header" (with
"pdf_header_status"), and the PDF is not kept. read_case makes it a
section of its own, which the rule that finds the panel reads after
the opinion's opening.
The requests to each website are made one at a time, delay seconds
apart, and up to workers websites are asked at once. The API key is
never sent. A case whose file already has a header (or a status, if
its PDF could not be read) is skipped unless refresh is true.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
Iterable[Path | str]
|
paths of saved cases. |
required |
refresh
|
bool
|
whether to fetch the PDF of a case again. Defaults to
|
False
|
delay
|
float | None
|
seconds between requests to one website. Defaults to
|
None
|
workers
|
int
|
websites to ask at once. With 1, every request is made
with |
4
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A row for each case, with its "path", "url", and "status": "ok", "saved before", "no url", "http {code}" (such as "http 404"), "not a pdf", "unreadable", or "failed: {error}". |
Source code in src/courtpy/sources/courtlistener.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | |
get(path, params=None)
¶
Returns the JSON response of one request to the API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
a path in the API (such as "clusters/") or a full address (such as the "next" page of a response). |
required |
params
|
Mapping[str, Any] | None
|
query parameters. Defaults to |
None
|
Raises:
| Type | Description |
|---|---|
CourtListenerError
|
if the API returns an error, rejects the key, or cannot be reached after several tries. |
MissingAPIKeyError
|
if no key was passed and none can be found
(see |
RateLimitError
|
if the API asks for a pause longer than
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The decoded JSON. |
Source code in src/courtpy/sources/courtlistener.py
pages(path, params=None)
¶
Yields the results of each page of a list, and the next address.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
a path in the API (such as "clusters/") or the address of a page. |
required |
params
|
Mapping[str, Any] | None
|
query parameters for the first page. Defaults to |
None
|
Yields:
| Type | Description |
|---|---|
tuple[list[dict[str, Any]], str | None]
|
tuple[list[dict[str, Any]], str | None]: the results on each page
and the address of the next page (or |
Source code in src/courtpy/sources/courtlistener.py
read_bulk_chunks(kind, *, columns=None, keep=None)
¶
Yields chosen rows and columns of one bulk file, a chunk at a time.
The file is read with amos.loaders.read_chunks, which parses and
decompresses it on every core and finds the rows in keep before
any of their text becomes Python objects. If stream is true, the
file is read from the bucket as it arrives, without being saved.
Otherwise, it is downloaded first (or found in the bulk folder) with
fetch_bulk_file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
"courts", "dockets", "opinion-clusters", "citations", or "opinions". |
required |
columns
|
Sequence[str] | None
|
the columns to return. Defaults to |
None
|
keep
|
Mapping[str, Any] | None
|
the values to keep of each of some columns, such as
|
None
|
Yields:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: the rows of each chunk that are kept, with every
value as text. Missing values are empty |
Source code in src/courtpy/sources/courtlistener.py
read_bulk_rows(kind, *, columns=None, keep=None)
¶
Yields chosen rows of one bulk file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
"courts", "dockets", "opinion-clusters", "citations", or "opinions". |
required |
columns
|
Sequence[str] | None
|
the columns to return. Defaults to |
None
|
keep
|
Mapping[str, Any] | None
|
the values to keep of each of some columns (see
|
None
|
Yields:
| Type | Description |
|---|---|
dict[str, str]
|
dict[str, str]: each row, mapping column names to text. Missing
values are empty |
Source code in src/courtpy/sources/courtlistener.py
resolve_bulk_date()
¶
Returns bulk_date, finding the newest complete set if needed.
Raises:
| Type | Description |
|---|---|
LookupError
|
if no date has every kind of file that courtpy needs. |
Returns:
| Type | Description |
|---|---|
str
|
The date of the bulk files to use, as YYYY-MM-DD. |
Source code in src/courtpy/sources/courtlistener.py
CourtListenerError
¶
Bases: RuntimeError
Raised when the CourtListener API returns an error.
CourtListenerSource()
dataclass
¶
Bases: Source
Reads cases saved by CourtListener, from the bulk data or the API.
Each case is a JSON file (see read_case), whose sections come from its
data, so no rules are needed to find them. The "courtlistener_header"
rules find the parties and judges in them.
RateLimitError(seconds)
¶
Bases: CourtListenerError
Raised when the API asks for a longer pause than courtpy will wait.
Its message says how long to wait and how to resume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
how long CourtListener asked courtpy to wait. |
required |
Source code in src/courtpy/sources/courtlistener.py
add_opinions(path, opinions)
¶
Adds opinions to a saved case, replacing any with the same id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
path of the saved case. |
required |
opinions
|
Iterable[Mapping[str, Any]]
|
opinions to add. |
required |
Source code in src/courtpy/sources/courtlistener.py
case_path(folder, court, cluster_id)
¶
Returns the path of a saved case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder
|
Path | str
|
the folder of saved cases. |
required |
court
|
str
|
the case's CourtListener court id. |
required |
cluster_id
|
int | str
|
the case's CourtListener cluster id. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
"{folder}/{court}/{cluster_id}.json". |
Source code in src/courtpy/sources/courtlistener.py
citation_strings(citations)
¶
Returns citations as text, such as "123 F.3d 456".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
citations
|
Iterable[Any]
|
citations from CourtListener: |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
The citations as text. |
Source code in src/courtpy/sources/courtlistener.py
is_complete(path)
¶
Returns whether a case was saved with at least one opinion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
path of a saved case. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the file exists, can be read, and has an opinion. |
Source code in src/courtpy/sources/courtlistener.py
opinion_role(kind)
¶
Classifies an opinion by its CourtListener type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str | None
|
the opinion's "type", such as "010combined", "020lead", "030concurrence", "035concurrenceinpart", or "040dissent". |
required |
Returns:
| Type | Description |
|---|---|
str
|
"majority" (combined, lead, unanimous, or plurality opinions, and opinions without a type), "concurrence", "dissent", "mixed" (an opinion concurring in part and dissenting in part), or "other" (such as an addendum). |
Source code in src/courtpy/sources/courtlistener.py
opinion_text(opinion)
¶
Returns the plain text of an opinion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
opinion
|
Mapping[str, Any]
|
an opinion from CourtListener. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text of the first field in |
Source code in src/courtpy/sources/courtlistener.py
read_case(path)
¶
Reads a saved CourtListener case for parsing.
The sections of text have the same names as the sections that rules find
in Lexis-Nexis cases (such as "party", "history", "counsel", and
"judges"), so that the same rules work for both. "court_num" and
"court_name" are courtpy's number and name for the case's court, found
by CourtListener's id for it in the table of courts of
options._DEFAULT_JURISDICTION (see
courtpy.courts.court_numbers_by_courtlistener_name), and
"lower_court_num" and "lower_court_name" are those of the court appealed
from, by the docket's id for it. Each is None if the table does not
have the court (the header rules then look for the court appealed from
in the history).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
path of a case saved by |
required |
Returns:
| Type | Description |
|---|---|
Case
|
The case. |
Source code in src/courtpy/sources/courtlistener.py
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 | |
save_case(path, record)
¶
Saves a case as a JSON file, replacing any file at path safely.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
path to save the case to. Its folder is created if needed. |
required |
record
|
Mapping[str, Any]
|
the case: "source", "court", "cluster", "docket", "citations", and "opinions". |
required |
Source code in src/courtpy/sources/courtlistener.py
slim_opinion(opinion)
¶
Returns an opinion with only one field of text, to save space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
opinion
|
Mapping[str, Any]
|
an opinion from the API or the bulk data. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The fields in |