Skip to content

Commit baba115

Browse files
authored
Add test recordings to code interpreter test (#44297)
1 parent ef8959a commit baba115

File tree

5 files changed

+100
-19
lines changed

5 files changed

+100
-19
lines changed

sdk/ai/azure-ai-projects/.env.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ AZURE_AI_PROJECTS_TESTS_CONTAINER_INGRESS_SUBDOMAIN_SUFFIX=
6363
AZURE_AI_PROJECTS_TESTS_BING_PROJECT_CONNECTION_ID=
6464
AZURE_AI_PROJECTS_TESTS_AI_SEARCH_PROJECT_CONNECTION_ID=
6565
AZURE_AI_PROJECTS_TESTS_AI_SEARCH_INDEX_NAME=
66+
AZURE_AI_PROJECTS_TESTS_AI_SEARCH_USER_INPUT=
6667
AZURE_AI_PROJECTS_TESTS_MCP_PROJECT_CONNECTION_ID=
68+
AZURE_AI_PROJECTS_TESTS_SHAREPOINT_PROJECT_CONNECTION_ID=
69+
AZURE_AI_PROJECTS_TESTS_SHAREPOINT_USER_INPUT=
70+
6771

6872
# Used in Fine-tuning tests
6973
AZURE_AI_PROJECTS_TESTS_COMPLETED_OAI_MODEL_SFT_FINE_TUNING_JOB_ID=

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_d20bfd0fdc"
5+
"Tag": "python/ai/azure-ai-projects_4160165ae8"
66
}

sdk/ai/azure-ai-projects/tests/agents/tools/test_agent_code_interpreter.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
import pytest
1010
from test_base import TestBase, servicePreparer
11-
from devtools_testutils import is_live_and_not_recording
11+
from devtools_testutils import recorded_by_proxy, RecordedTransport
1212
from azure.ai.projects.models import (
1313
PromptAgentDefinition,
1414
CodeInterpreterTool,
@@ -19,10 +19,7 @@
1919
class TestAgentCodeInterpreter(TestBase):
2020

2121
@servicePreparer()
22-
@pytest.mark.skipif(
23-
condition=(not is_live_and_not_recording()),
24-
reason="Skipped because we cannot record network calls with OpenAI client",
25-
)
22+
@recorded_by_proxy(RecordedTransport.AZURE_CORE, RecordedTransport.HTTPX)
2623
def test_agent_code_interpreter_simple_math(self, **kwargs):
2724
"""
2825
Test agent with Code Interpreter for simple Python code execution.
@@ -45,25 +42,23 @@ def test_agent_code_interpreter_simple_math(self, **kwargs):
4542
"""
4643

4744
model = self.test_agents_params["model_deployment_name"]
45+
agent_name = "code-interpreter-simple-agent"
4846

4947
with (
5048
self.create_client(operation_group="agents", **kwargs) as project_client,
5149
project_client.get_openai_client() as openai_client,
5250
):
5351
# Create agent with code interpreter tool (no files)
5452
agent = project_client.agents.create_version(
55-
agent_name="code-interpreter-simple-agent",
53+
agent_name=agent_name,
5654
definition=PromptAgentDefinition(
5755
model=model,
5856
instructions="You are a helpful assistant that can execute Python code.",
5957
tools=[CodeInterpreterTool(container=CodeInterpreterToolAuto(file_ids=[]))],
6058
),
6159
description="Simple code interpreter agent for basic Python execution.",
6260
)
63-
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
64-
assert agent.id is not None
65-
assert agent.name == "code-interpreter-simple-agent"
66-
assert agent.version is not None
61+
self._validate_agent_version(agent, expected_name=agent_name)
6762

6863
# Ask the agent to execute a complex Python calculation
6964
# Problem: Calculate the sum of cubes from 1 to 50, then add 12!/(8!)
@@ -107,10 +102,7 @@ def test_agent_code_interpreter_simple_math(self, **kwargs):
107102
@pytest.mark.skip(
108103
reason="Skipped due to known server bug. Enable once https://msdata.visualstudio.com/Vienna/_workitems/edit/4841313 is resolved"
109104
)
110-
@pytest.mark.skipif(
111-
condition=(not is_live_and_not_recording()),
112-
reason="Skipped because we cannot record network calls with OpenAI client",
113-
)
105+
@recorded_by_proxy(RecordedTransport.AZURE_CORE, RecordedTransport.HTTPX)
114106
def test_agent_code_interpreter_file_generation(self, **kwargs):
115107
"""
116108
Test agent with Code Interpreter for file upload, processing, and download.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# pylint: disable=too-many-lines,line-too-long,useless-suppression
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
# cSpell:disable
7+
8+
from test_base import TestBase, servicePreparer
9+
from devtools_testutils.aio import recorded_by_proxy_async
10+
from devtools_testutils import RecordedTransport
11+
from azure.ai.projects.models import (
12+
PromptAgentDefinition,
13+
CodeInterpreterTool,
14+
CodeInterpreterToolAuto,
15+
)
16+
17+
18+
class TestAgentCodeInterpreterAsync(TestBase):
19+
20+
@servicePreparer()
21+
@recorded_by_proxy_async(RecordedTransport.AZURE_CORE, RecordedTransport.HTTPX)
22+
async def test_agent_code_interpreter_simple_math_async(self, **kwargs):
23+
"""
24+
Test agent with Code Interpreter for simple Python code execution (async version).
25+
26+
This test verifies that an agent can execute simple Python code
27+
without any file uploads or downloads - just pure code execution.
28+
"""
29+
30+
model = self.test_agents_params["model_deployment_name"]
31+
agent_name = "code-interpreter-simple-agent-async"
32+
33+
async with (
34+
self.create_async_client(operation_group="agents", **kwargs) as project_client,
35+
project_client.get_openai_client() as openai_client,
36+
):
37+
# Create agent with code interpreter tool (no files)
38+
agent = await project_client.agents.create_version(
39+
agent_name=agent_name,
40+
definition=PromptAgentDefinition(
41+
model=model,
42+
instructions="You are a helpful assistant that can execute Python code.",
43+
tools=[CodeInterpreterTool(container=CodeInterpreterToolAuto(file_ids=[]))],
44+
),
45+
description="Simple code interpreter agent for basic Python execution.",
46+
)
47+
self._validate_agent_version(agent, expected_name=agent_name)
48+
49+
# Ask the agent to execute a complex Python calculation
50+
# Problem: Calculate the sum of cubes from 1 to 50, then add 12!/(8!)
51+
# Expected answer: 1637505
52+
print("\nAsking agent to calculate: sum of cubes from 1 to 50, plus 12!/(8!)")
53+
54+
response = await openai_client.responses.create(
55+
input="Calculate this using Python: First, find the sum of cubes from 1 to 50 (1³ + 2³ + ... + 50³). Then add 12 factorial divided by 8 factorial (12!/8!). What is the final result?",
56+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
57+
)
58+
59+
print(f"Response completed (id: {response.id})")
60+
assert response.id is not None
61+
assert response.output is not None
62+
assert len(response.output) > 0
63+
64+
# Get the response text
65+
last_message = response.output[-1]
66+
response_text = ""
67+
68+
if last_message.type == "message":
69+
for content_item in last_message.content:
70+
if content_item.type == "output_text":
71+
response_text += content_item.text
72+
73+
print(f"Agent's response: {response_text}")
74+
75+
# Verify the response contains the correct answer (1637505)
76+
# Note: sum of cubes 1-50 = 1,625,625; 12!/8! = 11,880; total = 1,637,505
77+
assert (
78+
"1637505" in response_text or "1,637,505" in response_text
79+
), f"Expected answer 1637505 to be in response, but got: {response_text}"
80+
81+
print("✓ Code interpreter successfully executed Python code and returned correct answer")
82+
83+
# Teardown
84+
await project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
85+
print("Agent deleted")

sdk/ai/azure-ai-projects/tests/agents/tools/test_agent_function_tool_async.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,10 @@ async def test_agent_function_tool_context_dependent_followup_async(self, **kwar
375375
model = self.test_agents_params["model_deployment_name"]
376376

377377
# Setup
378-
project_client = self.create_async_client(operation_group="agents", **kwargs)
379-
openai_client = project_client.get_openai_client()
380-
381-
async with project_client:
378+
async with (
379+
self.create_async_client(operation_group="agents", **kwargs) as project_client,
380+
project_client.get_openai_client() as openai_client,
381+
):
382382
# Define function tool
383383
get_temperature = FunctionTool(
384384
name="get_temperature",

0 commit comments

Comments
 (0)