MCPcopy
hub / github.com/openai/openai-python / bedrock

Function bedrock

src/openai/providers/bedrock.py:308–408  ·  view source on GitHub ↗

Configure the standard OpenAI client for Amazon Bedrock Mantle.

(
    *,
    region: str | None = None,
    base_url: str | httpx.URL | None | NotGiven = NOT_GIVEN,
    api_key: str | None | NotGiven = NOT_GIVEN,
    token_provider: BedrockTokenProvider | None = None,
    access_key_id: str | None = None,
    secret_access_key: str | None = None,
    session_token: str | None = None,
    profile: str | None = None,
    credential_provider: AwsCredentialsProvider | None = None,
)

Source from the content-addressed store, hash-verified

306
307
308def bedrock(
309 *,
310 region: str | None = None,
311 base_url: str | httpx.URL | None | NotGiven = NOT_GIVEN,
312 api_key: str | None | NotGiven = NOT_GIVEN,
313 token_provider: BedrockTokenProvider | None = None,
314 access_key_id: str | None = None,
315 secret_access_key: str | None = None,
316 session_token: str | None = None,
317 profile: str | None = None,
318 credential_provider: AwsCredentialsProvider | None = None,
319) -> _Provider:
320 """Configure the standard OpenAI client for Amazon Bedrock Mantle."""
321
322 normalized_region = _normalize_optional_string(region)
323 if region is not None and normalized_region is None:
324 raise OpenAIError("The Bedrock AWS `region` must not be empty.")
325
326 region_source: Literal["explicit", "environment"] | None = None
327 if normalized_region is not None:
328 region_source = "explicit"
329 else:
330 normalized_region = _normalize_optional_string(
331 os.environ.get("AWS_REGION") or os.environ.get("AWS_DEFAULT_REGION")
332 )
333 if normalized_region is not None:
334 region_source = "environment"
335
336 configured_base_url: httpx.URL | None
337 if isinstance(base_url, NotGiven):
338 environment_base_url = _normalize_optional_string(os.environ.get("AWS_BEDROCK_BASE_URL"))
339 configured_base_url = _normalize_base_url(environment_base_url) if environment_base_url else None
340 elif base_url is None:
341 configured_base_url = None
342 else:
343 if isinstance(base_url, str) and not base_url.strip():
344 raise OpenAIError("The Bedrock `base_url` must not be empty.")
345 configured_base_url = _normalize_base_url(base_url)
346
347 normalized_profile = _normalize_optional_string(profile)
348 if profile is not None and normalized_profile is None:
349 raise OpenAIError("The Bedrock AWS `profile` must not be empty.")
350
351 if (access_key_id is None) != (secret_access_key is None) or (session_token is not None and access_key_id is None):
352 raise OpenAIError(
353 "Static AWS credentials require both `access_key_id` and `secret_access_key`. "
354 "A `session_token` may only be used with both."
355 )
356 if access_key_id is not None and (not access_key_id.strip() or not cast(str, secret_access_key).strip()):
357 raise OpenAIError("Static AWS credentials require non-empty `access_key_id` and `secret_access_key` values.")
358 if session_token is not None and not session_token.strip():
359 raise OpenAIError("A static AWS `session_token` must not be empty when provided.")
360
361 explicit_api_key = not isinstance(api_key, NotGiven) and api_key is not None
362 if explicit_api_key and (not isinstance(api_key, str) or not api_key.strip()):
363 raise OpenAIError("The Bedrock bearer credential must not be empty.")
364 if explicit_api_key and token_provider is not None:
365 raise OpenAIError("The `api_key` and `token_provider` options are mutually exclusive. Configure only one.")

Calls 6

OpenAIErrorClass · 0.85
_normalize_base_urlFunction · 0.85
_create_providerFunction · 0.85
getMethod · 0.45