Skip to content

Commit b8789d4

Browse files
authored
Fix missing await in telemetry wrapper methods for async Agent calls (#43606)
1 parent 1edf91c commit b8789d4

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

sdk/ai/azure-ai-agents/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
# Release History
44

5+
## 1.2.0b6 (2025-10-24)
6+
7+
### Bugs Fixed
8+
9+
- Fix missing `await` in calls to asynchronous Agent methods, when `AIAgentsInstrumentor().instrument()`
10+
is called but no tracer is configured
11+
512
## 1.2.0b5 (2025-09-29)
613

714
### Features Added

sdk/ai/azure-ai-agents/azure/ai/agents/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
77
# --------------------------------------------------------------------------
88

9-
VERSION = "1.2.0b5"
9+
VERSION = "1.2.0b6"

sdk/ai/azure-ai-agents/azure/ai/agents/telemetry/_ai_agents_instrumentor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ def _trace_async_function(
16171617
async def inner(*args, **kwargs): # pylint: disable=R0911
16181618
span_impl_type = settings.tracing_implementation() # pylint: disable=E1102
16191619
if span_impl_type is None:
1620-
return function(*args, **kwargs)
1620+
return await function(*args, **kwargs)
16211621

16221622
class_function_name = function.__qualname__
16231623

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
"""
7+
DESCRIPTION:
8+
This sample demonstrates how to use basic agent operations from
9+
the Azure Agents service using an asynchronous client with tracing to console.
10+
11+
USAGE:
12+
python sample_agents_basics_with_console_tracing_async.py
13+
14+
Before running the sample:
15+
16+
pip install azure-ai-projects azure-ai-agents azure-identity opentelemetry-sdk azure-core-tracing-opentelemetry aiohttp
17+
18+
If you want to export telemetry to OTLP endpoint (such as Aspire dashboard
19+
https://learn.microsoft.com/dotnet/aspire/fundamentals/dashboard/standalone?tabs=bash)
20+
install:
21+
22+
pip install azure-ai-projects opentelemetry-exporter-otlp-proto-grpc
23+
24+
Set these environment variables with your own values:
25+
1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
26+
page of your Azure AI Foundry portal.
27+
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
28+
the "Models + endpoints" tab in your Azure AI Foundry project.
29+
3) OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT - Optional. Set to `true` to trace the content of chat
30+
messages, which may contain personal data. False by default.
31+
"""
32+
33+
import asyncio
34+
import os
35+
from azure.ai.projects.aio import AIProjectClient
36+
from azure.ai.agents.models import ListSortOrder
37+
from azure.identity.aio import DefaultAzureCredential
38+
39+
from azure.core.settings import settings
40+
settings.tracing_implementation = "opentelemetry"
41+
42+
# Install opentelemetry with command "pip install azure-ai-projects opentelemetry-sdk".
43+
from opentelemetry import trace
44+
from opentelemetry.sdk.trace import TracerProvider
45+
from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter
46+
47+
# Setup tracing to console
48+
# Requires opentelemetry-sdk
49+
span_exporter = ConsoleSpanExporter()
50+
tracer_provider = TracerProvider()
51+
tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter))
52+
trace.set_tracer_provider(tracer_provider)
53+
tracer = trace.get_tracer(__name__)
54+
55+
from azure.ai.agents.telemetry import AIAgentsInstrumentor
56+
AIAgentsInstrumentor().instrument()
57+
58+
59+
async def main() -> None:
60+
61+
credential = DefaultAzureCredential()
62+
63+
async with credential:
64+
65+
project_client = AIProjectClient(
66+
endpoint=os.environ["PROJECT_ENDPOINT"],
67+
credential=credential,
68+
)
69+
70+
scenario = os.path.basename(__file__)
71+
with tracer.start_as_current_span(scenario):
72+
73+
async with project_client:
74+
75+
agent = await project_client.agents.create_agent(
76+
model=os.environ["MODEL_DEPLOYMENT_NAME"], name="my-agent", instructions="You are helpful agent"
77+
)
78+
print(f"Created agent, agent ID: {agent.id}")
79+
80+
thread = await project_client.agents.threads.create()
81+
print(f"Created thread, thread ID: {thread.id}")
82+
83+
message = await project_client.agents.messages.create(thread_id=thread.id, role="user", content="Hello, tell me a joke")
84+
print(f"Created message, message ID: {message.id}")
85+
86+
run = await project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
87+
print(f"Run completed with status: {run.status}")
88+
89+
await project_client.agents.delete_agent(agent.id)
90+
print("Deleted agent")
91+
92+
messages = project_client.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
93+
async for msg in messages:
94+
if msg.text_messages:
95+
last_text = msg.text_messages[-1]
96+
print(f"{msg.role}: {last_text.text.value}")
97+
98+
99+
if __name__ == "__main__":
100+
asyncio.run(main())

0 commit comments

Comments
 (0)