openai/openai-python · error · OpenAIError

A static AWS `session_token` must not be empty when provided

Error message

A static AWS `session_token` must not be empty when provided.

What it means

A `session_token` was supplied alongside static credentials but it is empty or whitespace-only. Temporary AWS credentials require a real session token, so a blank one is rejected up front.

Source

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

        _parse_bedrock_endpoint_hostname(configured_base_url.host) if configured_base_url is not None else None
    )
    resolved_endpoint: BedrockEndpoint = endpoint or (
        canonical_endpoint[0] if canonical_endpoint is not None else "mantle"
    )

    normalized_profile = _normalize_optional_string(profile)
    if profile is not None and normalized_profile is None:
        raise OpenAIError("The Bedrock AWS `profile` must not be empty.")

    if (access_key_id is None) != (secret_access_key is None) or (session_token is not None and access_key_id is None):
        raise OpenAIError(
            "Static AWS credentials require both `access_key_id` and `secret_access_key`. "
            "A `session_token` may only be used with both."
        )
    if access_key_id is not None and (not access_key_id.strip() or not cast(str, secret_access_key).strip()):
        raise OpenAIError("Static AWS credentials require non-empty `access_key_id` and `secret_access_key` values.")
    if session_token is not None and not session_token.strip():
        raise OpenAIError("A static AWS `session_token` must not be empty when provided.")

    explicit_api_key = not isinstance(api_key, NotGiven) and api_key is not None
    if explicit_api_key and (not isinstance(api_key, str) or not api_key.strip()):
        raise OpenAIError("The Bedrock bearer credential must not be empty.")
    if explicit_api_key and token_provider is not None:
        raise OpenAIError("The `api_key` and `token_provider` options are mutually exclusive. Configure only one.")

    explicit_bearer = explicit_api_key or token_provider is not None
    aws_modes = sum(
        (
            access_key_id is not None,
            normalized_profile is not None,
            credential_provider is not None,
        )
    )
    if aws_modes > 1:
        raise OpenAIError(
            "Bedrock authentication is ambiguous. Configure exactly one explicit AWS mode: static credentials, "

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Omit session_token entirely if using long-lived IAM keys
  2. Supply the full, non-empty session token when using temporary credentials

Example fix

# before
bedrock(ak, sk, session_token=os.getenv('AWS_SESSION_TOKEN', ''))
# after
bedrock(ak, sk, session_token=os.environ.get('AWS_SESSION_TOKEN') or None)
Defensive patterns

Strategy: validation

Validate before calling

session_token = os.environ.get('AWS_SESSION_TOKEN') or None
bedrock(access_key_id=ak, secret_access_key=sk, session_token=session_token)

Type guard

def token_ok(st: str | None) -> bool:
    return st is None or bool(st.strip())

Prevention

When it happens

Trigger: bedrock(access_key_id=..., secret_access_key=..., session_token='') or session_token=' '.

Common situations: Using long-lived keys but forwarding an AWS_SESSION_TOKEN env var that is blank, or truncating the token during copy-paste (session tokens are long).

Related errors


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