|
| 1 | +# Guide for migrating to the new azure-ai-agentserver package architecture |
| 2 | + |
| 3 | +This guide helps you migrate from `azure-ai-agentserver-core` 1.x to the new three-package |
| 4 | +architecture introduced in `azure-ai-agentserver-core` 2.0.0b1. |
| 5 | + |
| 6 | +## Table of contents |
| 7 | + |
| 8 | +- [Migration benefits](#migration-benefits) |
| 9 | +- [Package changes](#package-changes) |
| 10 | +- [API changes](#api-changes) |
| 11 | + - [Handler registration](#handler-registration) |
| 12 | + - [Streaming handler](#streaming-handler) |
| 13 | + - [Server startup](#server-startup) |
| 14 | + - [Tracing](#tracing) |
| 15 | + - [Logging](#logging) |
| 16 | + - [Error responses](#error-responses) |
| 17 | +- [Import changes](#import-changes) |
| 18 | +- [Multi-protocol composition](#multi-protocol-composition) |
| 19 | +- [Additional information](#additional-information) |
| 20 | + |
| 21 | +## Migration benefits |
| 22 | + |
| 23 | +The new package architecture provides: |
| 24 | + |
| 25 | +- **Separation of concerns** — protocol implementations (Responses API, Invocations) are in |
| 26 | + dedicated packages rather than bundled into a monolithic Core package. |
| 27 | +- **Dramatically simpler API** — the old approach required manually constructing SSE events, |
| 28 | + tracking sequence numbers, and building response objects. The new API provides decorator-based |
| 29 | + handler registration with builder methods that handle all of this automatically. |
| 30 | +- **Type-safe event builders** — `ResponseEventStream` and its convenience generators manage |
| 31 | + event sequencing, output indices, and content indices. You cannot accidentally emit events in |
| 32 | + the wrong order. |
| 33 | +- **Built-in convenience methods** — common patterns like "emit a text message" or "stream |
| 34 | + tokens" are one-liners via `ResponseEventStream` generators or `TextResponse`. |
| 35 | +- **Zero-config startup** — `app.run()` replaces manual server configuration with sensible |
| 36 | + defaults including OpenTelemetry, health endpoints, and user-agent headers. |
| 37 | +- **Multi-protocol support** — a single server can host both Responses and Invocations endpoints |
| 38 | + via cooperative mixin inheritance. |
| 39 | + |
| 40 | +## Package changes |
| 41 | + |
| 42 | +| Before | After | Notes | |
| 43 | +|--------|-------|-------| |
| 44 | +| `azure-ai-agentserver-core` 1.x | `azure-ai-agentserver-core` 2.x | Stripped to hosting foundation only | |
| 45 | +| _(bundled in core)_ | `azure-ai-agentserver-responses` 1.x | New — Responses API protocol | |
| 46 | +| _(bundled in core)_ | `azure-ai-agentserver-invocations` 1.x | New — Invocations protocol | |
| 47 | + |
| 48 | +Update your dependencies: |
| 49 | + |
| 50 | +```bash |
| 51 | +# Install the protocol package you need (transitively brings in core 2.x) |
| 52 | +pip install azure-ai-agentserver-responses |
| 53 | + |
| 54 | +# If you also need the Invocations protocol: |
| 55 | +pip install azure-ai-agentserver-invocations |
| 56 | +``` |
| 57 | + |
| 58 | +> **Note:** Both `azure-ai-agentserver-responses` and `azure-ai-agentserver-invocations` |
| 59 | +> depend on `azure-ai-agentserver-core`, so you do not need to install Core separately. |
| 60 | +
|
| 61 | +## API changes |
| 62 | + |
| 63 | +### Handler registration |
| 64 | + |
| 65 | +**Before (1.x):** |
| 66 | + |
| 67 | +```python |
| 68 | +from azure.ai.agentserver.core import FoundryCBAgent, AgentRunContext |
| 69 | + |
| 70 | +class MyAgent(FoundryCBAgent): |
| 71 | + def register_routes(self): |
| 72 | + self.app.add_route("/responses", self.handle_create, methods=["POST"]) |
| 73 | + |
| 74 | + async def handle_create(self, request): |
| 75 | + # Manually parse request, build SSE events, track sequence numbers |
| 76 | + ... |
| 77 | +``` |
| 78 | + |
| 79 | +**After (2.x) — Responses protocol:** |
| 80 | + |
| 81 | +```python |
| 82 | +from azure.ai.agentserver.responses import ( |
| 83 | + CreateResponse, |
| 84 | + ResponseContext, |
| 85 | + ResponsesAgentServerHost, |
| 86 | + TextResponse, |
| 87 | +) |
| 88 | + |
| 89 | +app = ResponsesAgentServerHost() |
| 90 | + |
| 91 | +@app.response_handler |
| 92 | +async def handle_create(request: CreateResponse, context: ResponseContext, cancellation_signal): |
| 93 | + input_text = await context.get_input_text() |
| 94 | + return TextResponse(context, request, text=f"Echo: {input_text}") |
| 95 | + |
| 96 | +app.run() |
| 97 | +``` |
| 98 | + |
| 99 | +**After (2.x) — Invocations protocol:** |
| 100 | + |
| 101 | +```python |
| 102 | +from starlette.requests import Request |
| 103 | +from starlette.responses import JSONResponse, Response |
| 104 | +from azure.ai.agentserver.invocations import InvocationAgentServerHost |
| 105 | + |
| 106 | +app = InvocationAgentServerHost() |
| 107 | + |
| 108 | +@app.invoke_handler |
| 109 | +async def handle(request: Request) -> Response: |
| 110 | + data = await request.json() |
| 111 | + return JSONResponse({"greeting": f"Hello, {data['name']}!"}) |
| 112 | + |
| 113 | +app.run() |
| 114 | +``` |
| 115 | + |
| 116 | +### Streaming handler |
| 117 | + |
| 118 | +**Before (1.x):** |
| 119 | + |
| 120 | +```python |
| 121 | +# Manually construct every SSE event, track sequence numbers and indices |
| 122 | +seq = 0 |
| 123 | +yield {"type": "response.created", "sequence_number": seq, "response": {...}} |
| 124 | +seq += 1 |
| 125 | +yield {"type": "response.output_item.added", "sequence_number": seq, ...} |
| 126 | +seq += 1 |
| 127 | +# ... many more events with manual index tracking |
| 128 | +``` |
| 129 | + |
| 130 | +**After (2.x):** |
| 131 | + |
| 132 | +```python |
| 133 | +from azure.ai.agentserver.responses import ( |
| 134 | + CreateResponse, |
| 135 | + ResponseContext, |
| 136 | + ResponseEventStream, |
| 137 | + ResponsesAgentServerHost, |
| 138 | +) |
| 139 | + |
| 140 | +app = ResponsesAgentServerHost() |
| 141 | + |
| 142 | +@app.response_handler |
| 143 | +async def handle_create(request: CreateResponse, context: ResponseContext, cancellation_signal): |
| 144 | + stream = ResponseEventStream(context, request) |
| 145 | + stream.emit_created() |
| 146 | + |
| 147 | + # All inner events (output_item.added, content_part.added, deltas, done events) |
| 148 | + # are emitted automatically with correct sequence numbers and indices |
| 149 | + async for token in get_tokens(): |
| 150 | + yield from stream.output_item_message(token) |
| 151 | + |
| 152 | + stream.emit_completed() |
| 153 | +``` |
| 154 | + |
| 155 | +Or, for the simplest case: |
| 156 | + |
| 157 | +```python |
| 158 | +return TextResponse(context, request, text="Hello!") |
| 159 | +``` |
| 160 | + |
| 161 | +### Server startup |
| 162 | + |
| 163 | +**Before (1.x):** |
| 164 | + |
| 165 | +```python |
| 166 | +agent = MyAgent() |
| 167 | +agent.run() # or manual uvicorn/hypercorn setup |
| 168 | +``` |
| 169 | + |
| 170 | +**After (2.x):** |
| 171 | + |
| 172 | +```python |
| 173 | +# Responses protocol |
| 174 | +app = ResponsesAgentServerHost() |
| 175 | + |
| 176 | +@app.response_handler |
| 177 | +async def handle(request, context, cancellation_signal): |
| 178 | + ... |
| 179 | + |
| 180 | +app.run() # Built-in Hypercorn server with OpenTelemetry, health endpoint, graceful shutdown |
| 181 | +``` |
| 182 | + |
| 183 | +Configuration is via constructor parameters: |
| 184 | + |
| 185 | +```python |
| 186 | +app = ResponsesAgentServerHost( |
| 187 | + log_level="DEBUG", # Console log level |
| 188 | + graceful_shutdown_timeout=60, # Drain period in seconds |
| 189 | + applicationinsights_connection_string="InstrumentationKey=...", # Azure Monitor |
| 190 | + configure_observability=None, # Disable SDK logging/tracing setup |
| 191 | +) |
| 192 | +``` |
| 193 | + |
| 194 | +## Import changes |
| 195 | + |
| 196 | +| Before (1.x) | After (2.x) | |
| 197 | +|---------------|-------------| |
| 198 | +| `from azure.ai.agentserver.core import FoundryCBAgent` | `from azure.ai.agentserver.core import AgentServerHost` | |
| 199 | +| `from azure.ai.agentserver.core import AgentRunContext` | `from azure.ai.agentserver.responses import ResponseContext` | |
| 200 | +| _(n/a)_ | `from azure.ai.agentserver.responses import ResponsesAgentServerHost` | |
| 201 | +| _(n/a)_ | `from azure.ai.agentserver.responses import TextResponse` | |
| 202 | +| _(n/a)_ | `from azure.ai.agentserver.responses import ResponseEventStream` | |
| 203 | +| _(n/a)_ | `from azure.ai.agentserver.invocations import InvocationAgentServerHost` | |
| 204 | + |
| 205 | +## Multi-protocol composition |
| 206 | + |
| 207 | +A single server can host both Responses and Invocations endpoints using cooperative |
| 208 | +mixin inheritance: |
| 209 | + |
| 210 | +```python |
| 211 | +from azure.ai.agentserver.invocations import InvocationAgentServerHost |
| 212 | +from azure.ai.agentserver.responses import ResponsesAgentServerHost |
| 213 | + |
| 214 | +class MyHost(InvocationAgentServerHost, ResponsesAgentServerHost): |
| 215 | + pass |
| 216 | + |
| 217 | +app = MyHost() |
| 218 | + |
| 219 | +@app.response_handler |
| 220 | +async def handle_responses(request, context, cancellation_signal): |
| 221 | + return TextResponse(context, request, text="Hello from Responses!") |
| 222 | + |
| 223 | +@app.invoke_handler |
| 224 | +async def handle_invocations(request): |
| 225 | + return JSONResponse({"hello": "from Invocations!"}) |
| 226 | + |
| 227 | +app.run() |
| 228 | +# Serves both POST /responses and POST /invocations |
| 229 | +``` |
| 230 | + |
| 231 | +## Additional information |
| 232 | + |
| 233 | +- [azure-ai-agentserver-core README](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/agentserver/azure-ai-agentserver-core/README.md) |
| 234 | +- [azure-ai-agentserver-responses README](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/agentserver/azure-ai-agentserver-responses/README.md) |
| 235 | +- [azure-ai-agentserver-invocations README](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/agentserver/azure-ai-agentserver-invocations/README.md) |
| 236 | +- [Responses samples](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/agentserver/azure-ai-agentserver-responses/samples) |
| 237 | +- [Invocations samples](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/agentserver/azure-ai-agentserver-invocations/samples) |
0 commit comments