{"record":{"id":"3f9c726fe28a1726","repo":"openai/openai-python","slug":"an-async-bedrock-token-provider-requires-asyncope","errorCode":null,"errorMessage":"An async Bedrock token provider requires `AsyncOpenAI`.","messagePattern":"An async Bedrock token provider requires `AsyncOpenAI`\\.","errorType":"error_code","errorClass":"OpenAIError","httpStatus":null,"severity":"error","filePath":"src/openai/providers/bedrock.py","lineNumber":165,"sourceCode":"        _assert_provider_owns_authorization(request)\n        if not _same_origin(request.url, self._base_url):\n            raise OpenAIError(\n                \"Refusing to authenticate a Bedrock request for an origin other than the configured provider URL.\"\n            )\n\n    def _resolve_token(self) -> str:\n        try:\n            token = cast(object, self._token_provider())\n        except OpenAIError:\n            raise\n        except Exception as exc:\n            raise OpenAIError(\"Failed to resolve a bearer credential for Bedrock.\") from exc\n\n        if inspect.isawaitable(token):\n            close = getattr(token, \"close\", None)\n            if callable(close):\n                close()\n            raise OpenAIError(\"An async Bedrock token provider requires `AsyncOpenAI`.\")\n        if not isinstance(token, str) or not token.strip():\n            raise OpenAIError(\"The Bedrock bearer credential provider must return a non-empty string.\")\n        return token\n\n    async def _resolve_token_async(self) -> str:\n        try:\n            token = cast(object, self._token_provider())\n            if inspect.isawaitable(token):\n                token = await token\n        except OpenAIError:\n            raise\n        except Exception as exc:\n            raise OpenAIError(\"Failed to resolve a bearer credential for Bedrock.\") from exc\n\n        if not isinstance(token, str) or not token.strip():\n            raise OpenAIError(\"The Bedrock bearer credential provider must return a non-empty string.\")\n        return token\n","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/openai/openai-python/blob/9917c6e28e66e90e1227b3d223c06a8c5441515a/src/openai/providers/bedrock.py#L147-L183","documentation":"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.","triggerScenarios":"Using a sync OpenAI client with a Bedrock provider whose bearer token provider is `async def token(): ...`.","commonSituations":"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.","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)."],"exampleFix":"# before (sync client)\nasync def token(): return await fetch_token()\nclient = OpenAI(provider=bedrock(bearer=token))\n\n# after\nasync def token(): return await fetch_token()\nclient = AsyncOpenAI(provider=bedrock(bearer=token))","handlingStrategy":"type-guard","validationCode":"import inspect\nif inspect.iscoroutinefunction(token_provider):\n    assert using_async_client, \"async token provider requires AsyncOpenAI\"","typeGuard":"import inspect\n\ndef provider_is_sync(fn) -> bool:\n    return not inspect.iscoroutinefunction(fn) and not inspect.isawaitable(fn)","tryCatchPattern":"try:\n    client = OpenAI(provider=bedrock(bearer=token_fn))\nexcept OpenAIError as e:\n    if \"AsyncOpenAI\" in str(e):\n        client = AsyncOpenAI(provider=bedrock(bearer=token_fn))\n    else:\n        raise","preventionTips":["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."],"tags":["bedrock","aws","bearer","async","sync-mismatch"],"backgroundTag":"sync-async-mismatch","analyzedSha":"9917c6e28e66e90e1227b3d223c06a8c5441515a","analyzedAt":"2026-08-28T11:46:34.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}