mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: [BREAKING] Redesign Python exception hierarchy (#4082)
* [BREAKING] Redesign Python exception hierarchy Replace the flat ServiceException family with domain-scoped branches: - AgentException (with InvalidAuth, InvalidRequest, InvalidResponse, ContentFilter) - ChatClientException (same consistent suberrors) - IntegrationException (same + InitializationError) - WorkflowException (Runner, Convergence, Checkpoint, Validation, Action, Declarative) - ContentError (AdditionItemMismatch) - ToolException / ToolExecutionException (unchanged) - MiddlewareException / MiddlewareTermination (unchanged) Key changes: - All Service* exceptions removed (ServiceException, ServiceInitializationError, etc.) - AgentExecutionException split into AgentInvalidRequest/ResponseException - AgentInvocationError removed, split into AgentInvalidRequest/ResponseException - Workflow exceptions moved from _workflows/_exceptions.py into main exceptions.py - _workflows/__init__.py emptied; main __init__.py imports directly from submodules - Purview exceptions re-parented under IntegrationException hierarchy - Init validation errors use built-in ValueError/TypeError instead of custom exceptions - CODING_STANDARD.md updated with hierarchy design and rationale Fixes microsoft/agent-framework#3410 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Clarify ToolException vs ToolExecutionException docstrings ToolException: base class for all tool-related exceptions (preconditions, connection/init failures). ToolExecutionException: runtime call failures (tool call failed, reconnect failed, MCP errors). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix remaining stale imports from agent_framework._workflows - azurefunctions: _context.py, _app.py, _serialization.py, test_func_utils.py used 'from agent_framework._workflows import X' which broke after emptying _workflows/__init__.py; changed to direct submodule imports - azure-ai-search: test still referenced ServiceInitializationError; updated to ValueError to match production code 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
7f606a2e3a
commit
5ee06853a1
@@ -18,7 +18,6 @@ from agent_framework._mcp import MCPTool
|
||||
from agent_framework._settings import load_settings
|
||||
from agent_framework._tools import ToolTypes
|
||||
from agent_framework.azure._entra_id_authentication import AzureCredentialTypes
|
||||
from agent_framework.exceptions import ServiceInitializationError
|
||||
from azure.ai.agents.aio import AgentsClient
|
||||
from azure.ai.agents.models import Agent as AzureAgent
|
||||
from azure.ai.agents.models import ResponseFormatJsonSchema, ResponseFormatJsonSchemaType
|
||||
@@ -113,7 +112,7 @@ class AzureAIAgentsProvider(Generic[OptionsCoT]):
|
||||
env_file_encoding: Encoding of the .env file.
|
||||
|
||||
Raises:
|
||||
ServiceInitializationError: If required parameters are missing or invalid.
|
||||
ValueError: If required parameters are missing or invalid.
|
||||
"""
|
||||
self._settings = load_settings(
|
||||
AzureAISettings,
|
||||
@@ -130,12 +129,12 @@ class AzureAIAgentsProvider(Generic[OptionsCoT]):
|
||||
else:
|
||||
resolved_endpoint = self._settings.get("project_endpoint")
|
||||
if not resolved_endpoint:
|
||||
raise ServiceInitializationError(
|
||||
raise ValueError(
|
||||
"Azure AI project endpoint is required. Provide 'project_endpoint' parameter "
|
||||
"or set 'AZURE_AI_PROJECT_ENDPOINT' environment variable."
|
||||
)
|
||||
if not credential:
|
||||
raise ServiceInitializationError("Azure credential is required when agents_client is not provided.")
|
||||
raise ValueError("Azure credential is required when agents_client is not provided.")
|
||||
self._agents_client = AgentsClient(
|
||||
endpoint=resolved_endpoint,
|
||||
credential=credential, # type: ignore[arg-type]
|
||||
@@ -199,7 +198,7 @@ class AzureAIAgentsProvider(Generic[OptionsCoT]):
|
||||
Agent: A Agent instance configured with the created agent.
|
||||
|
||||
Raises:
|
||||
ServiceInitializationError: If model deployment name is not available.
|
||||
ValueError: If model deployment name is not available.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
@@ -212,7 +211,7 @@ class AzureAIAgentsProvider(Generic[OptionsCoT]):
|
||||
"""
|
||||
resolved_model = model or self._settings.get("model_deployment_name")
|
||||
if not resolved_model:
|
||||
raise ServiceInitializationError(
|
||||
raise ValueError(
|
||||
"Model deployment name is required. Provide 'model' parameter "
|
||||
"or set 'AZURE_AI_MODEL_DEPLOYMENT_NAME' environment variable."
|
||||
)
|
||||
@@ -290,7 +289,7 @@ class AzureAIAgentsProvider(Generic[OptionsCoT]):
|
||||
Agent: A Agent instance configured with the retrieved agent.
|
||||
|
||||
Raises:
|
||||
ServiceInitializationError: If required function tools are not provided.
|
||||
ValueError: If required function tools are not provided.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
@@ -340,7 +339,7 @@ class AzureAIAgentsProvider(Generic[OptionsCoT]):
|
||||
Agent: A Agent instance configured with the agent.
|
||||
|
||||
Raises:
|
||||
ServiceInitializationError: If required function tools are not provided.
|
||||
ValueError: If required function tools are not provided.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
@@ -449,7 +448,7 @@ class AzureAIAgentsProvider(Generic[OptionsCoT]):
|
||||
"""Validate that required function tools are provided.
|
||||
|
||||
Raises:
|
||||
ServiceInitializationError: If agent has function tools but user
|
||||
ValueError: If agent has function tools but user
|
||||
didn't provide implementations.
|
||||
"""
|
||||
if not agent_tools:
|
||||
@@ -483,7 +482,7 @@ class AzureAIAgentsProvider(Generic[OptionsCoT]):
|
||||
# Check for missing implementations
|
||||
missing = function_tool_names - provided_names
|
||||
if missing:
|
||||
raise ServiceInitializationError(
|
||||
raise ValueError(
|
||||
f"Agent has function tools that require implementations: {missing}. "
|
||||
"Provide these functions via the 'tools' parameter."
|
||||
)
|
||||
|
||||
@@ -36,7 +36,10 @@ from agent_framework import (
|
||||
from agent_framework._settings import load_settings
|
||||
from agent_framework._tools import ToolTypes
|
||||
from agent_framework.azure._entra_id_authentication import AzureCredentialTypes
|
||||
from agent_framework.exceptions import ServiceInitializationError, ServiceInvalidRequestError, ServiceResponseException
|
||||
from agent_framework.exceptions import (
|
||||
ChatClientException,
|
||||
ChatClientInvalidRequestException,
|
||||
)
|
||||
from agent_framework.observability import ChatTelemetryLayer
|
||||
from azure.ai.agents.aio import AgentsClient
|
||||
from azure.ai.agents.models import (
|
||||
@@ -498,20 +501,20 @@ class AzureAIAgentClient(
|
||||
if agents_client is None:
|
||||
resolved_endpoint = azure_ai_settings.get("project_endpoint")
|
||||
if not resolved_endpoint:
|
||||
raise ServiceInitializationError(
|
||||
raise ValueError(
|
||||
"Azure AI project endpoint is required. Set via 'project_endpoint' parameter "
|
||||
"or 'AZURE_AI_PROJECT_ENDPOINT' environment variable."
|
||||
)
|
||||
|
||||
if agent_id is None and not azure_ai_settings.get("model_deployment_name"):
|
||||
raise ServiceInitializationError(
|
||||
raise ValueError(
|
||||
"Azure AI model deployment name is required. Set via 'model_deployment_name' parameter "
|
||||
"or 'AZURE_AI_MODEL_DEPLOYMENT_NAME' environment variable."
|
||||
)
|
||||
|
||||
# Use provided credential
|
||||
if not credential:
|
||||
raise ServiceInitializationError("Azure credential is required when agents_client is not provided.")
|
||||
raise ValueError("Azure credential is required when agents_client is not provided.")
|
||||
agents_client = AgentsClient(
|
||||
endpoint=resolved_endpoint,
|
||||
credential=credential, # type: ignore[arg-type]
|
||||
@@ -606,7 +609,7 @@ class AzureAIAgentClient(
|
||||
# If no agent_id is provided, create a temporary agent
|
||||
if self.agent_id is None:
|
||||
if "model" not in run_options or not run_options["model"]:
|
||||
raise ServiceInitializationError(
|
||||
raise ValueError(
|
||||
"Model deployment name is required for agent creation, "
|
||||
"can also be passed to the get_response methods."
|
||||
)
|
||||
@@ -916,7 +919,7 @@ class AzureAIAgentClient(
|
||||
response_id=response_id,
|
||||
)
|
||||
case AgentStreamEvent.THREAD_RUN_FAILED:
|
||||
raise ServiceResponseException(event_data.last_error.message)
|
||||
raise ChatClientException(event_data.last_error.message)
|
||||
case _:
|
||||
yield ChatResponseUpdate(
|
||||
contents=[],
|
||||
@@ -1159,7 +1162,7 @@ class AzureAIAgentClient(
|
||||
# Runtime JSON schema dict - pass through as-is
|
||||
run_options["response_format"] = response_format
|
||||
else:
|
||||
raise ServiceInvalidRequestError(
|
||||
raise ChatClientInvalidRequestException(
|
||||
"response_format must be a Pydantic BaseModel class or a dict with runtime JSON schema."
|
||||
)
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ from agent_framework import (
|
||||
from agent_framework._settings import load_settings
|
||||
from agent_framework._tools import ToolTypes
|
||||
from agent_framework.azure._entra_id_authentication import AzureCredentialTypes
|
||||
from agent_framework.exceptions import ServiceInitializationError
|
||||
from agent_framework.observability import ChatTelemetryLayer
|
||||
from agent_framework.openai import OpenAIResponsesOptions
|
||||
from agent_framework.openai._responses_client import RawOpenAIResponsesClient
|
||||
@@ -188,14 +187,14 @@ class RawAzureAIClient(RawOpenAIResponsesClient[AzureAIClientOptionsT], Generic[
|
||||
if project_client is None:
|
||||
resolved_endpoint = azure_ai_settings.get("project_endpoint")
|
||||
if not resolved_endpoint:
|
||||
raise ServiceInitializationError(
|
||||
raise ValueError(
|
||||
"Azure AI project endpoint is required. Set via 'project_endpoint' parameter "
|
||||
"or 'AZURE_AI_PROJECT_ENDPOINT' environment variable."
|
||||
)
|
||||
|
||||
# Use provided credential
|
||||
if not credential:
|
||||
raise ServiceInitializationError("Azure credential is required when project_client is not provided.")
|
||||
raise ValueError("Azure credential is required when project_client is not provided.")
|
||||
project_client = AIProjectClient(
|
||||
endpoint=resolved_endpoint,
|
||||
credential=credential, # type: ignore[arg-type]
|
||||
@@ -345,7 +344,7 @@ class RawAzureAIClient(RawOpenAIResponsesClient[AzureAIClientOptionsT], Generic[
|
||||
"""
|
||||
# Agent name must be explicitly provided by the user.
|
||||
if self.agent_name is None:
|
||||
raise ServiceInitializationError(
|
||||
raise ValueError(
|
||||
"Agent name is required. Provide 'agent_name' when initializing AzureAIClient "
|
||||
"or 'name' when initializing Agent."
|
||||
)
|
||||
@@ -363,7 +362,7 @@ class RawAzureAIClient(RawOpenAIResponsesClient[AzureAIClientOptionsT], Generic[
|
||||
return {"name": self.agent_name, "version": self.agent_version, "type": "agent_reference"}
|
||||
|
||||
if "model" not in run_options or not run_options["model"]:
|
||||
raise ServiceInitializationError(
|
||||
raise ValueError(
|
||||
"Model deployment name is required for agent creation, "
|
||||
"can also be passed to the get_response methods."
|
||||
)
|
||||
|
||||
@@ -19,7 +19,6 @@ from agent_framework._mcp import MCPTool
|
||||
from agent_framework._settings import load_settings
|
||||
from agent_framework._tools import ToolTypes
|
||||
from agent_framework.azure._entra_id_authentication import AzureCredentialTypes
|
||||
from agent_framework.exceptions import ServiceInitializationError
|
||||
from azure.ai.projects.aio import AIProjectClient
|
||||
from azure.ai.projects.models import (
|
||||
AgentReference,
|
||||
@@ -123,7 +122,7 @@ class AzureAIProjectAgentProvider(Generic[OptionsCoT]):
|
||||
env_file_encoding: Encoding of the environment file.
|
||||
|
||||
Raises:
|
||||
ServiceInitializationError: If required parameters are missing or invalid.
|
||||
ValueError: If required parameters are missing or invalid.
|
||||
"""
|
||||
self._settings = load_settings(
|
||||
AzureAISettings,
|
||||
@@ -140,13 +139,13 @@ class AzureAIProjectAgentProvider(Generic[OptionsCoT]):
|
||||
if project_client is None:
|
||||
resolved_endpoint = self._settings.get("project_endpoint")
|
||||
if not resolved_endpoint:
|
||||
raise ServiceInitializationError(
|
||||
raise ValueError(
|
||||
"Azure AI project endpoint is required. Set via 'project_endpoint' parameter "
|
||||
"or 'AZURE_AI_PROJECT_ENDPOINT' environment variable."
|
||||
)
|
||||
|
||||
if not credential:
|
||||
raise ServiceInitializationError("Azure credential is required when project_client is not provided.")
|
||||
raise ValueError("Azure credential is required when project_client is not provided.")
|
||||
|
||||
project_client = AIProjectClient(
|
||||
endpoint=resolved_endpoint,
|
||||
@@ -186,12 +185,12 @@ class AzureAIProjectAgentProvider(Generic[OptionsCoT]):
|
||||
Agent: A Agent instance configured with the created agent.
|
||||
|
||||
Raises:
|
||||
ServiceInitializationError: If required parameters are missing.
|
||||
ValueError: If required parameters are missing.
|
||||
"""
|
||||
# Resolve model from parameter or environment variable
|
||||
resolved_model = model or self._settings.get("model_deployment_name")
|
||||
if not resolved_model:
|
||||
raise ServiceInitializationError(
|
||||
raise ValueError(
|
||||
"Model deployment name is required. Provide 'model' parameter "
|
||||
"or set 'AZURE_AI_MODEL_DEPLOYMENT_NAME' environment variable."
|
||||
)
|
||||
|
||||
@@ -10,7 +10,7 @@ from typing import Any, cast
|
||||
from agent_framework import (
|
||||
FunctionTool,
|
||||
)
|
||||
from agent_framework.exceptions import ServiceInvalidRequestError
|
||||
from agent_framework.exceptions import IntegrationInvalidRequestException
|
||||
from azure.ai.agents.models import (
|
||||
CodeInterpreterToolDefinition,
|
||||
ToolDefinition,
|
||||
@@ -125,7 +125,7 @@ def to_azure_ai_agent_tools(
|
||||
List of Azure AI V1 SDK tool definitions.
|
||||
|
||||
Raises:
|
||||
ServiceInitializationError: If tool configuration is invalid.
|
||||
ValueError: If tool configuration is invalid.
|
||||
"""
|
||||
if not tools:
|
||||
return []
|
||||
@@ -458,7 +458,7 @@ def create_text_format_config(
|
||||
if format_type == "text":
|
||||
return ResponseTextFormatConfigurationText()
|
||||
|
||||
raise ServiceInvalidRequestError("response_format must be a Pydantic model or mapping.")
|
||||
raise IntegrationInvalidRequestException("response_format must be a Pydantic model or mapping.")
|
||||
|
||||
|
||||
def _convert_response_format(response_format: Mapping[str, Any]) -> dict[str, Any]:
|
||||
@@ -470,11 +470,11 @@ def _convert_response_format(response_format: Mapping[str, Any]) -> dict[str, An
|
||||
if format_type == "json_schema":
|
||||
schema_section = response_format.get("json_schema", response_format)
|
||||
if not isinstance(schema_section, Mapping):
|
||||
raise ServiceInvalidRequestError("json_schema response_format must be a mapping.")
|
||||
raise IntegrationInvalidRequestException("json_schema response_format must be a mapping.")
|
||||
schema_section_typed = cast("Mapping[str, Any]", schema_section)
|
||||
schema: Any = schema_section_typed.get("schema")
|
||||
if schema is None:
|
||||
raise ServiceInvalidRequestError("json_schema response_format requires a schema.")
|
||||
raise IntegrationInvalidRequestException("json_schema response_format requires a schema.")
|
||||
name: str = str(
|
||||
schema_section_typed.get("name")
|
||||
or schema_section_typed.get("title")
|
||||
@@ -495,4 +495,4 @@ def _convert_response_format(response_format: Mapping[str, Any]) -> dict[str, An
|
||||
if format_type in {"json_object", "text"}:
|
||||
return {"type": format_type}
|
||||
|
||||
raise ServiceInvalidRequestError("Unsupported response_format provided for Azure AI client.")
|
||||
raise IntegrationInvalidRequestException("Unsupported response_format provided for Azure AI client.")
|
||||
|
||||
@@ -9,7 +9,6 @@ from agent_framework import (
|
||||
Agent,
|
||||
tool,
|
||||
)
|
||||
from agent_framework.exceptions import ServiceInitializationError
|
||||
from azure.ai.agents.models import (
|
||||
Agent as AzureAgent,
|
||||
)
|
||||
@@ -37,7 +36,6 @@ skip_if_azure_ai_integration_tests_disabled = pytest.mark.skipif(
|
||||
else "Integration tests are disabled.",
|
||||
)
|
||||
|
||||
|
||||
# region Provider Initialization Tests
|
||||
|
||||
|
||||
@@ -90,7 +88,7 @@ def test_provider_init_missing_endpoint_raises(
|
||||
with patch("agent_framework_azure_ai._agent_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {"project_endpoint": None, "model_deployment_name": "test-model"}
|
||||
|
||||
with pytest.raises(ServiceInitializationError) as exc_info:
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
AzureAIAgentsProvider(credential=mock_azure_credential)
|
||||
|
||||
assert "project endpoint is required" in str(exc_info.value).lower()
|
||||
@@ -98,7 +96,7 @@ def test_provider_init_missing_endpoint_raises(
|
||||
|
||||
def test_provider_init_missing_credential_raises(azure_ai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test AzureAIAgentsProvider raises error when credential is missing."""
|
||||
with pytest.raises(ServiceInitializationError) as exc_info:
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
AzureAIAgentsProvider()
|
||||
|
||||
assert "credential is required" in str(exc_info.value).lower()
|
||||
@@ -106,7 +104,6 @@ def test_provider_init_missing_credential_raises(azure_ai_unit_test_env: dict[st
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region Context Manager Tests
|
||||
|
||||
|
||||
@@ -142,7 +139,6 @@ async def test_provider_context_manager_does_not_close_external_client(mock_agen
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region create_agent Tests
|
||||
|
||||
|
||||
@@ -272,7 +268,7 @@ async def test_create_agent_missing_model_raises(
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
with pytest.raises(ServiceInitializationError) as exc_info:
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
await provider.create_agent(name="TestAgent")
|
||||
|
||||
assert "model deployment name is required" in str(exc_info.value).lower()
|
||||
@@ -280,7 +276,6 @@ async def test_create_agent_missing_model_raises(
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region get_agent Tests
|
||||
|
||||
|
||||
@@ -332,7 +327,7 @@ async def test_get_agent_with_function_tools(
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
with pytest.raises(ServiceInitializationError) as exc_info:
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
await provider.get_agent("agent-with-tools")
|
||||
|
||||
assert "get_weather" in str(exc_info.value)
|
||||
@@ -374,7 +369,6 @@ async def test_get_agent_with_provided_function_tools(
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region as_agent Tests
|
||||
|
||||
|
||||
@@ -427,7 +421,7 @@ def test_as_agent_with_function_tools_validates(
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
with pytest.raises(ServiceInitializationError) as exc_info:
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
provider.as_agent(mock_agent)
|
||||
|
||||
assert "my_function" in str(exc_info.value)
|
||||
@@ -489,7 +483,7 @@ def test_as_agent_with_dict_function_tools_validates(
|
||||
|
||||
provider = AzureAIAgentsProvider(agents_client=mock_agents_client)
|
||||
|
||||
with pytest.raises(ServiceInitializationError) as exc_info:
|
||||
with pytest.raises(ValueError) as exc_info:
|
||||
provider.as_agent(mock_agent)
|
||||
|
||||
assert "dict_based_function" in str(exc_info.value)
|
||||
@@ -534,7 +528,6 @@ def test_as_agent_with_dict_function_tools_provided(
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region Tool Conversion Tests - to_azure_ai_agent_tools
|
||||
|
||||
|
||||
@@ -659,7 +652,6 @@ def test_to_azure_ai_agent_tools_unsupported_type() -> None:
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region Tool Conversion Tests - from_azure_ai_agent_tools
|
||||
|
||||
|
||||
@@ -784,7 +776,6 @@ def test_from_azure_ai_agent_tools_unknown_dict() -> None:
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region Integration Tests
|
||||
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ from agent_framework import (
|
||||
)
|
||||
from agent_framework._serialization import SerializationMixin
|
||||
from agent_framework._settings import load_settings
|
||||
from agent_framework.exceptions import ServiceInitializationError, ServiceInvalidRequestError
|
||||
from agent_framework.exceptions import ChatClientInvalidRequestException
|
||||
from azure.ai.agents.models import (
|
||||
AgentsNamedToolChoice,
|
||||
AgentsNamedToolChoiceType,
|
||||
@@ -165,7 +165,7 @@ def test_azure_ai_chat_client_init_missing_project_endpoint() -> None:
|
||||
with patch("agent_framework_azure_ai._chat_client.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {"project_endpoint": None, "model_deployment_name": "test-model"}
|
||||
|
||||
with pytest.raises(ServiceInitializationError, match="project endpoint is required"):
|
||||
with pytest.raises(ValueError, match="project endpoint is required"):
|
||||
AzureAIAgentClient(
|
||||
agents_client=None,
|
||||
agent_id=None,
|
||||
@@ -181,7 +181,7 @@ def test_azure_ai_chat_client_init_missing_model_deployment_for_agent_creation()
|
||||
with patch("agent_framework_azure_ai._chat_client.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {"project_endpoint": "https://test.com", "model_deployment_name": None}
|
||||
|
||||
with pytest.raises(ServiceInitializationError, match="model deployment name is required"):
|
||||
with pytest.raises(ValueError, match="model deployment name is required"):
|
||||
AzureAIAgentClient(
|
||||
agents_client=None,
|
||||
agent_id=None, # No existing agent
|
||||
@@ -193,9 +193,7 @@ def test_azure_ai_chat_client_init_missing_model_deployment_for_agent_creation()
|
||||
|
||||
def test_azure_ai_chat_client_init_missing_credential(azure_ai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test AzureAIAgentClient.__init__ when credential is missing and no agents_client provided."""
|
||||
with pytest.raises(
|
||||
ServiceInitializationError, match="Azure credential is required when agents_client is not provided"
|
||||
):
|
||||
with pytest.raises(ValueError, match="Azure credential is required when agents_client is not provided"):
|
||||
AzureAIAgentClient(
|
||||
agents_client=None,
|
||||
agent_id="existing-agent",
|
||||
@@ -325,7 +323,7 @@ async def test_azure_ai_chat_client_get_agent_id_or_create_missing_model(
|
||||
"""Test _get_agent_id_or_create when model_deployment_name is missing."""
|
||||
client = create_test_azure_ai_chat_client(mock_agents_client)
|
||||
|
||||
with pytest.raises(ServiceInitializationError, match="Model deployment name is required"):
|
||||
with pytest.raises(ValueError, match="Model deployment name is required"):
|
||||
await client._get_agent_id_or_create() # type: ignore
|
||||
|
||||
|
||||
@@ -2011,7 +2009,7 @@ async def test_azure_ai_chat_client_prepare_options_with_invalid_response_format
|
||||
# Invalid response_format (not BaseModel or Mapping)
|
||||
chat_options: ChatOptions = {"response_format": "invalid_format"} # type: ignore[typeddict-item]
|
||||
|
||||
with pytest.raises(ServiceInvalidRequestError, match="response_format must be a Pydantic BaseModel"):
|
||||
with pytest.raises(ChatClientInvalidRequestException, match="response_format must be a Pydantic BaseModel"):
|
||||
await client._prepare_options([], chat_options) # type: ignore
|
||||
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ from agent_framework import (
|
||||
tool,
|
||||
)
|
||||
from agent_framework._settings import load_settings
|
||||
from agent_framework.exceptions import ServiceInitializationError
|
||||
from azure.ai.projects.aio import AIProjectClient
|
||||
from azure.ai.projects.models import (
|
||||
ApproximateLocation,
|
||||
@@ -213,15 +212,13 @@ def test_init_missing_project_endpoint() -> None:
|
||||
with patch("agent_framework_azure_ai._client.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {"project_endpoint": None, "model_deployment_name": "test-model"}
|
||||
|
||||
with pytest.raises(ServiceInitializationError, match="Azure AI project endpoint is required"):
|
||||
with pytest.raises(ValueError, match="Azure AI project endpoint is required"):
|
||||
AzureAIClient(credential=MagicMock())
|
||||
|
||||
|
||||
def test_init_missing_credential(azure_ai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test AzureAIClient.__init__ when credential is missing and no project_client provided."""
|
||||
with pytest.raises(
|
||||
ServiceInitializationError, match="Azure credential is required when project_client is not provided"
|
||||
):
|
||||
with pytest.raises(ValueError, match="Azure credential is required when project_client is not provided"):
|
||||
AzureAIClient(
|
||||
project_endpoint=azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
|
||||
model_deployment_name=azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
|
||||
@@ -245,7 +242,7 @@ async def test_get_agent_reference_or_create_missing_agent_name(
|
||||
"""Test _get_agent_reference_or_create raises when agent_name is missing."""
|
||||
client = create_test_azure_ai_client(mock_project_client, agent_name=None)
|
||||
|
||||
with pytest.raises(ServiceInitializationError, match="Agent name is required"):
|
||||
with pytest.raises(ValueError, match="Agent name is required"):
|
||||
await client._get_agent_reference_or_create({}, None) # type: ignore
|
||||
|
||||
|
||||
@@ -283,7 +280,7 @@ async def test_get_agent_reference_missing_model(
|
||||
"""Test _get_agent_reference_or_create when model is missing for agent creation."""
|
||||
client = create_test_azure_ai_client(mock_project_client, agent_name="test-agent")
|
||||
|
||||
with pytest.raises(ServiceInitializationError, match="Model deployment name is required for agent creation"):
|
||||
with pytest.raises(ValueError, match="Model deployment name is required for agent creation"):
|
||||
await client._get_agent_reference_or_create({}, None) # type: ignore
|
||||
|
||||
|
||||
@@ -1287,7 +1284,6 @@ def test_from_azure_ai_tools_web_search() -> None:
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region Integration Tests
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
||||
import pytest
|
||||
from agent_framework import Agent, FunctionTool
|
||||
from agent_framework._mcp import MCPTool
|
||||
from agent_framework.exceptions import ServiceInitializationError
|
||||
from azure.ai.projects.aio import AIProjectClient
|
||||
from azure.ai.projects.models import (
|
||||
AgentReference,
|
||||
@@ -110,15 +109,13 @@ def test_provider_init_missing_endpoint() -> None:
|
||||
with patch("agent_framework_azure_ai._project_provider.load_settings") as mock_load_settings:
|
||||
mock_load_settings.return_value = {"project_endpoint": None, "model_deployment_name": "test-model"}
|
||||
|
||||
with pytest.raises(ServiceInitializationError, match="Azure AI project endpoint is required"):
|
||||
with pytest.raises(ValueError, match="Azure AI project endpoint is required"):
|
||||
AzureAIProjectAgentProvider(credential=MagicMock())
|
||||
|
||||
|
||||
def test_provider_init_missing_credential(azure_ai_unit_test_env: dict[str, str]) -> None:
|
||||
"""Test AzureAIProjectAgentProvider initialization when credential is missing."""
|
||||
with pytest.raises(
|
||||
ServiceInitializationError, match="Azure credential is required when project_client is not provided"
|
||||
):
|
||||
with pytest.raises(ValueError, match="Azure credential is required when project_client is not provided"):
|
||||
AzureAIProjectAgentProvider(
|
||||
project_endpoint=azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
|
||||
)
|
||||
@@ -208,7 +205,7 @@ async def test_provider_create_agent_missing_model(mock_project_client: MagicMoc
|
||||
|
||||
provider = AzureAIProjectAgentProvider(project_client=mock_project_client)
|
||||
|
||||
with pytest.raises(ServiceInitializationError, match="Model deployment name is required"):
|
||||
with pytest.raises(ValueError, match="Model deployment name is required"):
|
||||
await provider.create_agent(name="test-agent")
|
||||
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import pytest
|
||||
from agent_framework import (
|
||||
FunctionTool,
|
||||
)
|
||||
from agent_framework.exceptions import ServiceInvalidRequestError
|
||||
from agent_framework.exceptions import IntegrationInvalidRequestException
|
||||
from azure.ai.agents.models import CodeInterpreterToolDefinition
|
||||
from pydantic import BaseModel
|
||||
|
||||
@@ -387,7 +387,7 @@ def test_create_text_format_config_text() -> None:
|
||||
|
||||
def test_create_text_format_config_invalid_raises() -> None:
|
||||
"""Test invalid response_format raises error."""
|
||||
with pytest.raises(ServiceInvalidRequestError):
|
||||
with pytest.raises(IntegrationInvalidRequestException):
|
||||
create_text_format_config({"type": "invalid"})
|
||||
|
||||
|
||||
@@ -400,7 +400,7 @@ def test_convert_response_format_with_format_key() -> None:
|
||||
|
||||
def test_convert_response_format_json_schema_missing_schema_raises() -> None:
|
||||
"""Test json_schema without schema raises error."""
|
||||
with pytest.raises(ServiceInvalidRequestError, match="requires a schema"):
|
||||
with pytest.raises(IntegrationInvalidRequestException, match="requires a schema"):
|
||||
_convert_response_format({"type": "json_schema", "json_schema": {}})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user