Skip to content

Commit 93a9600

Browse files
committed
added tests
1 parent 5cbd98d commit 93a9600

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

python/packages/core/agent_framework/azure/_shared.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ def __init__(
230230
if not v1_base_url:
231231
raise ServiceInitializationError(
232232
"Please provide an endpoint or a base_url. "
233-
"For standard Azure OpenAI endpoints, the v1 API path will be appended automatically."
233+
"For standard Azure OpenAI endpoints (*.openai.azure.com and *.services.ai.azure.com), "
234+
"the v1 API path will be appended automatically; for non-standard or private deployments, "
235+
"you must provide a base_url that already includes the desired API path."
234236
)
235237

236238
# Determine the effective api_key for AsyncOpenAI

python/packages/core/tests/azure/test_azure_chat_client.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,56 @@ async def test_streaming_with_none_delta(
630630
assert any(msg.contents for msg in results)
631631

632632

633+
def test_client_uses_custom_base_url_when_provided() -> None:
634+
"""Test that a custom base_url is used directly when provided."""
635+
custom_base_url = "https://custom.example.com/my/path/"
636+
637+
client = AzureOpenAIChatClient(
638+
deployment_name="gpt-4o",
639+
base_url=custom_base_url,
640+
api_key="test-key",
641+
)
642+
643+
assert client.client is not None
644+
assert str(client.client.base_url) == custom_base_url
645+
646+
647+
def test_client_constructs_v1_url_for_openai_azure_com_endpoint() -> None:
648+
"""Test v1 URL construction for .openai.azure.com endpoints."""
649+
client = AzureOpenAIChatClient(
650+
deployment_name="gpt-4o",
651+
endpoint="https://my-resource.openai.azure.com",
652+
api_key="test-key",
653+
)
654+
655+
assert client.client is not None
656+
assert str(client.client.base_url) == "https://my-resource.openai.azure.com/openai/v1/"
657+
658+
659+
def test_client_constructs_v1_url_for_services_ai_azure_com_endpoint() -> None:
660+
"""Test v1 URL construction for .services.ai.azure.com endpoints."""
661+
client = AzureOpenAIChatClient(
662+
deployment_name="gpt-4o",
663+
endpoint="https://my-resource.services.ai.azure.com",
664+
api_key="test-key",
665+
)
666+
667+
assert client.client is not None
668+
assert str(client.client.base_url) == "https://my-resource.services.ai.azure.com/openai/v1/"
669+
670+
671+
def test_client_raises_error_for_non_standard_endpoint() -> None:
672+
"""Test that non-standard endpoints raise an error."""
673+
with pytest.raises(ServiceInitializationError) as exc_info:
674+
AzureOpenAIChatClient(
675+
deployment_name="gpt-4o",
676+
endpoint="https://api.openai.com",
677+
api_key="test-key",
678+
)
679+
680+
assert "Please provide an endpoint or a base_url" in str(exc_info.value)
681+
682+
633683
@ai_function
634684
def get_story_text() -> str:
635685
"""Returns a story about Emily and David."""

python/packages/core/tests/azure/test_azure_responses_client.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pytest
88
from azure.identity import AzureCliCredential
9+
from openai import AsyncOpenAI
910
from pydantic import BaseModel
1011
from pytest import param
1112

@@ -156,6 +157,59 @@ def test_serialize(azure_openai_unit_test_env: dict[str, str]) -> None:
156157
assert "User-Agent" not in dumped_settings["default_headers"]
157158

158159

160+
def test_responses_client_uses_custom_base_url_when_provided() -> None:
161+
"""Test that a custom base_url is used directly when provided."""
162+
custom_base_url = "https://custom.example.com/my/path/"
163+
164+
client = AzureOpenAIResponsesClient(
165+
deployment_name="gpt-4o",
166+
base_url=custom_base_url,
167+
api_key="test-key",
168+
)
169+
170+
assert client.client is not None
171+
assert isinstance(client.client, AsyncOpenAI)
172+
assert str(client.client.base_url) == custom_base_url
173+
174+
175+
def test_responses_client_constructs_v1_url_for_openai_azure_com_endpoint() -> None:
176+
"""Test v1 URL construction for .openai.azure.com endpoints."""
177+
client = AzureOpenAIResponsesClient(
178+
deployment_name="gpt-4o",
179+
endpoint="https://my-resource.openai.azure.com",
180+
api_key="test-key",
181+
)
182+
183+
assert client.client is not None
184+
assert isinstance(client.client, AsyncOpenAI)
185+
assert str(client.client.base_url) == "https://my-resource.openai.azure.com/openai/v1/"
186+
187+
188+
def test_responses_client_constructs_v1_url_for_services_ai_azure_com_endpoint() -> None:
189+
"""Test v1 URL construction for .services.ai.azure.com endpoints."""
190+
client = AzureOpenAIResponsesClient(
191+
deployment_name="gpt-4o",
192+
endpoint="https://my-resource.services.ai.azure.com",
193+
api_key="test-key",
194+
)
195+
196+
assert client.client is not None
197+
assert isinstance(client.client, AsyncOpenAI)
198+
assert str(client.client.base_url) == "https://my-resource.services.ai.azure.com/openai/v1/"
199+
200+
201+
def test_responses_client_raises_error_for_non_standard_endpoint() -> None:
202+
"""Test that non-standard endpoints raise an error."""
203+
with pytest.raises(ServiceInitializationError) as exc_info:
204+
AzureOpenAIResponsesClient(
205+
deployment_name="gpt-4o",
206+
endpoint="https://api.openai.com",
207+
api_key="test-key",
208+
)
209+
210+
assert "Please provide an endpoint or a base_url" in str(exc_info.value)
211+
212+
159213
# region Integration Tests
160214

161215

0 commit comments

Comments
 (0)