openai/openai-python · error · OpenAIError

The Bedrock AWS `region` is invalid. Use a standard AWS regi

Error message

The Bedrock AWS `region` is invalid. Use a standard AWS region such as `us-east-1`.

What it means

The Bedrock provider validates the AWS region against a standard region pattern (e.g. `us-east-1`, `eu-central-1`) and throws this OpenAIError when the supplied region string does not match. This catches typos and non-existent regions before a request is ever signed or sent.

Source

Thrown at src/openai/providers/bedrock.py:79

    return "amazonaws.com", "api.aws"


def _parse_bedrock_endpoint_hostname(hostname: str) -> tuple[BedrockEndpoint, str] | None:
    service, separator, remainder = hostname.removesuffix(".").lower().partition(".")
    region, region_separator, suffix = remainder.partition(".")
    if not separator or not region_separator or _AWS_REGION.fullmatch(region) is None:
        return None

    if service == "bedrock-mantle" and suffix == "api.aws":
        return "mantle", region
    if service in {"bedrock-runtime", "bedrock-runtime-fips"} and suffix in _runtime_dns_suffixes(region):
        return "runtime", region
    return None


def _validate_bedrock_region(region: str | None) -> None:
    if region is not None and _AWS_REGION.fullmatch(region) is None:
        raise OpenAIError("The Bedrock AWS `region` is invalid. Use a standard AWS region such as `us-east-1`.")


def _validate_canonical_bedrock_endpoint(
    base_url: httpx2.URL, *, endpoint: BedrockEndpoint, region: str | None
) -> None:
    canonical_endpoint = _parse_bedrock_endpoint_hostname(base_url.host)
    if canonical_endpoint is None:
        return

    canonical_family, canonical_region = canonical_endpoint
    if base_url.scheme != "https":
        raise OpenAIError("Canonical Amazon Bedrock endpoints require HTTPS.")
    if canonical_family != endpoint:
        raise OpenAIError(
            f"The Bedrock {canonical_family} hostname does not match the selected `{endpoint}` endpoint. "
            f"Set `endpoint='{canonical_family}'` to use this hostname."
        )
    if region is not None and canonical_region != region:

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Pass a standard AWS region name, e.g. `region="us-east-1"`.
  2. Check and correct the AWS_REGION / AWS_DEFAULT_REGION environment variable.
  3. Strip whitespace and remove underscores/zone suffixes from the value.

Example fix

// before
provider = bedrock(region="us_east_1")

// after
provider = bedrock(region="us-east-1")
Defensive patterns

Strategy: validation

Validate before calling

import re
AWS_REGION = re.compile(r"^(us|eu|ap|ca|cn|sa|af|me|il)-[a-z]+-\d+$")
assert AWS_REGION.fullmatch(region or ""), f"invalid region: {region!r}"

Type guard

def is_valid_aws_region(r: str | None) -> bool:
    import re
    return r is not None and re.fullmatch(r"(us|eu|ap|ca|cn|sa|af|me|il)-[a-z]+-\d+", r) is not None

Try / catch

try:
    provider = bedrock(region=region)
except OpenAIError as e:
    if "region" in str(e):
        region = "us-east-1"
        provider = bedrock(region=region)
    else:
        raise

Prevention

When it happens

Trigger: Calling `bedrock(region=...)` or `configure()` with a malformed region such as `us_east_1`, `USEast1`, `us-east-1a` (an AZ id), or an empty string; or setting AWS_REGION to such a value.

Common situations: Copy-pasting an availability-zone id instead of a region; using underscores; trailing whitespace; environment variables from a custom AWS setup with arbitrary values.

Related errors


AI-assisted analysis of openai/openai-python@9917c6e28e (2026-08-28). Data as JSON: /api/errors/79cb2fd3f197515b. Report an issue: GitHub.