mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: improve .env handling and observability samples (#4032)
* Python: improve .env precedence and observability samples - Switch load_settings to explicit precedence: overrides -> explicit .env -> environment -> defaults\n- Raise when env_file_path is provided but missing\n- Update settings docs and tests for new behavior\n- Refresh observability samples and README guidance for env loading options\n\nCloses #3864\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fixed some imports * Fix load_settings CI regressions Allow explicit env_file_path values that exist but are not regular files (for example /dev/null) by checking path existence before dotenv parsing, and restore a dict accumulator with typed return cast to satisfy mypy. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Avoid implicit dotenv in observability Only load dotenv in observability helpers when env_file_path is explicitly provided, and remove test os.devnull workarounds that are no longer necessary. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
f900febb6f
commit
534e5f5bf7
+3
-3
@@ -118,9 +118,9 @@ FoundryLocalChatOptionsT = TypeVar(
|
||||
class FoundryLocalSettings(TypedDict, total=False):
|
||||
"""Foundry local model settings.
|
||||
|
||||
The settings are first loaded from environment variables with the prefix 'FOUNDRY_LOCAL_'.
|
||||
If the environment variables are not found, the settings can be loaded from a .env file
|
||||
with the encoding 'utf-8'.
|
||||
Settings are resolved in this order: explicit keyword arguments, values from an
|
||||
explicitly provided .env file, then environment variables with the prefix
|
||||
'FOUNDRY_LOCAL_'.
|
||||
|
||||
Keys:
|
||||
model_id: The name of the model deployment to use.
|
||||
|
||||
@@ -15,7 +15,7 @@ from agent_framework_foundry_local._foundry_local_client import FoundryLocalSett
|
||||
|
||||
def test_foundry_local_settings_init_from_env(foundry_local_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test FoundryLocalSettings initialization from environment variables."""
|
||||
settings = load_settings(FoundryLocalSettings, env_prefix="FOUNDRY_LOCAL_", env_file_path="test.env")
|
||||
settings = load_settings(FoundryLocalSettings, env_prefix="FOUNDRY_LOCAL_")
|
||||
|
||||
assert settings["model_id"] == foundry_local_unit_test_env["FOUNDRY_LOCAL_MODEL_ID"]
|
||||
|
||||
@@ -26,7 +26,6 @@ def test_foundry_local_settings_init_with_explicit_values() -> None:
|
||||
FoundryLocalSettings,
|
||||
env_prefix="FOUNDRY_LOCAL_",
|
||||
model_id="custom-model-id",
|
||||
env_file_path="test.env",
|
||||
)
|
||||
|
||||
assert settings["model_id"] == "custom-model-id"
|
||||
@@ -40,14 +39,13 @@ def test_foundry_local_settings_missing_model_id(foundry_local_unit_test_env: di
|
||||
FoundryLocalSettings,
|
||||
env_prefix="FOUNDRY_LOCAL_",
|
||||
required_fields=["model_id"],
|
||||
env_file_path="test.env",
|
||||
)
|
||||
|
||||
|
||||
def test_foundry_local_settings_explicit_overrides_env(foundry_local_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test that explicit values override environment variables."""
|
||||
settings = load_settings(
|
||||
FoundryLocalSettings, env_prefix="FOUNDRY_LOCAL_", model_id="override-model-id", env_file_path="test.env"
|
||||
FoundryLocalSettings, env_prefix="FOUNDRY_LOCAL_", model_id="override-model-id"
|
||||
)
|
||||
|
||||
assert settings["model_id"] == "override-model-id"
|
||||
@@ -63,7 +61,7 @@ def test_foundry_local_client_init(mock_foundry_local_manager: MagicMock) -> Non
|
||||
"agent_framework_foundry_local._foundry_local_client.FoundryLocalManager",
|
||||
return_value=mock_foundry_local_manager,
|
||||
):
|
||||
client = FoundryLocalClient(model_id="test-model-id", env_file_path="test.env")
|
||||
client = FoundryLocalClient(model_id="test-model-id")
|
||||
|
||||
assert client.model_id == "test-model-id"
|
||||
assert client.manager is mock_foundry_local_manager
|
||||
@@ -76,7 +74,7 @@ def test_foundry_local_client_init_with_bootstrap_false(mock_foundry_local_manag
|
||||
"agent_framework_foundry_local._foundry_local_client.FoundryLocalManager",
|
||||
return_value=mock_foundry_local_manager,
|
||||
) as mock_manager_class:
|
||||
FoundryLocalClient(model_id="test-model-id", bootstrap=False, env_file_path="test.env")
|
||||
FoundryLocalClient(model_id="test-model-id", bootstrap=False)
|
||||
|
||||
mock_manager_class.assert_called_once_with(
|
||||
bootstrap=False,
|
||||
@@ -90,7 +88,7 @@ def test_foundry_local_client_init_with_timeout(mock_foundry_local_manager: Magi
|
||||
"agent_framework_foundry_local._foundry_local_client.FoundryLocalManager",
|
||||
return_value=mock_foundry_local_manager,
|
||||
) as mock_manager_class:
|
||||
FoundryLocalClient(model_id="test-model-id", timeout=60.0, env_file_path="test.env")
|
||||
FoundryLocalClient(model_id="test-model-id", timeout=60.0)
|
||||
|
||||
mock_manager_class.assert_called_once_with(
|
||||
bootstrap=True,
|
||||
@@ -109,7 +107,7 @@ def test_foundry_local_client_init_model_not_found(mock_foundry_local_manager: M
|
||||
),
|
||||
pytest.raises(ServiceInitializationError, match="not found in Foundry Local"),
|
||||
):
|
||||
FoundryLocalClient(model_id="unknown-model", env_file_path="test.env")
|
||||
FoundryLocalClient(model_id="unknown-model")
|
||||
|
||||
|
||||
def test_foundry_local_client_uses_model_info_id(mock_foundry_local_manager: MagicMock) -> None:
|
||||
@@ -122,7 +120,7 @@ def test_foundry_local_client_uses_model_info_id(mock_foundry_local_manager: Mag
|
||||
"agent_framework_foundry_local._foundry_local_client.FoundryLocalManager",
|
||||
return_value=mock_foundry_local_manager,
|
||||
):
|
||||
client = FoundryLocalClient(model_id="model-alias", env_file_path="test.env")
|
||||
client = FoundryLocalClient(model_id="model-alias")
|
||||
|
||||
assert client.model_id == "resolved-model-id"
|
||||
|
||||
@@ -135,7 +133,7 @@ def test_foundry_local_client_init_from_env(
|
||||
"agent_framework_foundry_local._foundry_local_client.FoundryLocalManager",
|
||||
return_value=mock_foundry_local_manager,
|
||||
):
|
||||
client = FoundryLocalClient(env_file_path="test.env")
|
||||
client = FoundryLocalClient()
|
||||
|
||||
assert client.model_id == foundry_local_unit_test_env["FOUNDRY_LOCAL_MODEL_ID"]
|
||||
|
||||
@@ -148,7 +146,7 @@ def test_foundry_local_client_init_with_device(mock_foundry_local_manager: Magic
|
||||
"agent_framework_foundry_local._foundry_local_client.FoundryLocalManager",
|
||||
return_value=mock_foundry_local_manager,
|
||||
):
|
||||
FoundryLocalClient(model_id="test-model-id", device=DeviceType.CPU, env_file_path="test.env")
|
||||
FoundryLocalClient(model_id="test-model-id", device=DeviceType.CPU)
|
||||
|
||||
mock_foundry_local_manager.get_model_info.assert_called_once_with(
|
||||
alias_or_model_id="test-model-id",
|
||||
@@ -177,7 +175,7 @@ def test_foundry_local_client_init_model_not_found_with_device(mock_foundry_loca
|
||||
),
|
||||
pytest.raises(ServiceInitializationError, match="unknown-model:GPU.*not found"),
|
||||
):
|
||||
FoundryLocalClient(model_id="unknown-model", device=DeviceType.GPU, env_file_path="test.env")
|
||||
FoundryLocalClient(model_id="unknown-model", device=DeviceType.GPU)
|
||||
|
||||
|
||||
def test_foundry_local_client_init_with_prepare_model_false(mock_foundry_local_manager: MagicMock) -> None:
|
||||
@@ -186,7 +184,7 @@ def test_foundry_local_client_init_with_prepare_model_false(mock_foundry_local_m
|
||||
"agent_framework_foundry_local._foundry_local_client.FoundryLocalManager",
|
||||
return_value=mock_foundry_local_manager,
|
||||
):
|
||||
FoundryLocalClient(model_id="test-model-id", prepare_model=False, env_file_path="test.env")
|
||||
FoundryLocalClient(model_id="test-model-id", prepare_model=False)
|
||||
|
||||
mock_foundry_local_manager.download_model.assert_not_called()
|
||||
mock_foundry_local_manager.load_model.assert_not_called()
|
||||
@@ -198,7 +196,7 @@ def test_foundry_local_client_init_calls_download_and_load(mock_foundry_local_ma
|
||||
"agent_framework_foundry_local._foundry_local_client.FoundryLocalManager",
|
||||
return_value=mock_foundry_local_manager,
|
||||
):
|
||||
FoundryLocalClient(model_id="test-model-id", env_file_path="test.env")
|
||||
FoundryLocalClient(model_id="test-model-id")
|
||||
|
||||
mock_foundry_local_manager.download_model.assert_called_once_with(
|
||||
alias_or_model_id="test-model-id",
|
||||
|
||||
Reference in New Issue
Block a user