-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Open
Labels
Hosted Agentssdk/agentserver/*sdk/agentserver/*Service AttentionWorkflow: This issue is responsible by Azure service team.Workflow: This issue is responsible by Azure service team.customer-reportedIssues that are reported by GitHub users external to the Azure organization.Issues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as thatThe issue doesn't require a change to the product in order to be resolved. Most issues start as that
Description
- Package Name: azure-ai-agentserver-core
- Package Version: 1.0.0b1 (or check your installed version with
pip show azure-ai-agentserver-core) - Operating System: macOS (also reproducible on other platforms)
- Python Version: 3.13
Describe the bug
When using streaming responses with azure-ai-agentserver-core, OpenTelemetry raises ValueError: <Token...> was created in a different Context errors during context detach. This is a known issue with OpenTelemetry when async generators aren't properly closed using contextlib.aclosing().
The error occurs in azure/ai/agentserver/core/server/base.py in the gen_async() function where the streaming response iterates over an async generator without wrapping it with aclosing().
To Reproduce
- Create an agent using
agent_frameworkwith streaming enabled
import asyncio
from dotenv import load_dotenv
load_dotenv(override=True)
from agent_framework.azure import AzureAIAgentClient
from azure.ai.agentserver.agentframework import from_agent_framework
from azure.identity.aio import DefaultAzureCredential
async def main():
async with (
DefaultAzureCredential() as credential,
AzureAIAgentClient(credential=credential) as client,
):
agent = client.create_agent(
name="SimpleAgent",
instructions="You are a helpful assistant. Just respond briefly to any message.",
)
print("Server running on http://localhost:8088")
server = from_agent_framework(agent, credential)
await server.run_async()
if __name__ == "__main__":
asyncio.run(main())- Host it using
azure.ai.agentserver.agentframework.from_agent_framework() - Send requests that trigger streaming responses. curl -X POST http://localhost:8088/responses -H "Content-Type: application/json" -d '{"stream": true, "input": "Hello"}'
- Observe the error in logs:```
ValueError: <Token var=<ContextVar name='current_context' default={} at 0x...> at 0x...> was created in a different Context
Failed to detach context
**Expected behavior**
No OpenTelemetry context detach errors during streaming responses.
**Screenshots**
N/A
**Additional context**
This is a known issue with OpenTelemetry and async generators, fixed in other projects:
- **Root cause**: https://github.com/open-telemetry/opentelemetry-python/issues/2606
- **Fix pattern**: https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3938
- **Documentation**: https://logfire.pydantic.dev/docs/reference/advanced/generators/
**Suggested fix** in `azure/ai/agentserver/core/server/base.py`:
The async generator `resp` should be wrapped with `contextlib.aclosing()`:
```python
from contextlib import aclosing
async def gen_async():
ctx = TraceContextTextMapPropagator().extract(carrier=context_carrier)
token = otel_context.attach(ctx)
error_sent = False
try:
yield _event_to_sse_chunk(first_event)
async with aclosing(resp) as stream: # <-- ADD THIS
async for event in stream:
yield _event_to_sse_chunk(event)
except Exception as e:
# ... error handling
finally:
otel_context.detach(token)
# ...
Similarly for the sync generator case using contextlib.closing().
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Hosted Agentssdk/agentserver/*sdk/agentserver/*Service AttentionWorkflow: This issue is responsible by Azure service team.Workflow: This issue is responsible by Azure service team.customer-reportedIssues that are reported by GitHub users external to the Azure organization.Issues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as thatThe issue doesn't require a change to the product in order to be resolved. Most issues start as that