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
- Use AsyncOpenAI with this provider, keeping the async token provider.
- 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
- Pair async token providers exclusively with AsyncOpenAI.
- Keep separate sync/async provider factory functions in shared code.
- Test both client types when writing token providers.
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
- Refusing to authenticate a Bedrock request for an origin oth
- Failed to resolve a bearer credential for Bedrock.
- The Bedrock bearer credential provider must return a non-emp
- Could not find credentials for Bedrock. Set `AWS_BEARER_TOKE
- Pass refreshable Bedrock credentials via `bedrock_token_prov
AI-assisted analysis of openai/openai-python@9917c6e28e (2026-08-28).
Data as JSON: /api/errors/3f9c726fe28a1726.
Report an issue: GitHub.