Skip to content

bug: honor PORTKEY_BASE_URL env var in set_base_url#435

Open
anxkhn wants to merge 1 commit into
Portkey-AI:mainfrom
anxkhn:bug/set-base-url-env-var-name
Open

bug: honor PORTKEY_BASE_URL env var in set_base_url#435
anxkhn wants to merge 1 commit into
Portkey-AI:mainfrom
anxkhn:bug/set-base-url-env-var-name

Conversation

@anxkhn

@anxkhn anxkhn commented Jul 8, 2026

Copy link
Copy Markdown

Title: Honor the PORTKEY_BASE_URL environment variable in set_base_url

Description:

  • set_base_url (portkey_ai/api_resources/utils.py) passed the PORTKEY_BASE_URL
    URL value constant ("https://api.portkey.ai/v1") to os.environ.get instead of
    the env-var name. It therefore looked up an environment variable literally named
    https://api.portkey.ai/v1 (always None), so the PORTKEY_BASE_URL environment
    variable was silently never read.
  • Add a PORTKEY_BASE_URL_ENV = "PORTKEY_BASE_URL" name constant in
    global_constants.py, next to the existing PORTKEY_API_KEY_ENV and
    PORTKEY_PROXY_ENV, and use it in set_base_url. This matches the codebase's
    existing _ENV-suffixed env-var-name convention.
  • Precedence is unchanged: explicit base_url argument > PORTKEY_BASE_URL env >
    PORTKEY_PROXY env > api_key-gated default (PORTKEY_BASE_URL value when an
    api_key is present, otherwise LOCAL_BASE_URL).
  • Add offline regression tests (tests/test_set_base_url.py) covering env
    resolution, precedence, and the defaults.

Motivation:
Before this fix, setting PORTKEY_BASE_URL (the natural env var, and the one the
SDK's own tests and examples/demo.py use) to point at a self-hosted gateway had no
effect: the client fell through to http://localhost:8787/v1 (no api_key) or the
public cloud URL https://api.portkey.ai/v1 (with an api_key), so requests silently
went to the wrong host. Only PORTKEY_PROXY worked as an env override. Reading the
correctly-named variable makes PORTKEY_BASE_URL behave as documented and as the
tests/examples already assume.

The other two references to the PORTKEY_BASE_URL value constant are intentionally
left untouched, because they legitimately use the URL value (not the env-var name):
default_api_key's base_url == PORTKEY_BASE_URL comparison, and
default_base_url's os.environ.get(PORTKEY_PROXY_ENV, PORTKEY_BASE_URL) default.

Related Issues:
None found (no existing open issue). Confirmed at runtime against main.


Test plan (in the PR / for reviewers)

python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
python -m pytest tests/test_set_base_url.py -q   # 7 passed
make lint                                        # mypy + black --check + ruff, all clean

Red -> green: reverting only the two source files (keeping the new test) makes the 3
env-honoring assertions fail with the exact buggy output (localhost / public-cloud /
PROXY fallthrough); the 4 guard tests pass on both the pre-fix and fixed code, so
they are not tautological.

set_base_url passed the PORTKEY_BASE_URL URL value constant
("https://api.portkey.ai/v1") to os.environ.get instead of the
env-var name, so it looked up an environment variable literally
named "https://api.portkey.ai/v1" (always None). As a result the
PORTKEY_BASE_URL environment variable was silently ignored and the
client fell through to the localhost default (or the public cloud
URL when an api_key was present), sending requests to the wrong host
for anyone pointing PORTKEY_BASE_URL at a self-hosted gateway.

Add a PORTKEY_BASE_URL_ENV = "PORTKEY_BASE_URL" name constant,
matching the existing PORTKEY_API_KEY_ENV / PORTKEY_PROXY_ENV
convention, and read it in set_base_url. Precedence is unchanged:
explicit base_url arg > PORTKEY_BASE_URL env > PORTKEY_PROXY env >
api_key-gated default. Add offline regression tests for set_base_url.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes set_base_url so it correctly reads the PORTKEY_BASE_URL environment variable (by using an env-var-name constant rather than the base URL value), aligning behavior with the documented/env-based configuration flow and preventing silent fall-through to unintended hosts.

Changes:

  • Add PORTKEY_BASE_URL_ENV = "PORTKEY_BASE_URL" to centralize the env-var name alongside existing *_ENV constants.
  • Update set_base_url to read os.environ.get(PORTKEY_BASE_URL_ENV) (and then fall back to PORTKEY_PROXY_ENV).
  • Add regression tests validating env resolution, precedence, and default behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tests/test_set_base_url.py Adds regression tests for set_base_url env handling and precedence.
portkey_ai/api_resources/utils.py Fixes env lookup to use the env-var name constant (PORTKEY_BASE_URL_ENV) rather than the URL value.
portkey_ai/api_resources/global_constants.py Introduces PORTKEY_BASE_URL_ENV constant consistent with existing env-var-name constants.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -0,0 +1,48 @@
import pytest
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