core Package
Azure AI AgentServerHost core framework.
Provides the <xref:AgentServerHost> base class and shared utilities for building Azure AI Hosted Agent containers.
Public API::
from azure.ai.agentserver.core import (
AgentConfig,
AgentServerHost,
configure_observability,
create_error_response,
detach_context,
end_span,
flush_spans,
record_error,
set_current_span,
trace_stream,
)
Classes
| AgentConfig |
Resolved configuration for an agent server host. All values are populated from environment variables at creation time.
Access via
|
| AgentServerHost |
Agent server host framework for Azure AI Hosted Agent containers. A <xref:starlette.applications.Starlette> subclass providing the protocol-agnostic infrastructure required by all hosted agent containers:
Protocol packages subclass this host to add their own routes:
For multi-protocol agents, compose via cooperative inheritance:
|
| InboundRequestLoggingMiddleware |
Pure-ASGI middleware that logs inbound HTTP requests. Unlike Wired automatically by AgentServerHost so that all protocol hosts (responses, invocations, etc.) get consistent inbound logging. |
| RequestIdMiddleware |
Pure-ASGI middleware that sets The resolved request ID is stored in Unlike |
Functions
build_server_version
Build a standard version segment for the x-platform-server header.
Format: {sdk_name}/{version} (python/{major}.{minor})
Protocol packages call this during host initialisation and pass the result to register_server_version.
build_server_version(sdk_name: str, version: str) -> str
Parameters
| Name | Description |
|---|---|
|
sdk_name
Required
|
The SDK identifier
(e.g., |
|
version
Required
|
The package version string (e.g., |
Returns
| Type | Description |
|---|---|
|
A formatted version string. |
Exceptions
| Type | Description |
|---|---|
|
If sdk_name or version is empty. |
configure_observability
Default observability setup: console logging + tracing/OTel export.
Attaches a formatted StreamHandler to the root logger so that
both SDK and user logging.info() calls are visible on the console.
Then configures OpenTelemetry tracing and log export (Azure Monitor
and/or OTLP) when a connection string or OTLP endpoint is available.
Pass this function (or a custom replacement) as the
configure_observability parameter of AgentServerHost.
Pass None to disable all SDK-managed observability setup.
configure_observability(*, connection_string: str | None = None, log_level: str | None = None) -> None
Keyword-Only Parameters
| Name | Description |
|---|---|
|
connection_string
|
Application Insights connection string. Default value: None
|
|
log_level
|
Log level name (e.g. Default value: None
|
create_error_response
Build a JSONResponse with the standard error envelope.
create_error_response(code: str, message: str, *, status_code: int, error_type: str | None = None, details: list[dict[str, Any]] | None = None, headers: dict[str, str] | None = None) -> JSONResponse
Parameters
| Name | Description |
|---|---|
|
code
Required
|
Machine-readable error code (e.g. |
|
message
Required
|
Human-readable error message. |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
status_code
|
HTTP status code for the response. |
|
error_type
|
Optional error type classification string. When
provided, included as Default value: None
|
|
details
|
Child error objects, each with at least Default value: None
|
|
headers
|
Extra HTTP headers to include on the response. Default value: None
|
Returns
| Type | Description |
|---|---|
|
<xref:JSONResponse>
|
A ready-to-send JSON error response. |
detach_context
Detach a context previously attached by set_current_span.
Best-effort no-op when token is None or when the token is no
longer the current OpenTelemetry context.
detach_context(token: Any) -> None
Parameters
| Name | Description |
|---|---|
|
token
Required
|
The token returned by set_current_span. |
end_span
End a span, optionally recording an error first.
No-op when span is None.
end_span(span: Any, exc: BaseException | None = None) -> None
Parameters
| Name | Description |
|---|---|
|
span
Required
|
The OTel span to end, or |
|
exc
|
Optional exception to record before ending. Default value: None
|
flush_spans
Flush all pending spans from the TracerProvider.
The BatchSpanProcessor buffers spans and exports them on a timer
(default 5 seconds). In hosted sandbox environments the platform may
suspend the process immediately after an HTTP response is sent, before
the batch timer fires. Calling this function after ending the request
span ensures that all child spans — including short-lived ones created
by third-party tracers (e.g. LangChain/LangGraph per-node spans) — are
exported before the sandbox is frozen.
No-op when the OTel SDK is not installed or the provider does not
support force_flush.
flush_spans(timeout_millis: int = 5000) -> None
Parameters
| Name | Description |
|---|---|
|
timeout_millis
|
Maximum time to wait for the flush, in milliseconds. Defaults to 5000 (5 seconds). Default value: 5000
|
record_error
Record an exception and ERROR status on a span.
Sets error.type and otel.status.description per OTel
semantic conventions.
record_error(span: Any, exc: BaseException) -> None
Parameters
| Name | Description |
|---|---|
|
span
Required
|
The OTel span, or |
|
exc
Required
|
The exception to record. |
set_current_span
Set a span as the current span in the OTel context.
This makes span the active parent for any child spans created by downstream code (e.g. framework handlers). Without this, spans created inside the handler would become siblings rather than children of span.
Returns a context token that must be passed to
detach_context when the scope ends. No-op when span
is None.
set_current_span(span: Any) -> Any
Parameters
| Name | Description |
|---|---|
|
span
Required
|
The OTel span to make current, or None. |
Returns
| Type | Description |
|---|---|
|
A context token, or None. |
trace_stream
Wrap a streaming body so the span covers the full transmission.
Yields chunks unchanged. Ends the span when the iterator is exhausted or raises an exception.
async trace_stream(iterator: AsyncIterable[str | bytes | memoryview], span: Any) -> AsyncIterator[str | bytes | memoryview]
Parameters
| Name | Description |
|---|---|
|
iterator
Required
|
The async iterable to wrap. |
|
span
Required
|
The OTel span to end on completion, or |
Returns
| Type | Description |
|---|---|
|
An async iterator yielding chunks unchanged. |