Fix

MLB doubleheaders duplicating game keys in your data

Short answer: a date, a home team and an away team do not identify one MLB game, because doubleheaders play two on the same day between the same clubs. Key on game_id. If you only have names and dates, add starts_at, and test for duplicates before every join.

The symptoms

Doubleheader bugs are small in number but easy to miss. They appear as follows:

  • A join between games and odds returns twice as many rows as expected for a handful of dates.
  • A box score for one game shows the other game's runs, or the sum of both.
  • A unique constraint on (date, home, away) fails on a date in the middle of a season, and a nightly load stops.
  • A deduplication step silently deletes the second game of a day, so your season has 2,402 games instead of 2,430.
  • Closing totals attached to a game are off by a run or so, because they belong to its twin.

None of them raises a clear error. The count of broken rows is small, around one game in a hundred, which is exactly why a casual check passes.

What the data holds

We pulled every final regular season game of 2025 from get_games on December 6, 2026. It took 13 pages of up to 200 games at 2 credits per page. The set held 2,430 games. Each one had a distinct game_id, an opaque string such as 401697224. Grouping the same games by their date, home team and away team gave only 2,402 distinct combinations, which means 28 keys were shared by two games each.

Take September 20, 2025 as an example. Cleveland visited Minnesota twice. The first game has game_id 401697224 and started at 18:10 UTC. Cleveland won 6-0. The second has game_id 401774634 and started at 23:10 UTC. Cleveland won 8-0. Both were nine-inning games, and both sit under the same date and the same two clubs. A key of date, home and away holds both rows, and whichever your code keeps last overwrites the other.

The odds are not shared either. In our check, the first game had one odds row with a closing total of 7.5, and the second had eight odds rows with a closing total of 8.5. If you attach odds by the date-and-team key, you can end up with a first game that carries a total taken for the second, and a second game with one row's worth of history.

A related field detail: the date for baseball is a US Eastern day, and starts_at is a UTC timestamp. They usually agree, but they are two views of the same instant, and a game on the West Coast can start after midnight UTC. Do not derive date from starts_at by cutting the string.

Cause: a natural key that is not unique

A natural key feels safe. A team plays once a day, so date plus team looks unique, and for most days it is. Doubleheaders break that, and so do suspended games that resume on a later date, and neutral-site series where two clubs play twice in one day. The pattern is common in any sport with one home team per game and no game number in the key. The fix is the same in every case: use the API's identifier.

SportsDataHQ returns game_id on every game row, every odds call and every box score. It is stable and unique across the season set. The id for the first game of a doubleheader is not a pattern you can predict, and you should not try. Take it from the schedule or the games list, and carry it through.

Step 1: count the collisions in your own data

This script pages through a full season, counts distinct ids and distinct natural keys, and prints one shared key. Set FASTBALLAPI_API_KEY first. It runs 13 calls of 2 credits.

import json, os, urllib.request
from collections import defaultdict
KEY = os.environ["FASTBALLAPI_API_KEY"]

def post(tool, body):
    req = urllib.request.Request(f"https://mcp.fastballapi.com/v1/{tool}", data=json.dumps(body).encode(),
                                 headers={"Content-Type": "application/json", "x-api-key": KEY})
    with urllib.request.urlopen(req, timeout=60) as resp:
        return resp.status, json.load(resp)["data"]

games, offset, pages = [], 0, 0
while offset is not None:
    status, data = post("get_games", {"season": 2025, "season_type": "regular", "status": "final",
                                      "limit": 200, "offset": offset})
    pages += 1
    games += data["games"]
    offset = data.get("next_offset")
print("HTTP", status, f"{pages} pages, {len(games)} games")

groups = defaultdict(list)
for g in games:
    groups[(g["date"], g["home"]["abbrev"], g["away"]["abbrev"])].append(g)
dupes = {k: v for k, v in groups.items() if len(v) > 1}
print("distinct game_id:", len({g["game_id"] for g in games}), "| distinct (date, home, away):", len(groups),
      "| keys shared by 2 games:", len(dupes))
k, v = next(iter(dupes.items()))
for g in sorted(v, key=lambda g: g["starts_at"]):
    print(k, g["game_id"], g["starts_at"], g["innings"], g["home"]["runs"], g["away"]["runs"])

On the verified run the loop finished in 13 pages with HTTP 200 and 2,430 games. It printed 2,430 distinct ids, 2,402 distinct natural keys and 28 shared keys. The example it printed was Minnesota at home to Cleveland on 2025-09-20, with two rows, ids 401697224 and 401774634, start times 18:10 and 23:10, nine innings each, and runs of 0 to 6 and 0 to 8.

Step 2: confirm that each game has its own detail

The next script pulls the full record for each game of that pair and prints the odds count and the closing totals. It costs 5 credits per game.

for game_id in ["401697224", "401774634"]:
    status, box = post("get_game", {"game_id": game_id})
    g, odds = box["game"], box["odds"]
    print(status, game_id, g["starts_at"], f'{g["away"]["abbrev"]} {g["away"]["runs"]} at {g["home"]["abbrev"]} {g["home"]["runs"]}',
          "| odds rows:", len(odds), "| closing totals:", sorted({o["total_close"] for o in odds if o["total_close"] is not None}))

The output showed 6-0 with 1 odds row and a closing total of 7.5 for the first game, and 8-0 with 8 odds rows and a closing total of 8.5 for the second. Two games, two records, and no shared fields except the clubs and the date.

Step 3: use game_id as the join key everywhere

Make game_id the primary key of your games table and the foreign key of every table that hangs off a game, including odds, box scores, plate appearances and your own model output. Do not create a surrogate from the date and the teams, and do not use it as a unique constraint. If you need a human-readable key for a report, build one from the date, the clubs and the UTC start time, and treat it as a label.

When a source gives you no id, a composite of date, home, away and starts_at separates two games unless they start at the same minute, which a doubleheader never does. Keep a uniqueness test on the composite in your pipeline, so a collision stops the load before it corrupts a table.

Step 4: guard the deduplication step

A deduplication step is where the second game vanishes. Before you drop duplicate rows, group on game_id. A duplicate by id is a real duplicate, such as a page you fetched twice. A duplicate by date and teams is a doubleheader and must stay. A quick assertion after the dedupe is that the number of games per club in a regular season is what you expect, and the 2025 season had 2,430 games in all.

When not to use this

  • You need a single game per day. If your question is about a club's daily result, aggregate by date on purpose, and add the number of games that day as a column, so nobody reads a doubleheader as one game.
  • You work in a sport with no doubleheaders. The risk is smaller in hockey and basketball, where a club plays at most once per day. Use the game id anyway, because the cost is nothing.
  • You need to know which game was first. starts_at orders them, but the API does not label them game 1 and game 2. Sort by starts_at and number them in your own code.
  • You need lines from after play began. Odds and props are pre-game only, which applies to every game in a doubleheader.

Live from the API

Free to embed on your own site, with no key needed. Get the embed code.

Get a free key

Checked against the live API on 2026-12-06. Questions: hello@sportsdatahq.com.