Monday, October 25, 2021

An API for Similar Bills Using FastAPI

In my previous post, I discussed how to define similarity for bills in Congress. Given a new bill, which other bills are similar, and how are they related?

We're building applications to let researchers look up any bill and get a list of similar bills. To do this, we separate the hard question (which bills are similar) from an easier problem (how to show the list of similar bills). By separating the problems, we can make progress on both at the same time.

The key to separating the two problems is an Application Programming Interface (API). An API is a technical specification that defines what response the application will return for a given request. When a researcher asks "What bills are similar to H.R. 200, the National Intersection and Interchange Safety Construction Program Act?", the API will respond with a list of related bills, and a predictable set of data for each bill: its number, title, the 'relatedness' score, etc. The API does not determine *which* bills are listed (that is the harder problem), but what information we list for the bills we do list.

Creating an API requires us to define, ahead of time, the relevant information that a researcher will want. Do they want the date of introduction of the bill? The length of each bill? This information is gathered by talking with the researchers themselves to know what is relevant to their research.

As a starting point, I've created an API that returns a list of bills with the bill number, bill title, similarity scores, and the reasons for considering the bills related. These reasons include the bills being identical, nearly identical, or having only some sections in common. With this starting point, we hope to discover what set of information a researcher actually wants when looking for related bills.

A live demonstration, with data up-to-date through September 2021, is here (see sample response below):

Sample API response, bills similar to H.R. 200 (117th)

Technical notes

The code for this API is here:

The API was built using a terrific Python library, FastAPI, that makes it easy to define and document an API, and has become my favorite way to quickly build a prototype "back end" for an application, and to communicate ideas with other developers.