Skip to content

fix: propagate the API run id to the tracker - #1327

Open
davidberenstein1957 wants to merge 3 commits into
masterfrom
fix/api-run-id-never-propagates
Open

fix: propagate the API run id to the tracker#1327
davidberenstein1957 wants to merge 3 commits into
masterfrom
fix/api-run-id-never-propagates

Conversation

@davidberenstein1957

Copy link
Copy Markdown
Collaborator

What

EmissionsTracker.run_id is now a property backed by a locally generated uuid that returns the API run id as soon as one exists, and the API run is created in start() rather than on the first emission.

Why

With OutputMethod.API enabled, the tracker read run_id from CodeCarbonAPIOutput at construction time, but that handler builds its ApiClient with create_run_automatically=False and only creates the run lazily in _ensure_api_run. Nothing ever wrote the resulting id back, so tracker.run_id stayed None and _prepare_emissions_data recorded the literal string "None" in every row.

Consequences for users: CSV data could not be joined with the API data, on_csv_write="update" matched on "None" and overwrote the previous run's row, and per-task / BOAMPS filenames all collided on ..._None.csv.

Creating the run in start() (instead of only fixing the property) makes sure a single run has a single id everywhere, including rows persisted before the first API call. Failures there are logged and fall back to the local uuid, so an unreachable API no longer prevents tracking.

Verified

tests/test_emissions_tracker.py::TestCarbonTracker::test_run_id_with_api_output_is_never_none asserts tracker.run_id is not None and that the run id written to emissions.csv matches the API run id. It fails on master and passes with this change.

uv run pytest tests/test_emissions_tracker.py tests/output_methods tests/test_tracking_inference.py tests/test_api_call.py -q passes locally.

Closes #1303

🤖 Generated with Claude Code

When OutputMethod.API was enabled, the tracker read run_id from the API
output handler before the run existed (it is created lazily), so run_id
stayed None and every record was written with the string "None".

Make run_id a property backed by a locally generated uuid that prefers the
API run id, and create the API run in start() so all outputs of a run share
the same id.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.51%. Comparing base (065d0e6) to head (922ebd2).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1327      +/-   ##
==========================================
+ Coverage   91.39%   91.51%   +0.12%     
==========================================
  Files          49       49              
  Lines        5056     5069      +13     
==========================================
+ Hits         4621     4639      +18     
+ Misses        435      430       -5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

davidberenstein1957 and others added 2 commits August 12, 2026 17:39
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@davidberenstein1957
davidberenstein1957 marked this pull request as ready for review August 12, 2026 19:14
@davidberenstein1957
davidberenstein1957 requested a review from a team as a code owner August 12, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant