mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-12-04 02:03:44 +00:00
fix: refactor auth and improve error handling for Bedrock provider
Refactor to use auth_credential for consistent credential management and improve error handling with defensive checks. Changes: - Use auth_credential instead of api_key for better credential handling - Simplify model availability check to accept all pre-registered models - Guard metrics collection when usage data is missing in responses - Add debug logging for better troubleshooting of API issues - Update unit tests for auth_credential refactoring
This commit is contained in:
parent
dc27537cce
commit
454aeaaf3e
6 changed files with 51 additions and 40 deletions
|
|
@ -19,7 +19,7 @@ def test_adapter_initialization():
|
|||
config = BedrockConfig(api_key="test-key", region_name="us-east-1")
|
||||
adapter = BedrockInferenceAdapter(config=config)
|
||||
|
||||
assert adapter.config.api_key == "test-key"
|
||||
assert adapter.config.auth_credential.get_secret_value() == "test-key"
|
||||
assert adapter.config.region_name == "us-east-1"
|
||||
|
||||
|
||||
|
|
@ -28,15 +28,15 @@ def test_client_url_construction():
|
|||
adapter = BedrockInferenceAdapter(config=config)
|
||||
|
||||
assert adapter.get_base_url() == "https://bedrock-runtime.us-west-2.amazonaws.com/openai/v1"
|
||||
assert adapter.get_api_key() == "test-key"
|
||||
|
||||
|
||||
def test_api_key_from_config():
|
||||
"""Test API key is read from config"""
|
||||
"""Test API key is stored as SecretStr in auth_credential"""
|
||||
config = BedrockConfig(api_key="config-key", region_name="us-east-1")
|
||||
adapter = BedrockInferenceAdapter(config=config)
|
||||
|
||||
assert adapter.get_api_key() == "config-key"
|
||||
# API key is stored in auth_credential field (SecretStr)
|
||||
assert adapter.config.auth_credential.get_secret_value() == "config-key"
|
||||
|
||||
|
||||
def test_api_key_from_header_overrides_config():
|
||||
|
|
|
|||
|
|
@ -12,23 +12,21 @@ def test_bedrock_config_defaults_no_env(monkeypatch):
|
|||
monkeypatch.delenv("AWS_BEDROCK_API_KEY", raising=False)
|
||||
monkeypatch.delenv("AWS_DEFAULT_REGION", raising=False)
|
||||
config = BedrockConfig()
|
||||
assert config.api_key is None
|
||||
assert config.auth_credential is None
|
||||
assert config.region_name == "us-east-2"
|
||||
|
||||
|
||||
def test_bedrock_config_defaults_with_env(monkeypatch):
|
||||
"""Test BedrockConfig reads from environment variables"""
|
||||
monkeypatch.setenv("AWS_BEDROCK_API_KEY", "env-key")
|
||||
def test_bedrock_config_reads_from_env(monkeypatch):
|
||||
"""Test BedrockConfig field initialization reads from environment variables"""
|
||||
monkeypatch.setenv("AWS_DEFAULT_REGION", "eu-west-1")
|
||||
config = BedrockConfig()
|
||||
assert config.api_key == "env-key"
|
||||
assert config.region_name == "eu-west-1"
|
||||
|
||||
|
||||
def test_bedrock_config_with_values():
|
||||
"""Test BedrockConfig accepts explicit values"""
|
||||
"""Test BedrockConfig accepts explicit values via alias"""
|
||||
config = BedrockConfig(api_key="test-key", region_name="us-west-2")
|
||||
assert config.api_key == "test-key"
|
||||
assert config.auth_credential.get_secret_value() == "test-key"
|
||||
assert config.region_name == "us-west-2"
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue