Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdk/ai/azure-ai-projects/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ A2A_USER_INPUT=
AZURE_TEST_RUN_LIVE=false
AZURE_SKIP_LIVE_RECORDING=true

CONTAINER_APP_RESOURCE_ID=
CONTAINER_INGRESS_SUBDOMAIN_SUFFIX=

# Used in Fine-tuning tests
COMPLETED_OAI_MODEL_SFT_FINE_TUNING_JOB_ID=
COMPLETED_OAI_MODEL_RFT_FINE_TUNING_JOB_ID=
Expand Down
2 changes: 1 addition & 1 deletion sdk/ai/azure-ai-projects/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "python",
"TagPrefix": "python/ai/azure-ai-projects",
"Tag": "python/ai/azure-ai-projects_b0e6f379ee"
"Tag": "python/ai/azure-ai-projects_8677538e54"
}
12 changes: 11 additions & 1 deletion sdk/ai/azure-ai-projects/azure/ai/projects/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from azure.identity import get_bearer_token_provider
from ._client import AIProjectClient as AIProjectClientGenerated
from .operations import TelemetryOperations
from ._version import VERSION


logger = logging.getLogger(__name__)
Expand All @@ -25,7 +26,7 @@ def _patch_user_agent(user_agent: Optional[str]) -> str:
# All authenticated external clients exposed by this client will have this application id
# set on their user-agent. For more info on user-agent HTTP header, see:
# https://azure.github.io/azure-sdk/general_azurecore.html#telemetry-policy
USER_AGENT_APP_ID = "AIProjectClient"
USER_AGENT_APP_ID = f"AIProjectClient/Python-{VERSION}"

if user_agent:
# If the calling application has set "user_agent" when constructing the AIProjectClient,
Expand Down Expand Up @@ -120,6 +121,7 @@ def get_openai_client(self, **kwargs: Any) -> "OpenAI": # type: ignore[name-def
* ``base_url`` set to the endpoint provided to the AIProjectClient constructor, with "/openai" appended.
* ``api-version`` set to "2025-05-15-preview" by default, unless overridden by the ``api_version`` keyword argument.
* ``api_key`` set to a get_bearer_token_provider() callable that uses the TokenCredential provided to the AIProjectClient constructor, with scope "https://ai.azure.com/.default".
* ``default_headers`` will automatically include a User-Agent header with the default value "AIProjectClient/Python-{version}".

.. note:: The packages ``openai`` and ``azure.identity`` must be installed prior to calling this method.

Expand All @@ -136,6 +138,13 @@ def get_openai_client(self, **kwargs: Any) -> "OpenAI": # type: ignore[name-def
if "default_query" not in kwargs:
kwargs["default_query"] = {"api-version": "2025-11-15-preview"}

# Set User-Agent header
user_headers = kwargs.pop("default_headers", {})
if "User-Agent" in user_headers:
user_headers["User-Agent"] = _patch_user_agent(user_headers["User-Agent"])
else:
user_headers["User-Agent"] = self._patched_user_agent

logger.debug( # pylint: disable=specify-parameter-names-in-call
"[get_openai_client] Creating OpenAI client using Entra ID authentication, base_url = `%s`", # pylint: disable=line-too-long
base_url,
Expand Down Expand Up @@ -249,6 +258,7 @@ def _log_request_body(self, request: httpx.Request) -> None:
),
base_url=base_url,
http_client=http_client,
default_headers=user_headers,
**kwargs,
)

Expand Down
9 changes: 9 additions & 0 deletions sdk/ai/azure-ai-projects/azure/ai/projects/aio/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def get_openai_client(self, **kwargs: Any) -> "AsyncOpenAI": # type: ignore[nam
* ``base_url`` set to the endpoint provided to the AIProjectClient constructor, with "/openai" appended.
* ``api-version`` set to "2025-05-15-preview" by default, unless overridden by the ``api_version`` keyword argument.
* ``api_key`` set to a get_bearer_token_provider() callable that uses the TokenCredential provided to the AIProjectClient constructor, with scope "https://ai.azure.com/.default".
* ``default_headers`` will automatically include a User-Agent header with the default value "AIProjectClient/Python-{version}".

.. note:: The packages ``openai`` and ``azure.identity`` must be installed prior to calling this method.

Expand All @@ -121,6 +122,13 @@ def get_openai_client(self, **kwargs: Any) -> "AsyncOpenAI": # type: ignore[nam
if "default_query" not in kwargs:
kwargs["default_query"] = {"api-version": "2025-11-15-preview"}

# Set User-Agent header
user_headers = kwargs.pop("default_headers", {})
if "User-Agent" in user_headers:
user_headers["User-Agent"] = _patch_user_agent(user_headers["User-Agent"])
else:
user_headers["User-Agent"] = self._patched_user_agent

logger.debug( # pylint: disable=specify-parameter-names-in-call
"[get_openai_client] Creating OpenAI client using Entra ID authentication, base_url = `%s`", # pylint: disable=line-too-long
base_url,
Expand Down Expand Up @@ -234,6 +242,7 @@ def _log_request_body(self, request: httpx.Request) -> None:
),
base_url=base_url,
http_client=http_client,
default_headers=user_headers,
**kwargs,
)

Expand Down
4 changes: 4 additions & 0 deletions sdk/ai/azure-ai-projects/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
add_body_key_sanitizer,
add_remove_header_sanitizer,
add_body_regex_sanitizer,
add_header_regex_sanitizer,
)

if not load_dotenv(find_dotenv(), override=True):
Expand Down Expand Up @@ -164,6 +165,9 @@ def sanitize_url_paths():
remove_batch_sanitizers(["AZSDK3493"])
remove_batch_sanitizers(["AZSDK3430"])

# Sanitize User-Agent header
add_header_regex_sanitizer(key="User-Agent", value="sanitized-user-agent")

# Sanitize ARM operation headers that contain certificates and identifiers
add_general_regex_sanitizer(regex=r"[?&]t=[0-9]+", value="&t=sanitized-timestamp")
add_general_regex_sanitizer(regex=r"[?&]c=[^&\"]+", value="&c=sanitized-certificate")
Expand Down
Loading