Azure AI Agents Client Library
The Azure AI Agents Client Library for Python provides tools for building AI agents that can interact with users and other services on the Azure platform. It allows developers to create, configure, and manage conversational AI experiences. The current version is 1.1.0, and it follows the Azure SDK release cadence, with frequent beta updates and less frequent, but regular, stable releases.
Warnings
- breaking The API for `azure-ai-agents` underwent significant changes between its initial beta (`1.0.0b1`) and the stable `1.1.0` release. Code written for beta versions may not be compatible with the stable release.
- gotcha Proper authentication is mandatory. You must provide an `endpoint` and a `credential` (either `AzureKeyCredential` or a credential from `azure-identity` like `DefaultAzureCredential`). Ensure corresponding environment variables (`AZURE_AGENT_ENDPOINT`, `AZURE_AGENT_API_KEY`, or `AZURE_CLIENT_ID`, etc.) are correctly configured.
- gotcha While the quickstart uses a basic `chat` method, real-world agent interactions often require managing conversation state via `thread_id` within the `context` parameter. Failure to provide a consistent `thread_id` can lead to stateless interactions.
Install
-
pip install azure-ai-agents
Imports
- AgentClient
from azure.ai.agents import AgentClient
- AzureKeyCredential
from azure.core.credentials import AzureKeyCredential
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.ai.agents import AgentClient
from azure.core.credentials import AzureKeyCredential
# Set your Azure AI Agent endpoint and API key as environment variables
# AZURE_AGENT_ENDPOINT='https://<your-agent-resource-name>.openai.azure.com'
# AZURE_AGENT_API_KEY='your-api-key'
endpoint = os.environ.get("AZURE_AGENT_ENDPOINT", "")
api_key = os.environ.get("AZURE_AGENT_API_KEY", "")
if not endpoint or not api_key:
print("Please set AZURE_AGENT_ENDPOINT and AZURE_AGENT_API_KEY environment variables.")
else:
try:
client = AgentClient(endpoint=endpoint, credential=AzureKeyCredential(api_key))
# Example: Create an agent (if not already created)
# For a full agent creation example, refer to official documentation.
# This quickstart assumes a basic interaction.
# Example: Chat with an existing agent or start a new conversation
# Note: 'context' with 'thread_id' might be required depending on your agent setup.
# This example uses a simplified chat without explicit thread_id for brevity,
# but in real applications, manage thread_ids for conversational continuity.
chat_response = client.chat(
input="Hello, what can you do?",
context={}
)
print(f"Agent Output: {chat_response.output}")
except Exception as e:
print(f"An error occurred: {e}")