Skip to content

fix: update geolocation fallback API and use static country code mapping#1104

Merged
benoit-cty merged 1 commit intomlco2:masterfrom
ucodia:fix-geolocation-fallback
Mar 16, 2026
Merged

fix: update geolocation fallback API and use static country code mapping#1104
benoit-cty merged 1 commit intomlco2:masterfrom
ucodia:fix-geolocation-fallback

Conversation

@ucodia
Copy link
Contributor

@ucodia ucodia commented Mar 13, 2026

Description

Fixes geolocation fallback API call

Issue 1

The fallback API will always fail when using the HTTPS endpoint

curl https://ip-api.com/json/
{"status":"fail","message":"SSL unavailable for this endpoint, order a key at https://members.ip-api.com/"}%

Using the HTTP endpoint is not great because this could expose the application location to eavesdropper and be vulnerable to man-in-the-middle.

Issue 2

If the fallback API call fails, it reports on the wrong URL on the exception: https://github.com/ucodia/codecarbon/blob/ebc8d1a3f5dcae9013dcbe6883d05e711ad6ce28/codecarbon/external/geography.py#L136

Non-issue 1

The current fallback relies on another API to map the 2 letters country code to a 3 letters code. This is static data that almost never changes, by switching to a static mapping provider we can make the fallback more resilient.

Related Issue

Fixes #1101

Motivation and Context

Certain IP ranges are not covered by the primary API and therefore a fallback is necessary otherwise CodeCarbon client will estimate on the wrong energy mix.

How Has This Been Tested?

  • Unit testing
  • Tested in production environment

Screenshots (if appropriate):

N/A

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

Go over all the following points, and put an x in all the boxes that apply.

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@ucodia ucodia marked this pull request as ready for review March 14, 2026 00:20
@ucodia ucodia requested a review from a team as a code owner March 14, 2026 00:20
@codecov
Copy link

codecov bot commented Mar 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.25%. Comparing base (b655866) to head (89e5a62).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1104      +/-   ##
==========================================
+ Coverage   78.21%   78.25%   +0.04%     
==========================================
  Files          38       38              
  Lines        3631     3638       +7     
==========================================
+ Hits         2840     2847       +7     
  Misses        791      791              

☔ View full report in Codecov by Sentry.
📢 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.

@benoit-cty
Copy link
Contributor

Thanks Lionel for testing your previous PR in a production environment.

Can you run uv run pre-commit run --all-files to fix the error.

I will prefer to avoid adding a dependency. But it is not really related to your PR. I think we could handle this later. Maybe @SaboniAmine could add the 2 letters code in the carbon intensity dataset from https://github.com/pycountry/pycountry/blob/main/src/pycountry/databases/iso3166-1.json in his work on https://github.com/mlco2/codecarbon-hardware-data . So we will remove the dependency later.

@ucodia ucodia force-pushed the fix-geolocation-fallback branch from 7d4ac4b to 89e5a62 Compare March 16, 2026 16:18
@ucodia
Copy link
Contributor Author

ucodia commented Mar 16, 2026

@benoit-cty ran the pre-commit script and fixed the code formatting issue.

I understand we want to keep dependency to the minimum, though highlighting we really traded a weak network dependency for a direct code dependency, which should improves the library resiliency.

Copy link
Contributor

@benoit-cty benoit-cty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will open an issue to remove this dependency later.

@benoit-cty benoit-cty enabled auto-merge March 16, 2026 16:33
@benoit-cty benoit-cty merged commit a6de0ba into mlco2:master Mar 16, 2026
10 checks passed
@SaboniAmine
Copy link
Member

Hi, thanks for this contribution!
Indeed, I guess adding temporarily pycountry is a good trade-off, as the work to integrate the new dataset still needs 1-2 weeks to be complete.

@benoit-cty
Copy link
Contributor

Hi, thanks for this contribution! Indeed, I guess adding temporarily pycountry is a good trade-off, as the work to integrate the new dataset still needs 1-2 weeks to be complete.

Issue created : https://github.com/mlco2/codecarbon-hardware-data/issues/3

@ucodia ucodia deleted the fix-geolocation-fallback branch March 16, 2026 19:28
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.

Geolocation fallback provider is not working

3 participants