1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
|
# pylint: disable=line-too-long,useless-suppression
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
# cSpell:disable
import os
import json
import jsonref
import time
import pytest
from typing import Any, Callable, Dict, List, Optional, Set
from azure.ai.agents.models import (
AgentsResponseFormatMode,
AgentsResponseFormat,
AsyncAgentEventHandler,
AsyncFunctionTool,
McpTool,
MessageDeltaChunk,
MessageDeltaTextContent,
OpenApiAnonymousAuthDetails,
OpenApiTool,
RequiredMcpToolCall,
RunStatus,
RunStep,
RunStepActivityDetails,
RunStepMcpToolCall,
RunStepToolCallDetails,
SubmitToolApprovalAction,
ThreadMessage,
ThreadRun,
ToolApproval,
AsyncToolSet,
Tool,
)
from azure.ai.agents.telemetry._ai_agents_instrumentor import _AIAgentsInstrumentorPreview
from azure.ai.agents.telemetry import _utils
from azure.core.settings import settings
from gen_ai_trace_verifier import GenAiTraceVerifier
from azure.ai.agents.aio import AgentsClient
from devtools_testutils.aio import recorded_by_proxy_async
from test_agents_client_base import agentClientPreparer
from test_ai_instrumentor_base import TestAiAgentsInstrumentorBase
settings.tracing_implementation = "OpenTelemetry"
_utils._span_impl_type = settings.tracing_implementation()
class TestAiAgentsInstrumentor(TestAiAgentsInstrumentorBase):
"""Tests for AI agents instrumentor."""
def create_client(self, **kwargs):
"""helper function: create client and using environment variables"""
# fetch environment variables
endpoint = kwargs.pop("azure_ai_agents_tests_project_endpoint")
credential = self.get_credential(AgentsClient, is_async=False)
# create and return client
client = AgentsClient(
endpoint=endpoint,
credential=credential,
)
return client
def test_convert_api_response_format_exception(self):
"""Test that the exception is raised if agent_api_response_to_str is given wrong type."""
with pytest.raises(ValueError) as cm:
_AIAgentsInstrumentorPreview.agent_api_response_to_str(42)
assert "Unknown response format <class 'int'>" in cm.value.args[0]
@pytest.mark.parametrize(
"fmt,expected",
[
(None, None),
("neep", "neep"),
(AgentsResponseFormatMode.AUTO, "auto"),
(AgentsResponseFormat(type="test"), "test"),
],
)
def test_convert_api_response_format(self, fmt, expected):
"""Test conversion of AgentsResponseFormatOption to string"""
actual = _AIAgentsInstrumentorPreview.agent_api_response_to_str(fmt)
assert actual == expected
@pytest.mark.usefixtures("instrument_with_content")
@agentClientPreparer()
@recorded_by_proxy_async
async def test_agent_chat_with_tracing_content_recording_enabled(self, **kwargs):
client = self.create_client(**kwargs)
agent = await client.create_agent(model="gpt-4o-mini", name="my-agent", instructions="You are helpful agent")
thread = await client.threads.create()
await client.messages.create(thread_id=thread.id, role="user", content="Hello, tell me a joke")
run = await client.runs.create(thread_id=thread.id, agent_id=agent.id)
while run.status in ["queued", "in_progress", "requires_action"]:
# wait for a second
time.sleep(self._sleep_time())
run = await client.runs.get(thread_id=thread.id, run_id=run.id)
print("Run status:", run.status)
print("Run completed with status:", run.status)
# delete agent and close client
await client.delete_agent(agent.id)
print("Deleted agent")
messages = [m async for m in client.messages.list(thread_id=thread.id)]
assert len(messages) > 1
await client.close()
self.exporter.force_flush()
spans = self.exporter.get_spans_by_name("create_agent my-agent")
assert len(spans) == 1
span = spans[0]
expected_attributes = [
("gen_ai.system", "az.ai.agents"),
("gen_ai.operation.name", "create_agent"),
("server.address", ""),
("gen_ai.request.model", "gpt-4o-mini"),
("gen_ai.agent.name", "my-agent"),
("gen_ai.agent.id", ""),
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
expected_events = [
{
"name": "gen_ai.system.message",
"attributes": {
"gen_ai.system": "az.ai.agents",
"gen_ai.event.content": '{"content": "You are helpful agent"}',
},
}
]
events_match = GenAiTraceVerifier().check_span_events(span, expected_events)
assert events_match == True
spans = self.exporter.get_spans_by_name("create_thread")
assert len(spans) == 1
span = spans[0]
expected_attributes = [
("gen_ai.system", "az.ai.agents"),
("gen_ai.operation.name", "create_thread"),
("server.address", ""),
("gen_ai.thread.id", ""),
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
spans = self.exporter.get_spans_by_name("create_message")
assert len(spans) == 1
span = spans[0]
expected_attributes = [
("gen_ai.system", "az.ai.agents"),
("gen_ai.operation.name", "create_message"),
("server.address", ""),
("gen_ai.thread.id", ""),
("gen_ai.message.id", ""),
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
expected_events = [
{
"name": "gen_ai.user.message",
"attributes": {
"gen_ai.system": "az.ai.agents",
"gen_ai.thread.id": "*",
"gen_ai.event.content": '{"content": "Hello, tell me a joke", "role": "user"}',
},
}
]
events_match = GenAiTraceVerifier().check_span_events(span, expected_events)
assert events_match == True
spans = self.exporter.get_spans_by_name("start_thread_run")
assert len(spans) == 1
span = spans[0]
expected_attributes = [
("gen_ai.system", "az.ai.agents"),
("gen_ai.operation.name", "start_thread_run"),
("server.address", ""),
("gen_ai.thread.id", ""),
("gen_ai.thread.run.id", ""),
("gen_ai.agent.id", ""),
("gen_ai.thread.run.id", ""),
("gen_ai.thread.run.status", "queued"),
("gen_ai.response.model", "gpt-4o-mini"),
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
spans = self.exporter.get_spans_by_name("get_thread_run")
assert len(spans) >= 1
span = spans[-1]
expected_attributes = [
("gen_ai.system", "az.ai.agents"),
("gen_ai.operation.name", "get_thread_run"),
("server.address", ""),
("gen_ai.thread.id", ""),
("gen_ai.thread.run.id", ""),
("gen_ai.agent.id", ""),
("gen_ai.thread.run.status", "completed"),
("gen_ai.response.model", "gpt-4o-mini"),
("gen_ai.usage.input_tokens", "+"),
("gen_ai.usage.output_tokens", "+"),
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
spans = self.exporter.get_spans_by_name("list_messages")
assert len(spans) == 2
span = spans[0]
expected_attributes = [
("gen_ai.system", "az.ai.agents"),
("gen_ai.operation.name", "list_messages"),
("server.address", ""),
("gen_ai.thread.id", ""),
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
expected_events = [
{
"name": "gen_ai.assistant.message",
"timestamp": "*",
"attributes": {
"gen_ai.system": "az.ai.agents",
"gen_ai.thread.id": "*",
"gen_ai.agent.id": "*",
"gen_ai.thread.run.id": "*",
"gen_ai.message.id": "*",
"gen_ai.event.content": '{"content": {"text": {"value": "*"}}, "role": "assistant"}',
},
},
]
events_match = GenAiTraceVerifier().check_span_events(span, expected_events)
assert events_match == True
span = spans[1]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
expected_events = [
{
"name": "gen_ai.user.message",
"attributes": {
"gen_ai.system": "az.ai.agents",
"gen_ai.thread.id": "*",
"gen_ai.message.id": "*",
"gen_ai.event.content": '{"content": {"text": {"value": "Hello, tell me a joke"}}, "role": "user"}',
},
},
]
events_match = GenAiTraceVerifier().check_span_events(span, expected_events)
assert events_match == True
@pytest.mark.usefixtures("instrument_without_content")
@agentClientPreparer()
@recorded_by_proxy_async
async def test_agent_chat_with_tracing_content_recording_disabled(self, **kwargs):
client = self.create_client(**kwargs)
agent = await client.create_agent(model="gpt-4o-mini", name="my-agent", instructions="You are helpful agent")
thread = await client.threads.create()
await client.messages.create(thread_id=thread.id, role="user", content="Hello, tell me a joke")
run = await client.runs.create(thread_id=thread.id, agent_id=agent.id)
while run.status in ["queued", "in_progress", "requires_action"]:
# wait for a second
time.sleep(self._sleep_time())
run = await client.runs.get(thread_id=thread.id, run_id=run.id)
print("Run status:", run.status)
print("Run completed with status:", run.status)
# delete agent and close client
await client.delete_agent(agent.id)
print("Deleted agent")
message_async = client.messages.list(thread_id=thread.id)
messages = [m async for m in message_async]
assert len(messages) > 1
await client.close()
self.exporter.force_flush()
spans = self.exporter.get_spans_by_name("create_agent my-agent")
assert len(spans) == 1
span = spans[0]
expected_attributes = [
("gen_ai.system", "az.ai.agents"),
("gen_ai.operation.name", "create_agent"),
("server.address", ""),
("gen_ai.request.model", "gpt-4o-mini"),
("gen_ai.agent.name", "my-agent"),
("gen_ai.agent.id", ""),
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
expected_events = [
{
"name": "gen_ai.system.message",
"attributes": {
"gen_ai.system": "az.ai.agents",
"gen_ai.event.content": "{}",
},
}
]
events_match = GenAiTraceVerifier().check_span_events(span, expected_events)
assert events_match == True
spans = self.exporter.get_spans_by_name("create_thread")
assert len(spans) == 1
span = spans[0]
expected_attributes = [
("gen_ai.system", "az.ai.agents"),
("gen_ai.operation.name", "create_thread"),
("server.address", ""),
("gen_ai.thread.id", ""),
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
spans = self.exporter.get_spans_by_name("create_message")
assert len(spans) == 1
span = spans[0]
expected_attributes = [
("gen_ai.system", "az.ai.agents"),
("gen_ai.operation.name", "create_message"),
("server.address", ""),
("gen_ai.thread.id", ""),
("gen_ai.message.id", ""),
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
expected_events = [
{
"name": "gen_ai.user.message",
"attributes": {
"gen_ai.system": "az.ai.agents",
"gen_ai.thread.id": "*",
"gen_ai.event.content": '{"role": "user"}',
},
}
]
events_match = GenAiTraceVerifier().check_span_events(span, expected_events)
assert events_match == True
spans = self.exporter.get_spans_by_name("start_thread_run")
assert len(spans) == 1
span = spans[0]
expected_attributes = [
("gen_ai.system", "az.ai.agents"),
("gen_ai.operation.name", "start_thread_run"),
("server.address", ""),
("gen_ai.thread.id", ""),
("gen_ai.thread.run.id", ""),
("gen_ai.agent.id", ""),
("gen_ai.thread.run.id", ""),
("gen_ai.thread.run.status", "queued"),
("gen_ai.response.model", "gpt-4o-mini"),
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
spans = self.exporter.get_spans_by_name("list_messages")
assert len(spans) == 2
span = spans[0]
expected_attributes = [
("gen_ai.system", "az.ai.agents"),
("gen_ai.operation.name", "list_messages"),
("server.address", ""),
("gen_ai.thread.id", ""),
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
expected_events = [
{
"name": "gen_ai.assistant.message",
"timestamp": "*",
"attributes": {
"gen_ai.system": "az.ai.agents",
"gen_ai.thread.id": "*",
"gen_ai.agent.id": "*",
"gen_ai.thread.run.id": "*",
"gen_ai.message.id": "*",
"gen_ai.event.content": '{"role": "assistant"}',
},
},
]
events_match = GenAiTraceVerifier().check_span_events(span, expected_events)
assert events_match == True
span = spans[1]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
expected_events = [
{
"name": "gen_ai.user.message",
"attributes": {
"gen_ai.system": "az.ai.agents",
"gen_ai.thread.id": "*",
"gen_ai.message.id": "*",
"gen_ai.event.content": '{"role": "user"}',
},
},
]
events_match = GenAiTraceVerifier().check_span_events(span, expected_events)
assert events_match == True
def _get_function_toolset(self):
"""Get a function toolset."""
def fetch_weather(location: str) -> str:
"""
Fetches the weather information for the specified location.
:param location (str): The location to fetch weather for.
:return: Weather information as a JSON string.
:rtype: str
"""
# In a real-world scenario, you'd integrate with a weather API.
# Here, we'll mock the response.
mock_weather_data = {"New York": "Sunny", "London": "Cloudy", "Tokyo": "Rainy"}
weather = mock_weather_data.get(location, "Weather data not available for this location.")
weather_json = json.dumps({"weather": weather})
return weather_json
user_functions: Set[Callable[..., Any]] = {
fetch_weather,
}
functions = AsyncFunctionTool(user_functions)
toolset = AsyncToolSet()
toolset.add(functions)
return toolset
@pytest.mark.usefixtures("instrument_with_content")
@agentClientPreparer()
@recorded_by_proxy_async
async def test_agent_streaming_with_toolset_with_tracing_content_recording_enabled(self, **kwargs):
"""Test running functions with streaming and tracing content recording."""
await self._do_test_run_steps_with_toolset_with_tracing_content_recording(
toolset=self._get_function_toolset(),
model="gpt-4o",
use_stream=True,
message="What is the weather in New York?",
recording_enabled=True,
tool_message_attribute_content='{\\"weather\\": \\"Sunny\\"}',
event_contents=[
'{"tool_calls": [{"id": "*", "type": "function", "function": {"name": "fetch_weather", "arguments": {"location": "New York"}}}]}',
'{"content": {"text": {"value": "*"}}, "role": "assistant"}'
],
have_submit_tools=True,
run_step_events=self.get_expected_fn_spans(True),
**kwargs
)
async def _do_test_run_steps_with_toolset_with_tracing_content_recording(
self,
model: str,
message: str,
use_stream: bool,
recording_enabled: bool,
tool_message_attribute_content: str,
event_contents: List[str],
instructions: str = "You are helpful agent",
toolset: Optional[AsyncToolSet] = None,
tool: Optional[Tool] = None,
have_submit_tools=False,
run_step_events: List[List[Dict[str, Any]]] = None,
has_annotations: bool = False,
**kwargs
):
"""The helper method to check the recordings."""
client = self.create_client(**kwargs)
if toolset is None == tool is None:
raise ValueError("Please provide at lease one of toolset or tool, but not both.")
elif toolset is not None:
agent = await client.create_agent(
model=model, name="my-agent", instructions=instructions, toolset=toolset
)
# workaround for https://github.com/Azure/azure-sdk-for-python/issues/40086
client.enable_auto_function_calls(toolset)
elif tool is not None:
agent = await client.create_agent(
model=model, name="my-agent", instructions=instructions,
tools=tool.definitions,
tool_resources=tool.resources,
)
thread = await client.threads.create()
await client.messages.create(thread_id=thread.id, role="user", content=message)
if use_stream:
event_handler = MyEventHandler()
async with await client.runs.stream(thread_id=thread.id, agent_id=agent.id, event_handler=event_handler) as stream:
await stream.until_done()
run_id = event_handler.run_id
else:
run = await client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id, polling_interval=self._sleep_time())
assert run.status != RunStatus.FAILED, run.last_error
run_id = run.id
# delete agent and close client
await client.delete_agent(agent.id)
print("Deleted agent")
messages = [m async for m in client.messages.list(thread_id=thread.id)]
assert len(messages) > 1
steps = [step async for step in client.run_steps.list(thread_id=thread.id, run_id=run_id)]
assert len(steps) >= 1
await client.close()
self.exporter.force_flush()
self._check_spans(
model=model,
recording_enabled=recording_enabled,
instructions=instructions,
message=message,
have_submit_tools=have_submit_tools,
use_stream=use_stream,
tool_message_attribute_content=tool_message_attribute_content,
event_contents=event_contents,
run_step_events=run_step_events,
has_annotations=has_annotations,
)
@pytest.mark.usefixtures("instrument_without_content")
@agentClientPreparer()
@recorded_by_proxy_async
async def test_agent_streaming_with_toolset_with_tracing_content_recording_disabled(self, **kwargs):
await self._do_test_run_steps_with_toolset_with_tracing_content_recording(
toolset=self._get_function_toolset(),
model="gpt-4o",
use_stream=True,
message="What is the weather in New York?",
recording_enabled=False,
tool_message_attribute_content='{\\"weather\\": \\"Sunny\\"}',
event_contents=[
'{"tool_calls": [{"id": "*", "type": "function"}]}',
'{"role": "assistant"}'
],
have_submit_tools=True,
run_step_events=self.get_expected_fn_spans(False),
**kwargs
)
@pytest.mark.usefixtures("instrument_with_content")
@agentClientPreparer()
@recorded_by_proxy_async
async def test_telemetry_steps_with_fn_tool(self, **kwargs):
"""Test running functions with streaming and tracing content recording."""
await self._do_test_run_steps_with_toolset_with_tracing_content_recording(
toolset=self._get_function_toolset(),
model="gpt-4o",
use_stream=False,
message="What is the weather in New York?",
recording_enabled=True,
tool_message_attribute_content='{\\"weather\\": \\"Sunny\\"}',
event_contents=[
'{"tool_calls": [{"id": "*", "type": "function", "function": {"name": "fetch_weather", "arguments": {"location": "New York"}}}]}',
'{"content": {"text": {"value": "*"}}, "role": "assistant"}'
],
have_submit_tools=True,
run_step_events=self.get_expected_fn_spans(True),
**kwargs
)
@pytest.mark.usefixtures("instrument_with_content")
@agentClientPreparer()
@recorded_by_proxy_async
async def test_telemetry_steps_with_openapi_tool(self, **kwargs):
"""Test run steps with OpenAPI."""
weather_asset_file_path = os.path.join(os.path.dirname(__file__), "assets", "weather_openapi.json")
auth = OpenApiAnonymousAuthDetails()
with open(weather_asset_file_path, "r") as f:
openapi_weather = jsonref.load(f)
openapi_tool = OpenApiTool(
name="get_weather",
spec=openapi_weather,
description="Retrieve weather information for a location",
auth=auth,
)
await self._do_test_run_steps_with_toolset_with_tracing_content_recording(
tool=openapi_tool,
model="gpt-4o",
use_stream=False,
message="What is the weather in New York, NY?",
recording_enabled=True,
tool_message_attribute_content='',
event_contents=[],
run_step_events=self.get_expected_openapi_spans(),
**kwargs)
@pytest.mark.usefixtures("instrument_with_content")
@agentClientPreparer()
@recorded_by_proxy_async
async def test_telemetry_steps_with_mcp_tool(self, **kwargs):
"""Test run steps with OpenAPI."""
mcp_tool = McpTool(
server_label="github",
server_url="https://gitmcp.io/Azure/azure-rest-api-specs",
allowed_tools=["search_azure_rest_api_code"], # Optional: specify allowed tools
)
model = "gpt-4o"
instructions = "You are a helpful agent that can use MCP tools to assist users. Use the available MCP tools to answer questions and perform tasks."
recording_enabled = True
message = "Please summarize the Azure REST API specifications Readme"
async with self.create_client(**kwargs, by_endpoint=True) as agents_client:
agent = await agents_client.create_agent(
model=model,
name="my-agent",
instructions=instructions,
tools=mcp_tool.definitions,
)
thread = await agents_client.threads.create()
try:
await agents_client.messages.create(
thread_id=thread.id,
role="user",
content=message,
)
mcp_tool.update_headers("SuperSecret", "123456")
run = await agents_client.runs.create(thread_id=thread.id, agent_id=agent.id, tool_resources=mcp_tool.resources)
was_approved = False
while run.status in [RunStatus.QUEUED, RunStatus.IN_PROGRESS, RunStatus.REQUIRES_ACTION]:
time.sleep(self._sleep_time())
run = await agents_client.runs.get(thread_id=thread.id, run_id=run.id)
if run.status == RunStatus.REQUIRES_ACTION and isinstance(run.required_action, SubmitToolApprovalAction):
tool_calls = run.required_action.submit_tool_approval.tool_calls
assert tool_calls, "No tool calls to approve."
tool_approvals = []
for tool_call in tool_calls:
if isinstance(tool_call, RequiredMcpToolCall):
tool_approvals.append(
ToolApproval(
tool_call_id=tool_call.id,
approve=True,
headers=mcp_tool.headers,
)
)
if tool_approvals:
was_approved = True
await agents_client.runs.submit_tool_outputs(
thread_id=thread.id, run_id=run.id, tool_approvals=tool_approvals
)
assert was_approved, "The run was never approved."
assert run.status != RunStatus.FAILED, run.last_error
is_activity_step_found = False
is_tool_call_step_found = False
async for run_step in agents_client.run_steps.list(thread_id=thread.id, run_id=run.id):
if isinstance(run_step.step_details, RunStepActivityDetails):
is_activity_step_found = True
if isinstance(run_step.step_details, RunStepToolCallDetails):
for tool_call in run_step.step_details.tool_calls:
if isinstance(tool_call, RunStepMcpToolCall):
is_tool_call_step_found = True
break
assert is_activity_step_found, "RunStepMcpToolCall was not found."
assert is_tool_call_step_found, "No RunStepMcpToolCall"
messages = [msg async for msg in agents_client.messages.list(thread_id=thread.id)]
assert len(messages) > 1
finally:
await agents_client.threads.delete(thread.id)
await agents_client.delete_agent(agent.id)
self.exporter.force_flush()
# Check the actual telemetry.
self._check_spans(
model=model,
recording_enabled=recording_enabled,
instructions=instructions,
message=message,
have_submit_tools=True,
use_stream=False,
tool_message_attribute_content="",
event_contents=[],
run_step_events=self.get_expected_mcp_spans(),
)
@pytest.mark.usefixtures("instrument_with_content")
@agentClientPreparer()
@recorded_by_proxy_async
async def test_telemetry_steps_with_deep_research_tool(self, **kwargs):
"""Test running functions with streaming and tracing content recording."""
await self._do_test_run_steps_with_toolset_with_tracing_content_recording(
tool=self._get_deep_research_tool(**kwargs),
model="gpt-4o",
use_stream=False,
instructions="You are a helpful agent that assists in researching scientific topics.",
message="Research the benefits of renewable energy sources. Keep the response brief.",
recording_enabled=True,
tool_message_attribute_content='',
event_contents=[],
have_submit_tools=False,
run_step_events=self.get_expected_deep_research_spans(),
has_annotations=True,
**kwargs
)
class MyEventHandler(AsyncAgentEventHandler):
async def on_message_delta(self, delta: "MessageDeltaChunk") -> None:
for content_part in delta.delta.content:
if isinstance(content_part, MessageDeltaTextContent):
text_value = content_part.text.value if content_part.text else "No text"
print(f"Text delta received: {text_value}")
async def on_thread_message(self, message: "ThreadMessage") -> None:
print(f"ThreadMessage created. ID: {message.id}, Status: {message.status}")
async def on_thread_run(self, run: "ThreadRun") -> None:
print(f"ThreadRun status: {run.status}")
self.run_id = run.id
if run.status == "failed":
print(f"Run failed. Error: {run.last_error}")
async def on_run_step(self, step: "RunStep") -> None:
print(f"RunStep type: {step.type}, Status: {step.status}")
async def on_error(self, data: str) -> None:
print(f"An error occurred. Data: {data}")
async def on_done(self) -> None:
print("Stream completed.")
async def on_unhandled_event(self, event_type: str, event_data: Any) -> None:
print(f"Unhandled Event Type: {event_type}, Data: {event_data}")
|