|
2 | 2 |
|
3 | 3 | import sys |
4 | 4 | from collections.abc import Mapping, MutableSequence |
5 | | -from typing import TYPE_CHECKING, Any, ClassVar, Generic, TypedDict, cast |
| 5 | +from typing import TYPE_CHECKING, Any, ClassVar, Generic, TypedDict, TypeVar, cast |
6 | 6 |
|
7 | 7 | from agent_framework import ( |
8 | 8 | AGENT_FRAMEWORK_USER_AGENT, |
|
13 | 13 | use_chat_middleware, |
14 | 14 | use_function_invocation, |
15 | 15 | ) |
16 | | -from agent_framework.exceptions import ServiceInitializationError, ServiceInvalidRequestError |
| 16 | +from agent_framework.exceptions import ServiceInitializationError |
17 | 17 | from agent_framework.observability import use_instrumentation |
18 | 18 | from agent_framework.openai._responses_client import OpenAIBaseResponsesClient |
19 | 19 | from azure.ai.projects.aio import AIProjectClient |
20 | 20 | from azure.ai.projects.models import ( |
21 | 21 | MCPTool, |
22 | 22 | PromptAgentDefinition, |
23 | 23 | PromptAgentDefinitionText, |
24 | | - ResponseTextFormatConfigurationJsonObject, |
25 | | - ResponseTextFormatConfigurationJsonSchema, |
26 | | - ResponseTextFormatConfigurationText, |
27 | 24 | ) |
28 | 25 | from azure.core.credentials_async import AsyncTokenCredential |
29 | 26 | from azure.core.exceptions import ResourceNotFoundError |
30 | | -from pydantic import BaseModel, ValidationError |
| 27 | +from pydantic import ValidationError |
31 | 28 |
|
32 | | -from ._shared import AzureAISettings |
| 29 | +from ._shared import AzureAISettings, create_text_format_config |
33 | 30 |
|
34 | 31 | if TYPE_CHECKING: |
35 | 32 | from agent_framework.openai import OpenAIResponsesOptions |
@@ -286,47 +283,6 @@ async def close(self) -> None: |
286 | 283 | """Close the project_client.""" |
287 | 284 | await self._close_client_if_needed() |
288 | 285 |
|
289 | | - def _create_text_format_config( |
290 | | - self, response_format: type[BaseModel] | Mapping[str, Any] |
291 | | - ) -> ( |
292 | | - ResponseTextFormatConfigurationJsonSchema |
293 | | - | ResponseTextFormatConfigurationJsonObject |
294 | | - | ResponseTextFormatConfigurationText |
295 | | - ): |
296 | | - """Convert response_format into Azure text format configuration.""" |
297 | | - if isinstance(response_format, type) and issubclass(response_format, BaseModel): |
298 | | - schema = response_format.model_json_schema() |
299 | | - # Ensure additionalProperties is explicitly false to satisfy Azure validation |
300 | | - if isinstance(schema, dict): |
301 | | - schema.setdefault("additionalProperties", False) |
302 | | - return ResponseTextFormatConfigurationJsonSchema( |
303 | | - name=response_format.__name__, |
304 | | - schema=schema, |
305 | | - ) |
306 | | - |
307 | | - if isinstance(response_format, Mapping): |
308 | | - format_config = self._convert_response_format(response_format) |
309 | | - format_type = format_config.get("type") |
310 | | - if format_type == "json_schema": |
311 | | - # Ensure schema includes additionalProperties=False to satisfy Azure validation |
312 | | - schema = dict(format_config.get("schema", {})) # type: ignore[assignment] |
313 | | - schema.setdefault("additionalProperties", False) |
314 | | - config_kwargs: dict[str, Any] = { |
315 | | - "name": format_config.get("name") or "response", |
316 | | - "schema": schema, |
317 | | - } |
318 | | - if "strict" in format_config: |
319 | | - config_kwargs["strict"] = format_config["strict"] |
320 | | - if "description" in format_config: |
321 | | - config_kwargs["description"] = format_config["description"] |
322 | | - return ResponseTextFormatConfigurationJsonSchema(**config_kwargs) |
323 | | - if format_type == "json_object": |
324 | | - return ResponseTextFormatConfigurationJsonObject() |
325 | | - if format_type == "text": |
326 | | - return ResponseTextFormatConfigurationText() |
327 | | - |
328 | | - raise ServiceInvalidRequestError("response_format must be a Pydantic model or mapping.") |
329 | | - |
330 | 286 | async def _get_agent_reference_or_create( |
331 | 287 | self, |
332 | 288 | run_options: dict[str, Any], |
@@ -380,7 +336,7 @@ async def _get_agent_reference_or_create( |
380 | 336 | # response_format is accessed from chat_options or additional_properties |
381 | 337 | # since the base class excludes it from run_options |
382 | 338 | if chat_options and (response_format := chat_options.get("response_format")): |
383 | | - args["text"] = PromptAgentDefinitionText(format=self._create_text_format_config(response_format)) |
| 339 | + args["text"] = PromptAgentDefinitionText(format=create_text_format_config(response_format)) |
384 | 340 |
|
385 | 341 | # Combine instructions from messages and options |
386 | 342 | combined_instructions = [ |
|
0 commit comments