BerriAI/litellm · error · OCIError

Invalid OCI region {region!r}: must match ^[a-z][a-z0-9-]{{0

Error message

Invalid OCI region {region!r}: must match ^[a-z][a-z0-9-]{{0,30}}[a-z0-9]$ (e.g. 'us-ashburn-1').

What it means

get_oci_base_url validates the resolved OCI region against ^[a-z][a-z0-9-]{0,30}[a-z0-9]$ before building https://inference.generativeai.<region>.oci.oraclecloud.com. An invalid region (wrong type, uppercase, underscores, spaces, or a full URL pasted as a region) raises OCIError(400) with the regex and an example.

Source

Thrown at litellm/llms/oci/common_utils.py:192


_OCI_REGION_RE: Final = re.compile(r"^[a-z][a-z0-9-]{0,30}[a-z0-9]$")
_OCI_ACTION_PATH_RE: Final = re.compile(rf"/{OCI_API_VERSION}/actions/[^/?#]+/?$")


def get_oci_base_url(optional_params: dict, api_base: str | None = None) -> str:
    """Return the OCI inference base URL, respecting any explicit api_base override.

    If ``api_base`` already ends with a fully-formed OCI action path
    (``/{OCI_API_VERSION}/actions/<name>``), that suffix is stripped so callers
    can append their own action path without producing a doubled URL.
    """
    if api_base:
        return _OCI_ACTION_PATH_RE.sub("", api_base).rstrip("/")
    creds: Final = resolve_oci_credentials(optional_params)
    region: Final = creds["oci_region"]
    if not isinstance(region, str) or not _OCI_REGION_RE.match(region):
        raise OCIError(
            status_code=400,
            message=(
                f"Invalid OCI region {region!r}: must match ^[a-z][a-z0-9-]{{0,30}}[a-z0-9]$ (e.g. 'us-ashburn-1')."
            ),
        )
    return f"https://inference.generativeai.{region}.oci.oraclecloud.com"


# ---------------------------------------------------------------------------
# Signing implementations (shared by chat, embed, and rerank configs)
# ---------------------------------------------------------------------------


def sign_with_oci_signer(
    headers: dict,
    optional_params: dict,
    request_data: dict,
    api_base: str,

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Set a valid three-part region identifier: us-ashburn-1, eu-frankfurt-1, uk-london-1, ap-tokyo-1, etc.
  2. Trim the env value: region.strip().lower() before setting OCI_REGION.
  3. If you actually want a custom endpoint, pass it as api_base instead of as the region.
  4. Verify with: echo "$OCI_REGION" | cat -A to reveal hidden whitespace.

Example fix

# before
os.environ["OCI_REGION"] = "US-Ashburn-1 "  # OCIError(400)

# after
import os, re
region = os.environ.get("OCI_REGION", "").strip().lower()
assert re.match(r"^[a-z][a-z0-9-]{0,30}[a-z0-9]$", region), f"bad region: {region!r}"
os.environ["OCI_REGION"] = region
Defensive patterns

Strategy: validation

Validate before calling

import os, re
region = os.environ.get("OCI_REGION", "").strip().lower()
if not re.match(r"^[a-z][a-z0-9-]{0,30}[a-z0-9]$", region):
    raise ValueError(f"Invalid OCI region: {region!r} — expected e.g. 'us-ashburn-1'")

Type guard

import re

def is_valid_oci_region(region: object) -> bool:
    return isinstance(region, str) and bool(re.match(r"^[a-z][a-z0-9-]{0,30}[a-z0-9]$", region))

Try / catch

from litellm.llms.oci.common_utils import OCIError
try:
    litellm.completion(model="oci/...", messages=m)
except OCIError as e:
    if e.status_code == 400 and "Invalid OCI region" in str(e):
        fix_region_config()
    raise

Prevention

When it happens

Trigger: OCI_REGION / oci_region set to 'us-ashburn-1 ' (trailing space), 'US-Ashburn-1', 'us_ashburn_1', None, an empty string, or a whole endpoint URL like 'https://inference.generativeai...'; also a numeric-only or 40+ char region string.

Common situations: Region copied from an OCI Console URL that includes extra fragments; environment variables with trailing whitespace/newlines from .env files or Helm values; passing a full endpoint where a region short code is expected; CI injecting a quoted value with spaces.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/1d2baeb36a70a2a3. Report an issue: GitHub.