Skip to content

Commit 1126bbc

Browse files
committed
Create tests
1 parent 2c0bb8a commit 1126bbc

File tree

4 files changed

+151
-9
lines changed

4 files changed

+151
-9
lines changed

sdk/ai/azure-ai-projects/samples/agents/sample_agent_structured_output.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using the synchronous AIProjectClient, while defining a desired
1111
JSON schema for the response ("structured output").
1212
13-
The OpenAI compatible Responses and Conversation calls in this sample are made using
13+
The Responses and Conversations calls in this sample are made using
1414
the OpenAI client from the `openai` package. See https://platform.openai.com/docs/api-reference
1515
for more information.
1616
@@ -40,15 +40,15 @@
4040
PromptAgentDefinitionText,
4141
ResponseTextFormatConfigurationJsonSchema,
4242
)
43-
from pydantic import BaseModel
43+
from pydantic import BaseModel, Field
4444

4545
load_dotenv()
4646

4747

4848
class CalendarEvent(BaseModel):
4949
model_config = {"extra": "forbid"}
5050
name: str
51-
date: str
51+
date: str = Field(description="Date in YYYY-MM-DD format")
5252
participants: list[str]
5353

5454

@@ -79,7 +79,13 @@ class CalendarEvent(BaseModel):
7979
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
8080

8181
conversation = openai_client.conversations.create(
82-
items=[{"type": "message", "role": "user", "content": "Alice and Bob are going to a science fair on Friday."}],
82+
items=[
83+
{
84+
"type": "message",
85+
"role": "user",
86+
"content": "Alice and Bob are going to a science fair this Friday, November 7, 2025.",
87+
}
88+
],
8389
)
8490
print(f"Created conversation with initial user message (id: {conversation.id})")
8591

sdk/ai/azure-ai-projects/samples/agents/sample_agent_structured_output_async.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@
4141
PromptAgentDefinitionText,
4242
ResponseTextFormatConfigurationJsonSchema,
4343
)
44-
from pydantic import BaseModel
44+
from pydantic import BaseModel, Field
4545

4646
load_dotenv()
4747

4848

4949
class CalendarEvent(BaseModel):
5050
model_config = {"extra": "forbid"}
5151
name: str
52-
date: str
52+
date: str = Field(description="Date in YYYY-MM-DD format")
5353
participants: list[str]
5454

5555

@@ -90,7 +90,7 @@ async def main() -> None:
9090
{
9191
"type": "message",
9292
"role": "user",
93-
"content": "Alice and Bob are going to a science fair on Friday.",
93+
"content": "Alice and Bob are going to a science fair this Friday, November 7, 2025.",
9494
}
9595
],
9696
)

sdk/ai/azure-ai-projects/tests/agents/test_agent_responses_crud.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
# ------------------------------------
66
# cSpell:disable
77

8+
from pydantic import BaseModel, Field
89
import pytest
910
from test_base import TestBase, servicePreparer
1011
from devtools_testutils import is_live_and_not_recording
11-
from azure.ai.projects.models import PromptAgentDefinition
12+
from azure.ai.projects.models import (
13+
PromptAgentDefinition,
14+
ResponseTextFormatConfigurationJsonSchema,
15+
PromptAgentDefinitionText,
16+
)
1217

1318

1419
class TestAgentResponsesCrud(TestBase):
@@ -150,3 +155,65 @@ def test_agent_responses_crud(self, **kwargs):
150155

151156
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
152157
print("Agent deleted")
158+
159+
# To run this tes:
160+
# pytest tests\agents\test_agent_responses_crud.py::TestAgentResponsesCrud::test_agent_responses_with_structured_output -s
161+
@servicePreparer()
162+
@pytest.mark.skipif(
163+
condition=(not is_live_and_not_recording()),
164+
reason="Skipped because we cannot record network calls with OpenAI client",
165+
)
166+
def test_agent_responses_with_structured_output(self, **kwargs):
167+
model = self.test_agents_params["model_deployment_name"]
168+
169+
# Setup
170+
project_client = self.create_client(operation_group="agents", **kwargs)
171+
openai_client = project_client.get_openai_client()
172+
173+
class CalendarEvent(BaseModel):
174+
model_config = {"extra": "forbid"}
175+
name: str
176+
date: str = Field(description="Date in YYYY-MM-DD format")
177+
participants: list[str]
178+
179+
agent = project_client.agents.create_version(
180+
agent_name="MyAgent",
181+
definition=PromptAgentDefinition(
182+
model=model,
183+
text=PromptAgentDefinitionText(
184+
format=ResponseTextFormatConfigurationJsonSchema(
185+
name="CalendarEvent", schema=CalendarEvent.model_json_schema()
186+
)
187+
),
188+
instructions="""
189+
You are a helpful assistant that extracts calendar event information from the input user messages,
190+
and returns it in the desired structured output format.
191+
""",
192+
),
193+
)
194+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
195+
196+
conversation = openai_client.conversations.create(
197+
items=[
198+
{
199+
"type": "message",
200+
"role": "user",
201+
"content": "Alice and Bob are going to a science fair this Friday, November 7, 2025.",
202+
}
203+
]
204+
)
205+
print(f"Created conversation with initial user message (id: {conversation.id})")
206+
207+
response = openai_client.responses.create(
208+
conversation=conversation.id,
209+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
210+
input="", # TODO: Remove 'input' once service is fixed
211+
)
212+
print(f"Response id: {response.id}, output text: {response.output_text}")
213+
assert response.output_text == '{"name":"Science Fair","date":"2025-11-07","participants":["Alice","Bob"]}'
214+
215+
openai_client.conversations.delete(conversation_id=conversation.id)
216+
print("Conversation deleted")
217+
218+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
219+
print("Agent deleted")

sdk/ai/azure-ai-projects/tests/agents/test_agent_responses_crud_async.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
# ------------------------------------
66
# cSpell:disable
77

8+
from pydantic import BaseModel, Field
89
import pytest
910
from test_base import TestBase, servicePreparer
1011
from devtools_testutils import is_live_and_not_recording
11-
from azure.ai.projects.models import PromptAgentDefinition
12+
from azure.ai.projects.models import (
13+
PromptAgentDefinition,
14+
ResponseTextFormatConfigurationJsonSchema,
15+
PromptAgentDefinitionText,
16+
)
1217

1318

1419
class TestAgentResponsesCrudAsync(TestBase):
@@ -122,3 +127,67 @@ async def test_agent_responses_crud_async(self, **kwargs):
122127

123128
await project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
124129
print("Agent deleted")
130+
131+
# To run this test:
132+
# pytest tests\agents\test_agent_responses_crud_async.py::TestAgentResponsesCrudAsync::test_agent_responses_with_structured_output_async -s
133+
@servicePreparer()
134+
@pytest.mark.skipif(
135+
condition=(not is_live_and_not_recording()),
136+
reason="Skipped because we cannot record network calls with OpenAI client",
137+
)
138+
async def test_agent_responses_with_structured_output_async(self, **kwargs):
139+
model = self.test_agents_params["model_deployment_name"]
140+
141+
# Setup
142+
project_client = self.create_async_client(operation_group="agents", **kwargs)
143+
openai_client = await project_client.get_openai_client()
144+
145+
class CalendarEvent(BaseModel):
146+
model_config = {"extra": "forbid"}
147+
name: str
148+
date: str = Field(description="Date in YYYY-MM-DD format")
149+
participants: list[str]
150+
151+
async with project_client:
152+
153+
agent = await project_client.agents.create_version(
154+
agent_name="MyAgent",
155+
definition=PromptAgentDefinition(
156+
model=model,
157+
text=PromptAgentDefinitionText(
158+
format=ResponseTextFormatConfigurationJsonSchema(
159+
name="CalendarEvent", schema=CalendarEvent.model_json_schema()
160+
)
161+
),
162+
instructions="""
163+
You are a helpful assistant that extracts calendar event information from the input user messages,
164+
and returns it in the desired structured output format.
165+
""",
166+
),
167+
)
168+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
169+
170+
conversation = await openai_client.conversations.create(
171+
items=[
172+
{
173+
"type": "message",
174+
"role": "user",
175+
"content": "Alice and Bob are going to a science fair this Friday, November 7, 2025.",
176+
}
177+
]
178+
)
179+
print(f"Created conversation with initial user message (id: {conversation.id})")
180+
181+
response = await openai_client.responses.create(
182+
conversation=conversation.id,
183+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
184+
input="", # TODO: Remove 'input' once service is fixed
185+
)
186+
print(f"Response id: {response.id}, output text: {response.output_text}")
187+
assert response.output_text == '{"name":"Science Fair","date":"2025-11-07","participants":["Alice","Bob"]}'
188+
189+
await openai_client.conversations.delete(conversation_id=conversation.id)
190+
print("Conversation deleted")
191+
192+
await project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
193+
print("Agent deleted")

0 commit comments

Comments
 (0)