Skip to content

Commit 52e71f6

Browse files
committed
Add projects sdk version to openai user-agent and update assets.json tag and enhance User-Agent header sanitization in tests
1 parent daf8953 commit 52e71f6

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

sdk/ai/azure-ai-projects/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/ai/azure-ai-projects",
5-
"Tag": "python/ai/azure-ai-projects_b0e6f379ee"
5+
"Tag": "python/ai/azure-ai-projects_838ee29bca"
66
}

sdk/ai/azure-ai-projects/azure/ai/projects/_patch.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from azure.identity import get_bearer_token_provider
1717
from ._client import AIProjectClient as AIProjectClientGenerated
1818
from .operations import TelemetryOperations
19+
from ._version import VERSION
1920

2021

2122
logger = logging.getLogger(__name__)
@@ -141,6 +142,11 @@ def get_openai_client(self, **kwargs: Any) -> "OpenAI": # type: ignore[name-def
141142
base_url,
142143
)
143144

145+
default_headers = kwargs.get("default_headers", {})
146+
user_agent = default_headers.get("User-Agent", "")
147+
default_headers["User-Agent"] = ", ".join([str(user_agent), f"AzureAIProjects/{VERSION}"]).lstrip(", ")
148+
kwargs["default_headers"] = default_headers
149+
144150
http_client = None
145151

146152
if self._console_logging_enabled:
@@ -179,6 +185,8 @@ def handle_request(self, request: httpx.Request) -> httpx.Response:
179185

180186
print(f"\n==> Request:\n{request.method} {request.url}")
181187
headers = dict(request.headers)
188+
if "user-agent" in headers:
189+
headers["user-agent"] += f", AzureAIProjects/{VERSION}"
182190
self._sanitize_auth_header(headers)
183191
print("Headers:")
184192
for key, value in sorted(headers.items()):

sdk/ai/azure-ai-projects/tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
add_body_key_sanitizer,
2121
add_remove_header_sanitizer,
2222
add_body_regex_sanitizer,
23+
add_header_regex_sanitizer,
2324
)
2425

2526
if not load_dotenv(find_dotenv(), override=True):
2627
print("Did not find a .env file. Using default environment variable values for tests.")
2728

29+
2830
def pytest_collection_modifyitems(items):
2931
if os.environ.get("AZURE_TEST_RUN_LIVE") == "true":
3032
return
@@ -157,6 +159,9 @@ def sanitize_url_paths():
157159
headers="x-stainless-arch, x-stainless-async, x-stainless-lang, x-stainless-os, x-stainless-package-version, x-stainless-read-timeout, x-stainless-retry-count, x-stainless-runtime, x-stainless-runtime-version"
158160
)
159161

162+
# Sanitize User-Agent header to avoid platform-specific information in recordings
163+
add_header_regex_sanitizer(key="User-Agent", value="sanitized-user-agent")
164+
160165
# Remove the following sanitizers since certain fields are needed in tests and are non-sensitive:
161166
# - AZSDK3493: $..name
162167
# - AZSDK3430: $..id

0 commit comments

Comments
 (0)