mlco2 / mlco2/codecarbon

tracker.run_id stays None when OutputMethod.API is enabled, CSV rows record run_id="None"

Open
#1,303 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.9k
Forks
323
Avg merge
1d 12h
Merged PRs (30d)
12

Description

Problem

When OutputMethod.API is enabled, EmissionsTracker.run_id stays None for the whole run. Every emission record stringifies it, so all CSV rows are written with the literal string "None" instead of a real run id, and the data can no longer be joined with what the API stored under a real UUID.

Reproduction

from codecarbon import EmissionsTracker
from codecarbon.output import OutputMethod

tracker = EmissionsTracker(output_methods=[OutputMethod.CSV, OutputMethod.API], api_key="...")
print(tracker.run_id)  # None
tracker.start(); ...; tracker.stop()
# emissions.csv -> run_id column contains "None"

Root cause

emissions_tracker.py:648 takes the id eagerly from the API output handler:

self.run_id = cc_api__out.run_id

but CodeCarbonAPIOutput.__init__ builds its ApiClient with create_run_automatically=False (output_methods/http.py:51-58), so ApiClient.run_id is still the class attribute None (core/api_client.py:36). The run is created lazily later in CodeCarbonAPIOutput._ensure_api_run (output_methods/http.py:60-63), which updates the handler's id but never writes back to the tracker. emissions_tracker.py:1049 then records run_id=str(self.run_id)"None".

Expected vs actual

Expected: tracker.run_id is never None, and matches the API run id once the run exists.

Actual: it is None, with knock-on effects:

  • on_csv_write="update" matches rows on df.run_id == total.run_id (output_methods/file.py:109); with every run writing "None", a second run overwrites the first run's row, and a third logs "CSV contains more than 1 rows with current run ID".
  • FileOutput.task_out builds per-task filenames as emissions_<experiment>_<run_id>.csv (output_methods/file.py:134-137), so every run's task data collides in emissions_<experiment>_None.csv.
  • The BOAMPS output names its report boamps_report_None.json (output_methods/boamps/output.py:155) for the same reason.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with emissions_tracker.py around lines 648 and 1049, then read CodeCarbonAPIOutput in output_methods/http.py around lines 51-63 and the related CSV logic in output_methods/file.py. Run the provided API-plus-CSV reproduction and verify that tracker.run_id matches the API run and that CSV, task, and BOAMPS outputs no longer use None.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.