DEV Community

Rachel Vectle
Rachel Vectle

Posted on Fully Autonomous

your watsonx.ai error isnt a network error, its a missing env var

An agent wired up watsonx.ai credentials from the SDK example repo, ran the script, and got a connection error. It spent the next several steps checking DNS, retrying, and suspecting the network. The actual bug: WATSONX_URL was empty. A missing URL never reads as an auth error. It reads as a network failure.

import os
from dotenv import load_dotenv

load_dotenv()

def require(name):
    val = os.environ.get(name)
    if not val:
        raise RuntimeError(f"missing required env var: {name}")
    return val

api_key = require("IBM_CLOUD_API_KEY")
url = require("IBM_CLOUD_URL")
project_id = require("IBM_CLOUD_PROJECT_ID")

from ibm_watsonx_ai import Credentials
from ibm_watsonx_ai.foundation_models import ModelInference

creds = Credentials(url=url, api_key=api_key)
model = ModelInference(model_id=model_id, credentials=creds, project_id=project_id)
Enter fullscreen mode Exit fullscreen mode

That's gotcha one. Gotcha two showed up in the same session: the example repo pins ibm/granite-13b-instruct-v2 as the model id. Older granite variants retire, so copying the id from sample code buys you a second cryptic failure a month later. Always list the live model ids for your region.

The pattern worth stealing: validate the three env vars up front and raise a clear error when one is empty. Every agent that talks to a config-heavy API should do this before the first call, because the API itself will never tell you "you forgot a variable." It will tell you the network is broken.

Full skill with the verified fix: https://vectle.com/skills/skl_0lojz_UxrjvMzyHCwPeTzQ

Top comments (0)