Typing experiments#45831
Conversation
39439b4
into
Azure:dargilco/typing-experiments
There was a problem hiding this comment.
Pull request overview
Adds a typing “patch” layer for azure-ai-projects so the OpenAI client returned by AIProjectClient.get_openai_client() can accept Azure-specific grader types for evals.create(), and updates a local typing experiment script to use the new type.
Changes:
- Added a new
_patch.pyitype stub to widenevals.create(testing_criteria=...)accepted types. - Updated
get_openai_clientreturn annotation in_patch.pyto referenceOpenAIdirectly. - Updated the typing experiment sample to use
find_dotenv()and to referenceAzureAIGraderCoherenceParamfrom_patch.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| sdk/ai/azure-ai-projects/typing-experiments/modified_sample_evaluations_graders.py | Updates the experiment script to use the new grader TypedDict and tweaks dotenv loading; currently contains a runtime import issue and double-create resource leak. |
| sdk/ai/azure-ai-projects/azure/ai/projects/_patch.pyi | Introduces the type stub that widens accepted grader types and re-types get_openai_client(); packaging/syntax compatibility concerns noted. |
| sdk/ai/azure-ai-projects/azure/ai/projects/_patch.py | Minor signature typing change for get_openai_client(); one now-redundant type: ignore remains. |
| testing_criteria=testing_criteria, | ||
| ) | ||
| eval_object = client.evals.create( | ||
| name="OpenAI graders test", | ||
| data_source_config=data_source_config, | ||
| testing_criteria=[ | ||
| AzureAIGraderCoherenceParam( | ||
| type="azure_ai_evaluator", | ||
| name="coherence", | ||
| evaluator_name="builtin.coherence", | ||
| initialization_parameters={"deployment_name": f"{model_deployment_name}"}, | ||
| data_mapping={"query": "{{item.query}}", "response": "{{item.response}}"}, | ||
| )], |
There was a problem hiding this comment.
This script creates an evaluation twice (client.evals.create(...) is called twice) but only deletes the second one (the first eval_object is overwritten). This will leave an evaluation resource behind and can create clutter/cost. Consider deleting both evaluations (track both ids) or remove the redundant first create call.
| testing_criteria=testing_criteria, | |
| ) | |
| eval_object = client.evals.create( | |
| name="OpenAI graders test", | |
| data_source_config=data_source_config, | |
| testing_criteria=[ | |
| AzureAIGraderCoherenceParam( | |
| type="azure_ai_evaluator", | |
| name="coherence", | |
| evaluator_name="builtin.coherence", | |
| initialization_parameters={"deployment_name": f"{model_deployment_name}"}, | |
| data_mapping={"query": "{{item.query}}", "response": "{{item.response}}"}, | |
| )], | |
| testing_criteria=[ | |
| AzureAIGraderCoherenceParam( | |
| type="azure_ai_evaluator", | |
| name="coherence", | |
| evaluator_name="builtin.coherence", | |
| initialization_parameters={"deployment_name": f"{model_deployment_name}"}, | |
| data_mapping={"query": "{{item.query}}", "response": "{{item.response}}"}, | |
| ) | |
| ], |
| metadata: Optional[Metadata] | Omit = ..., | ||
| name: str | Omit = ..., | ||
| extra_headers: Headers | None = None, | ||
| extra_query: Query | None = None, | ||
| extra_body: Body | None = None, | ||
| timeout: float | httpx.Timeout | None | NotGiven = ..., |
There was a problem hiding this comment.
This stub uses PEP 604 unions (e.g., Headers | None, str | Omit). Since this package supports Python 3.9, some type-checking setups targeting 3.9 will reject this syntax. Prefer Union[...] / Optional[...] forms in the .pyi to keep it compatible with Python 3.9 type checkers.
| metadata: Optional[Metadata] | Omit = ..., | |
| name: str | Omit = ..., | |
| extra_headers: Headers | None = None, | |
| extra_query: Query | None = None, | |
| extra_body: Body | None = None, | |
| timeout: float | httpx.Timeout | None | NotGiven = ..., | |
| metadata: Union[Optional[Metadata], Omit] = ..., | |
| name: Union[str, Omit] = ..., | |
| extra_headers: Optional[Headers] = None, | |
| extra_query: Optional[Query] = None, | |
| extra_body: Optional[Body] = None, | |
| timeout: Union[float, httpx.Timeout, None, NotGiven] = ..., |
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| """Type stub for _patch.py. | ||
|
|
||
| Overrides get_openai_client() return type so that evals.create() accepts | ||
| Azure-specific grader types in addition to the standard OpenAI graders. | ||
| """ | ||
|
|
There was a problem hiding this comment.
Adding _patch.pyi may not affect consumers unless it is included in the published sdist/wheel. In this package, MANIFEST.in only includes py.typed and pyproject.toml's [tool.setuptools.package-data] only lists py.typed, so the new .pyi is likely not shipped. Please update packaging config to include azure/ai/projects/_patch.pyi (or *.pyi) as package data.
|
|
||
| @distributed_trace | ||
| def get_openai_client(self, **kwargs: Any) -> "OpenAI": # type: ignore[name-defined] # pylint: disable=too-many-statements | ||
| def get_openai_client(self, **kwargs: Any) -> OpenAI: # type: ignore[name-defined] # pylint: disable=too-many-statements |
There was a problem hiding this comment.
OpenAI is imported in this module (from openai import OpenAI), so # type: ignore[name-defined] on the return annotation is no longer necessary and may hide real type issues. Consider removing the type: ignore[name-defined] here.
| def get_openai_client(self, **kwargs: Any) -> OpenAI: # type: ignore[name-defined] # pylint: disable=too-many-statements | |
| def get_openai_client(self, **kwargs: Any) -> OpenAI: # pylint: disable=too-many-statements |
| from openai.types.eval_create_params import DataSourceConfigCustom | ||
| from azure.identity import DefaultAzureCredential | ||
| from azure.ai.projects import AIProjectClient | ||
| from azure.ai.projects._patch import AzureAIGraderCoherenceParam |
There was a problem hiding this comment.
AzureAIGraderCoherenceParam is imported from azure.ai.projects._patch, but it only exists in _patch.pyi (type stub) and is not defined in _patch.py, so this import will fail at runtime. Either define/export AzureAIGraderCoherenceParam in _patch.py (e.g., as a TypedDict) or keep the TypedDict definition local to this script (or gate the import behind TYPE_CHECKING and avoid using it at runtime).
Description
Please add an informative description that covers that changes made by the pull request and link all relevant issues.
If an SDK is being regenerated based on a new API spec, a link to the pull request containing these API spec changes should be included above.
All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines