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 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
|
# pylint: disable=too-many-lines,line-too-long,useless-suppression
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
# cSpell:disable# cSpell:disable
import pytest
import os
import json
import jsonref
import time
from typing import Any, Callable, Dict, Optional, List, Set
from azure.ai.agents.models import (
AgentsResponseFormatMode,
AgentsResponseFormat,
AgentEventHandler,
FunctionTool,
McpTool,
MessageDeltaChunk,
MessageDeltaTextContent,
OpenApiAnonymousAuthDetails,
OpenApiTool,
RequiredMcpToolCall,
RunStatus,
RunStep,
RunStepActivityDetails,
RunStepMcpToolCall,
RunStepToolCallDetails,
SubmitToolApprovalAction,
ThreadMessage,
ThreadRun,
Tool,
ToolApproval,
ToolSet,
)
from azure.ai.agents.telemetry._ai_agents_instrumentor import _AIAgentsInstrumentorPreview
from azure.ai.agents.telemetry import AIAgentsInstrumentor, _utils
from azure.core.settings import settings
from memory_trace_exporter import MemoryTraceExporter
from gen_ai_trace_verifier import GenAiTraceVerifier
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from azure.ai.agents import AgentsClient
from devtools_testutils import (
recorded_by_proxy,
)
from test_agents_client_base import agentClientPreparer
from test_ai_instrumentor_base import TestAiAgentsInstrumentorBase
CONTENT_TRACING_ENV_VARIABLE = "OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"
settings.tracing_implementation = "OpenTelemetry"
_utils._span_impl_type = settings.tracing_implementation()
class TestAiAgentsInstrumentor(TestAiAgentsInstrumentorBase):
"""Tests for AI agents instrumentor."""
@pytest.fixture(scope="function")
def instrument_with_content(self):
os.environ.update({CONTENT_TRACING_ENV_VARIABLE: "True"})
self.setup_telemetry()
yield
self.cleanup()
@pytest.fixture(scope="function")
def instrument_without_content(self):
os.environ.update({CONTENT_TRACING_ENV_VARIABLE: "False"})
self.setup_telemetry()
yield
self.cleanup()
def setup_telemetry(self):
trace._TRACER_PROVIDER = TracerProvider()
self.exporter = MemoryTraceExporter()
span_processor = SimpleSpanProcessor(self.exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
AIAgentsInstrumentor().instrument()
def cleanup(self):
self.exporter.shutdown()
AIAgentsInstrumentor().uninstrument()
trace._TRACER_PROVIDER = None
os.environ.pop(CONTENT_TRACING_ENV_VARIABLE, None)
# helper function: create client and using environment variables
def create_client(self, **kwargs):
# 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
def test_instrumentation(self, **kwargs):
# Make sure code is not instrumented due to a previous test exception
AIAgentsInstrumentor().uninstrument()
exception_caught = False
try:
assert AIAgentsInstrumentor().is_instrumented() == False
AIAgentsInstrumentor().instrument()
assert AIAgentsInstrumentor().is_instrumented() == True
AIAgentsInstrumentor().uninstrument()
assert AIAgentsInstrumentor().is_instrumented() == False
except RuntimeError as e:
exception_caught = True
print(e)
assert exception_caught == False
def test_instrumenting_twice_does_not_cause_exception(self, **kwargs):
# Make sure code is not instrumented due to a previous test exception
AIAgentsInstrumentor().uninstrument()
exception_caught = False
try:
AIAgentsInstrumentor().instrument()
AIAgentsInstrumentor().instrument()
except RuntimeError as e:
exception_caught = True
print(e)
AIAgentsInstrumentor().uninstrument()
assert exception_caught == False
def test_uninstrumenting_uninstrumented_does_not_cause_exception(self, **kwargs):
# Make sure code is not instrumented due to a previous test exception
AIAgentsInstrumentor().uninstrument()
exception_caught = False
try:
AIAgentsInstrumentor().uninstrument()
except RuntimeError as e:
exception_caught = True
print(e)
assert exception_caught == False
def test_uninstrumenting_twice_does_not_cause_exception(self, **kwargs):
# Make sure code is not instrumented due to a previous test exception
AIAgentsInstrumentor().uninstrument()
exception_caught = False
try:
AIAgentsInstrumentor().instrument()
AIAgentsInstrumentor().uninstrument()
AIAgentsInstrumentor().uninstrument()
except RuntimeError as e:
exception_caught = True
print(e)
assert exception_caught == False
@pytest.mark.parametrize(
"env1, env2, expected",
[
(None, None, False),
(None, False, False),
(None, True, True),
(False, None, False),
(False, False, False),
(False, True, False),
(True, None, True),
(True, False, False),
(True, True, True),
],
)
def test_content_recording_enabled_with_old_and_new_environment_variables(
self, env1: Optional[bool], env2: Optional[bool], expected: bool
):
"""
Test content recording enablement with both old and new environment variables.
This test verifies the behavior of content recording when both the current
and legacy environment variables are set to different combinations of values.
The method tests all possible combinations of None, True, and False for both
environment variables to ensure backward compatibility and proper precedence.
Args:
env1: Value for the current content tracing environment variable.
Can be None (unset), True, or False.
env2: Value for the old/legacy content tracing environment variable.
Can be None (unset), True, or False.
expected: The expected result of is_content_recording_enabled() given
the environment variable combination.
The test ensures that only if one or both of the environment variables are
defined and set to "true" content recording is enabled.
"""
OLD_CONTENT_TRACING_ENV_VARIABLE = "AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED" # Deprecated, undocumented.
def set_env_var(var_name, value):
if value is None:
os.environ.pop(var_name, None)
else:
os.environ[var_name] = "true" if value else "false"
set_env_var(CONTENT_TRACING_ENV_VARIABLE, env1)
set_env_var(OLD_CONTENT_TRACING_ENV_VARIABLE, env2)
self.setup_telemetry()
try:
assert AIAgentsInstrumentor().is_content_recording_enabled() == expected
finally:
self.cleanup() # This also undefines CONTENT_TRACING_ENV_VARIABLE
os.environ.pop(OLD_CONTENT_TRACING_ENV_VARIABLE, None)
@pytest.mark.usefixtures("instrument_with_content")
@agentClientPreparer()
@recorded_by_proxy
def test_agent_chat_with_tracing_content_recording_enabled(self, **kwargs):
assert True == AIAgentsInstrumentor().is_content_recording_enabled()
assert True == AIAgentsInstrumentor().is_instrumented()
client = self.create_client(**kwargs)
agent = client.create_agent(model="gpt-4o-mini", name="my-agent", instructions="You are helpful agent")
thread = client.threads.create()
client.messages.create(thread_id=thread.id, role="user", content="Hello, tell me a joke")
run = 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 = 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
client.delete_agent(agent.id)
print("Deleted agent")
messages = list(client.messages.list(thread_id=thread.id))
assert len(messages) > 1
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.message.status": "completed", - there is not status over-the wire
"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
def test_agent_chat_with_tracing_content_recording_disabled(self, **kwargs):
assert False == AIAgentsInstrumentor().is_content_recording_enabled()
client = self.create_client(**kwargs)
agent = client.create_agent(model="gpt-4o-mini", name="my-agent", instructions="You are helpful agent")
thread = client.threads.create()
client.messages.create(thread_id=thread.id, role="user", content="Hello, tell me a joke")
run = 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 = 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
client.delete_agent(agent.id)
print("Deleted agent")
messages = list(client.messages.list(thread_id=thread.id))
assert len(messages) > 1
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]
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"}',
},
},
]
attributes_match = GenAiTraceVerifier().check_span_attributes(span, expected_attributes)
assert attributes_match == True
events_match = GenAiTraceVerifier().check_span_events(span, expected_events)
assert events_match == True
@pytest.mark.usefixtures("instrument_with_content")
@agentClientPreparer()
@recorded_by_proxy
def test_agent_streaming_with_toolset_with_tracing_content_recording_enabled(self, **kwargs):
self._do_test_run_steps_with_toolset_with_tracing_content_recording(
expected_event_content='{"tool_calls": [{"id": "*", "type": "function", "function": {"name": "fetch_weather", "arguments": {"location": "New York"}}}]}',
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
)
@pytest.mark.usefixtures("instrument_with_content")
@agentClientPreparer()
@recorded_by_proxy
def test_agent_streaming_with_toolset_with_tracing_content_recording_enabled_unicode(self, **kwargs):
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", "Sofia": "Дъждовно"}
weather = mock_weather_data.get(location, f"Weather data not available for this location: {location}")
weather_json = json.dumps({"weather": weather}, ensure_ascii=False)
return weather_json
user_functions: Set[Callable[..., Any]] = {
fetch_weather,
}
functions = FunctionTool(user_functions)
toolset = ToolSet()
toolset.add(functions)
client = self.create_client(**kwargs)
agent = client.create_agent(
model="gpt-4o",
name="my-agent",
instructions="You are helpful agent. Translate user message to English before executing tools.",
toolset=toolset,
)
# workaround for https://github.com/Azure/azure-sdk-for-python/issues/40086
client.enable_auto_function_calls(toolset)
thread = client.threads.create()
client.messages.create(thread_id=thread.id, role="user", content="Времето в Софи�?")
with client.runs.stream(thread_id=thread.id, agent_id=agent.id, event_handler=MyEventHandler()) as stream:
stream.until_done()
# delete agent and close client
client.delete_agent(agent.id)
print("Deleted agent")
messages = list(client.messages.list(thread_id=thread.id))
assert len(messages) > 1
client.close()
self.exporter.force_flush()
spans = self.exporter.get_spans_by_name("create_message")
assert len(spans) == 1
span = spans[0]
expected_events = [
{
"name": "gen_ai.user.message",
"attributes": {
"gen_ai.system": "az.ai.agents",
"gen_ai.thread.id": "*",
"gen_ai.event.content": '{"content": "Времето в Софи�?", "role": "user"}',
},
}
]
events_match = GenAiTraceVerifier().check_span_events(span, expected_events)
assert events_match == True
spans = self.exporter.get_spans_by_name("process_thread_run")
assert len(spans) == 1
span = spans[0]
expected_events = [
{
"name": "gen_ai.tool.message",
"attributes": {
"gen_ai.event.content": '{"content": "{\\"weather\\": \\"Дъждовно\\"}", "id": "*"}'
},
},
{
"name": "gen_ai.assistant.message",
"attributes": {
"gen_ai.system": "az.ai.agents",
"gen_ai.thread.id": "*",
"gen_ai.agent.id": "*",
"gen_ai.thread.run.id": "*",
"gen_ai.message.status": "completed",
"gen_ai.run_step.start.timestamp": "*",
"gen_ai.run_step.end.timestamp": "*",
"gen_ai.usage.input_tokens": "+",
"gen_ai.usage.output_tokens": "+",
"gen_ai.event.content": '{"tool_calls": [{"id": "*", "type": "function", "function": {"name": "fetch_weather", "arguments": {"location": "Sofia"}}}]}',
},
},
{
"name": "gen_ai.assistant.message",
"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.message.status": "*", # In some cases the message may be "in progress"
"gen_ai.usage.input_tokens": "+",
"gen_ai.usage.output_tokens": "+",
"gen_ai.event.content": '{"content": {"text": {"value": "*"}}, "role": "assistant"}',
},
},
]
events_match = GenAiTraceVerifier().check_span_events(span, expected_events)
assert events_match == True
@pytest.mark.usefixtures("instrument_without_content")
@agentClientPreparer()
@recorded_by_proxy
def test_agent_streaming_with_toolset_with_tracing_content_recording_disabled(self, **kwargs):
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
)
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 = FunctionTool(user_functions)
toolset = ToolSet()
toolset.add(functions)
return toolset
@pytest.mark.usefixtures("instrument_with_content")
@agentClientPreparer()
@recorded_by_proxy
def test_agent_streaming_run_steps_with_toolset_with_tracing_content_recording_enabled(self, **kwargs):
"""Test running functions with streaming and tracing content recording."""
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
)
def _do_test_run_steps_with_toolset_with_tracing_content_recording(
self,
model: str,
use_stream: bool,
message: str,
recording_enabled: bool,
tool_message_attribute_content: str,
event_contents: List[str],
instructions: str = "You are helpful agent",
test_run_steps=True,
toolset: Optional[ToolSet] = None,
tool: Optional[Tool] = None,
have_submit_tools: bool = False,
run_step_events: List[List[Dict[str, Any]]] = None,
has_annotations: bool = False,
**kwargs
) -> None:
"""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 = 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 = client.create_agent(
model=model, name="my-agent", instructions=instructions,
tools=tool.definitions,
tool_resources=tool.resources,
)
thread = client.threads.create()
client.messages.create(thread_id=thread.id, role="user", content=message)
if use_stream:
event_handler = MyEventHandler()
with client.runs.stream(thread_id=thread.id, agent_id=agent.id, event_handler=event_handler) as stream:
stream.until_done()
run_id = event_handler.run_id
else:
run = 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
client.delete_agent(agent.id)
print("Deleted agent")
messages = list(client.messages.list(thread_id=thread.id))
assert len(messages) > 1
if test_run_steps:
steps = list(client.run_steps.list(thread_id=thread.id, run_id=run_id))
assert len(steps) >= 1
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_with_content")
@agentClientPreparer()
@recorded_by_proxy
def test_telemetry_steps_with_fn_tool(self, **kwargs):
"""Test running functions with streaming and tracing content recording."""
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
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,
)
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
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"
with self.create_client(**kwargs, by_endpoint=True) as agents_client:
agent = agents_client.create_agent(
model=model,
name="my-agent",
instructions=instructions,
tools=mcp_tool.definitions,
)
thread = agents_client.threads.create()
try:
agents_client.messages.create(
thread_id=thread.id,
role="user",
content=message,
)
mcp_tool.update_headers("SuperSecret", "123456")
run = 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 = 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
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
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 = list(agents_client.messages.list(thread_id=thread.id))
assert len(messages) > 1
finally:
agents_client.threads.delete(thread.id)
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
def test_telemetry_steps_with_deep_research_tool(self, **kwargs):
"""Test running functions with streaming and tracing content recording."""
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(AgentEventHandler):
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}")
def on_thread_message(self, message: "ThreadMessage") -> None:
print(f"ThreadMessage created. ID: {message.id}, Status: {message.status}")
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}")
def on_run_step(self, step: "RunStep") -> None:
print(f"RunStep type: {step.type}, Status: {step.status}")
def on_error(self, data: str) -> None:
print(f"An error occurred. Data: {data}")
def on_done(self) -> None:
print("Stream completed.")
def on_unhandled_event(self, event_type: str, event_data: Any) -> None:
print(f"Unhandled Event Type: {event_type}, Data: {event_data}")
|