openai/openai-python · error · OpenAIError

An async Bedrock token provider requires `AsyncOpenAI`.

Error message

An async Bedrock token provider requires `AsyncOpenAI`.

What it means

In the synchronous prepare_request path, the bearer token provider returned an awaitable (an async def or coroutine). The sync client cannot await it, so the provider closes the awaitable and raises, telling you an async-capable client is required for async token providers.

Source

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

        _assert_provider_owns_authorization(request)
        if not _same_origin(request.url, self._base_url):
            raise OpenAIError(
                "Refusing to authenticate a Bedrock request for an origin other than the configured provider URL."
            )

    def _resolve_token(self) -> str:
        try:
            token = cast(object, self._token_provider())
        except OpenAIError:
            raise
        except Exception as exc:
            raise OpenAIError("Failed to resolve a bearer credential for Bedrock.") from exc

        if inspect.isawaitable(token):
            close = getattr(token, "close", None)
            if callable(close):
                close()
            raise OpenAIError("An async Bedrock token provider requires `AsyncOpenAI`.")
        if not isinstance(token, str) or not token.strip():
            raise OpenAIError("The Bedrock bearer credential provider must return a non-empty string.")
        return token

    async def _resolve_token_async(self) -> str:
        try:
            token = cast(object, self._token_provider())
            if inspect.isawaitable(token):
                token = await token
        except OpenAIError:
            raise
        except Exception as exc:
            raise OpenAIError("Failed to resolve a bearer credential for Bedrock.") from exc

        if not isinstance(token, str) or not token.strip():
            raise OpenAIError("The Bedrock bearer credential provider must return a non-empty string.")
        return token

View on GitHub (pinned to 9917c6e28e)

Solutions

  1. Use AsyncOpenAI with this provider, keeping the async token provider.
  2. Or change the token provider to a sync function (e.g. run the fetch with asyncio.run or use a sync HTTP call).

Example fix

# before (sync client)
async def token(): return await fetch_token()
client = OpenAI(provider=bedrock(bearer=token))

# after
async def token(): return await fetch_token()
client = AsyncOpenAI(provider=bedrock(bearer=token))
Defensive patterns

Strategy: type-guard

Validate before calling

import inspect
if inspect.iscoroutinefunction(token_provider):
    assert using_async_client, "async token provider requires AsyncOpenAI"

Type guard

import inspect

def provider_is_sync(fn) -> bool:
    return not inspect.iscoroutinefunction(fn) and not inspect.isawaitable(fn)

Try / catch

try:
    client = OpenAI(provider=bedrock(bearer=token_fn))
except OpenAIError as e:
    if "AsyncOpenAI" in str(e):
        client = AsyncOpenAI(provider=bedrock(bearer=token_fn))
    else:
        raise

Prevention

When it happens

Trigger: Using a sync OpenAI client with a Bedrock provider whose bearer token provider is `async def token(): ...`.

Common situations: Writing an async token fetcher (http call to a vault/STS) and reusing it with the sync client; shared codebase mixing sync and async entry points.

Related errors


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