Skip to content

Conversation

johncalesp
Copy link
Contributor

This helps with reading env vars from user input through:
export VAR=VAL or
VAR=VAL python script.py

@michaelshin
Copy link
Contributor

Does the Pydantic model not pick up the env vars? It claims it does it in the documentation. See https://docs.pydantic.dev/latest/concepts/pydantic_settings/#dotenv-env-support

@johncalesp
Copy link
Contributor Author

It throws an error:
my .env file is this


CENTML_WEB_URL=some_url
CENTML_CONFIG_PATH=/home/john/Documents/centml/platform/centml-python-client/sandbox
CENTML_CRED_FILE=credentials.json
CENTML_PLATFORM_API_URL=some_url
CENTML_FIREBASE_API_KEY=key

and I get this error:

Traceback (most recent call last):
  File "/home/john/Documents/centml/platform/centml-python-client/sandbox/endpoint.py", line 301, in <module>
    hardware_data = get_hardware_information(cluster_id, hardware_instance_id)
  File "/home/john/Documents/centml/platform/centml-python-client/sandbox/endpoint.py", line 207, in get_hardware_information
    with get_centml_client() as cclient:
  File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/home/john/Documents/centml/platform/centml-python-client/centml/sdk/api.py", line 68, in get_centml_client
    host=settings.CENTML_PLATFORM_API_URL, access_token=auth.get_centml_token()
  File "/home/john/Documents/centml/platform/centml-python-client/centml/sdk/auth.py", line 54, in get_centml_token
    exp_time = int(jwt.decode(cred["id_token"], options={"verify_signature": False})["exp"])
KeyError: 'id_token'

but If I do this, it works fine

CENTML_WEB_URL=some_url CENTML_CONFIG_PATH=$(pwd) CENTML_CRED_FILE=credentials.json CENTML_PLATFORM_API_URL=some_url CENTML_FIREBASE_API_KEY=key python endpoint.py

Also, another reason is the variable CENTML_CONFIG_PATH, this adds flexibility from where you call the script, since when you use github actions I don't know for sure whats the path and I don't think $(pwd) is allowed in .env file

@michaelshin
Copy link
Contributor

My comment meant that the current configuration should be able to pick up the env vars if you set them like

CENTML_WEB_URL=some_url CENTML_CONFIG_PATH=$(pwd) CENTML_CRED_FILE=credentials.json CENTML_PLATFORM_API_URL=some_url CENTML_FIREBASE_API_KEY=key python endpoint.py

If it doesn't work, I think it's a bug in how we set up the configuration

@johncalesp
Copy link
Contributor Author

So I tested with new environment and test with main branch, and does not seem to pick up the env vars.
command

(venv) ➜  centml-python-client git:(main) ✗ CENTML_WEB_URL=https://app.centml.org CENTML_CONFIG_PATH=$(pwd) CENTML_CRED_FILE=credentials.json CENTML_PLATFORM_API_URL=https://api.centml.org CENTML_FIREBASE_API_KEY=AIzaSyBXSNjruNdtypqUt_CPhB8QNl8Djfh5RXI python endpoint.py --image-url 544849402588.dkr.ecr.us-east-1.amazonaws.com/cserve-nightly:cserve-weekly-2.1.0.debug.name-cuda --hardware cuda
Traceback (most recent call last):
  File "/home/john/Downloads/centml-python-client/endpoint.py", line 656, in <module>
    hardware_data = get_hardware_information(cluster_id, hardware_instance_id)
  File "/home/john/Downloads/centml-python-client/endpoint.py", line 506, in get_hardware_information
    with get_centml_client() as cclient:
  File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/home/john/Downloads/centml-python-client/centml/sdk/api.py", line 68, in get_centml_client
    host=settings.CENTML_PLATFORM_API_URL, access_token=auth.get_centml_token()
  File "/home/john/Downloads/centml-python-client/centml/sdk/auth.py", line 54, in get_centml_token
    exp_time = int(jwt.decode(cred["id_token"], options={"verify_signature": False})["exp"])
KeyError: 'id_token'

Credential File

{"access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjhkMjUwZDIyYTkzODVmYzQ4NDJhYTU2YWJhZjUzZmU5NDcxNmVjNTQiLCJ0eXAiOiJKV1QifQ.eyJuYW1lIjoiSm9obiBDYWxkZXJvbiIsInBpY3R1cmUiOiJodHRwczovL2xoMy5nb29nbGV1c2VyY29udGVudC5jb20vYS9BQ2c4b2NKbDdWSThvSVptTkg0dlh5NHVla1ZZZHVOLVpPU292Q01JQWM3ZDNhZ049czk2LWMiLCJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vcGxhdGZvcm0tZGV2LTZjM2E3IiwiYXVkIjoicGxh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ZRJeCTns-MlxRLFvhS9zg7aXvlHhzM2FEnaGTV9pqzZ1Av72GLfxnv-Fz4hh-eCOJKMhbQG1yKUE6JI-NuE3Dbi8KlxBIr84gOg2w29ouMJaNll_s-fMEwV7BQU6ti1UGQ3gMn6B00BnKh99Vq7RFfWVUAEB0xVtI1Fs3_fZr52d-G2-MSH4GmWyVxwxllESrfUKgk8xPg37Ms3RxxcpB2yanUUdNHPucxuPW_NEczUNRkZpTSwnuGPBV4zHGVP61Nkf8Z41RuWMJ0eDCsLTaAcnZs2RZv6NJrLILyeHjobnOYI8BNnd-b5RHebvlY126wXPNNwa91p1gJLMEDg", "user_id": "9zfmX6CouOT0n5vw0c1eotFg6WY2", "project_id": "411971949855"}%

@michaelshin
Copy link
Contributor

That's annoying I guess pydantic doesn't support it well.

CENTML_CONFIG_PATH: str = os.path.expanduser("~/.centml")
CENTML_CRED_FILE: str = "credentials.json"
CENTML_CRED_FILE_PATH: str = CENTML_CONFIG_PATH + "/" + CENTML_CRED_FILE
CENTML_WEB_URL: str = os.getenv("CENTML_WEB_URL") or "https://app.centml.com/"
Copy link
Contributor

Choose a reason for hiding this comment

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

I would change this to be the following format os.getenv(ENV_VAR, default=DEFAULT_VALUE) instead of using ors

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeap, done

@johncalesp
Copy link
Contributor Author

Merging, I don't know if unit test are working right now

@johncalesp johncalesp merged commit 9443cee into main Feb 25, 2025
1 of 2 checks passed
@johncalesp johncalesp deleted the johncalesp/sdk-config branch February 25, 2025 16:41
@michaelshin
Copy link
Contributor

Yeah it's a known issue all good to merge

@michaelshin michaelshin mentioned this pull request Apr 8, 2025
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.

2 participants