Skip to content

Commit ac26635

Browse files
1aifanaticclaude
andcommitted
samples: guard every toolbox model import behind the SDK check
MCPToolboxTool and ToolboxTool are missing from azure-ai-projects 2.2.0, which the foundry package still allows, so importing them outside the guard raised a raw ImportError instead of the upgrade message. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8025ba7 commit ac26635

2 files changed

Lines changed: 77 additions & 2 deletions

File tree

‎python/samples/02-agents/providers/foundry/foundry_chat_client_with_toolbox.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ def create_sample_toolbox(name: str) -> str:
4545
created version identifier without deleting existing versions.
4646
"""
4747
from azure.ai.projects import AIProjectClient
48-
from azure.ai.projects.models import MCPToolboxTool, ToolboxTool
4948

5049
try:
51-
from azure.ai.projects.models import ToolSearchToolboxTool
50+
from azure.ai.projects.models import MCPToolboxTool, ToolboxTool, ToolSearchToolboxTool
5251
except ImportError as exc:
5352
raise ImportError(
5453
"This sample requires azure-ai-projects>=2.4.0 for toolbox tool search. "
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
"""Tests for the azure-ai-projects version guard in the Foundry toolbox sample."""
4+
5+
import importlib.util
6+
import sys
7+
from pathlib import Path
8+
from types import ModuleType
9+
10+
import pytest
11+
12+
_SAMPLE_PATH = (
13+
Path(__file__).parents[3]
14+
/ "samples"
15+
/ "02-agents"
16+
/ "providers"
17+
/ "foundry"
18+
/ "foundry_chat_client_with_toolbox.py"
19+
)
20+
21+
22+
def _load_sample_module() -> ModuleType:
23+
spec = importlib.util.spec_from_file_location("foundry_chat_client_with_toolbox", _SAMPLE_PATH)
24+
assert spec is not None
25+
assert spec.loader is not None
26+
module = importlib.util.module_from_spec(spec)
27+
spec.loader.exec_module(module)
28+
return module
29+
30+
31+
sample_module = _load_sample_module()
32+
33+
34+
class _StopBeforeNetwork(Exception):
35+
pass
36+
37+
38+
def _install_models_surface(monkeypatch: pytest.MonkeyPatch, exported: tuple[str, ...]) -> None:
39+
"""Replace azure.ai.projects.models with a module exposing only the given toolbox names."""
40+
models = ModuleType("azure.ai.projects.models")
41+
for name in exported:
42+
setattr(models, name, type(name, (), {}))
43+
monkeypatch.setitem(sys.modules, "azure.ai.projects.models", models)
44+
45+
46+
@pytest.mark.parametrize(
47+
"exported",
48+
[
49+
pytest.param((), id="azure-ai-projects-2.2"),
50+
pytest.param(("MCPToolboxTool", "ToolboxTool"), id="azure-ai-projects-2.3"),
51+
],
52+
)
53+
def test_create_sample_toolbox_reports_upgrade_for_older_sdk(
54+
monkeypatch: pytest.MonkeyPatch, exported: tuple[str, ...]
55+
) -> None:
56+
_install_models_surface(monkeypatch, exported)
57+
58+
def fail_if_called() -> None:
59+
raise AssertionError("The version check must run before any credential is created.")
60+
61+
monkeypatch.setattr(sample_module, "AzureCliCredential", fail_if_called)
62+
63+
with pytest.raises(ImportError, match=r"azure-ai-projects>=2\.4\.0"):
64+
sample_module.create_sample_toolbox("toolbox")
65+
66+
67+
def test_create_sample_toolbox_accepts_tool_search_sdk(monkeypatch: pytest.MonkeyPatch) -> None:
68+
_install_models_surface(monkeypatch, ("MCPToolboxTool", "ToolboxTool", "ToolSearchToolboxTool"))
69+
70+
def stop_before_network() -> None:
71+
raise _StopBeforeNetwork
72+
73+
monkeypatch.setattr(sample_module, "AzureCliCredential", stop_before_network)
74+
75+
with pytest.raises(_StopBeforeNetwork):
76+
sample_module.create_sample_toolbox("toolbox")

0 commit comments

Comments
 (0)