Labsco
microsoft logo

azure-ai-projects-py

✓ Official2,700

by microsoft · part of microsoft/skills

Build AI applications using the Azure AI Projects Python SDK (azure-ai-projects). Use when working with Foundry project clients, creating versioned agents with…

🔥🔥🔥🔥✓ VerifiedFreeQuick setup
🧩 One of 7 skills in the microsoft/skills package — works on its own, and pairs well with its siblings.

Build AI applications using the Azure AI Projects Python SDK (azure-ai-projects). Use when working with Foundry project clients, creating versioned agents with…

Inspect the full instructions your agent will receiveExpand

This is the exact playbook injected into your agent when the skill activates — shown here so you can audit it before installing. You don't need to read it to use the skill.

by microsoft

Build AI applications using the Azure AI Projects Python SDK (azure-ai-projects). Use when working with Foundry project clients, creating versioned agents with… npx skills add https://github.com/microsoft/skills --skill azure-ai-projects-py Download ZIPGitHub2.7k

Azure AI Projects Python SDK (Foundry SDK)

Build AI applications on Microsoft Foundry using the azure-ai-projects SDK.

Environment Variables

Copy & paste — that's it
AZURE_AI_PROJECT_ENDPOINT="https:// .services.ai.azure.com/api/projects/ " # Required for all auth methods
AZURE_AI_MODEL_DEPLOYMENT_NAME="gpt-4o-mini" # Required for all auth methods
AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production

Authentication & Lifecycle

🔑 Two rules apply to every code sample below:

  • Prefer DefaultAzureCredential. It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.

  • Local dev: DefaultAzureCredential works as-is.

  • Production: set AZURE_TOKEN_CREDENTIALS=prod (or AZURE_TOKEN_CREDENTIALS=<specific_credential>) to constrain the credential chain to production-safe credentials.

  • Wrap every client in a context manager so HTTP transports, sockets, and token caches are released deterministically:

  • Sync: with <Client>(...) as client:

  • Async: async with <Client>(...) as client: and async with DefaultAzureCredential() as credential: (from azure.identity.aio)

Snippets may abbreviate this setup, but production code should always follow both rules.

Copy & paste — that's it
import os
from azure.identity import DefaultAzureCredential, ManagedIdentityCredential
from azure.ai.projects import AIProjectClient

# Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS= 
credential = DefaultAzureCredential(require_envvar=True)
# Or use a specific credential directly in production:
# See https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes
# credential = ManagedIdentityCredential()
with AIProjectClient(
 endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
 credential=credential,
) as client:
 deployments = list(client.deployments.list())

Client Operations Overview

Operation Access Purpose client.agents .agents.* Agent CRUD, versions, threads, runs client.connections .connections.* List/get project connections client.deployments .deployments.* List model deployments client.datasets .datasets.* Dataset management client.indexes .indexes.* Index management client.evaluations .evaluations.* Run evaluations client.red_teams .red_teams.* Red team operations

Two Client Approaches

1. AIProjectClient (Native Foundry)

Copy & paste — that's it
from azure.ai.projects import AIProjectClient

with AIProjectClient(
 endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
 credential=DefaultAzureCredential(),
) as client:
 # Use Foundry-native operations
 agent = client.agents.create_agent(
 model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
 name="my-agent",
 instructions="You are helpful.",
 )

2. OpenAI-Compatible Client

Copy & paste — that's it
# Get OpenAI-compatible client from project
openai_client = client.get_openai_client()

# Use standard OpenAI API
response = openai_client.chat.completions.create(
 model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
 messages=[{"role": "user", "content": "Hello!"}],
)

Agent Operations

Create Agent (Basic)

Copy & paste — that's it
agent = client.agents.create_agent(
 model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
 name="my-agent",
 instructions="You are a helpful assistant.",
)

Create Agent with Tools

Copy & paste — that's it
from azure.ai.agents import CodeInterpreterTool, FileSearchTool

agent = client.agents.create_agent(
 model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
 name="tool-agent",
 instructions="You can execute code and search files.",
 tools=[CodeInterpreterTool(), FileSearchTool()],
)

Versioned Agents with PromptAgentDefinition

Copy & paste — that's it
from azure.ai.projects.models import PromptAgentDefinition

# Create a versioned agent
agent_version = client.agents.create_version(
 agent_name="customer-support-agent",
 definition=PromptAgentDefinition(
 model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
 instructions="You are a customer support specialist.",
 tools=[], # Add tools as needed
 ),
 version_label="v1.0",
)

See references/agents.md for detailed agent patterns.

Tools Overview

Tool Class Use Case Code Interpreter CodeInterpreterTool Execute Python, generate files File Search FileSearchTool RAG over uploaded documents Bing Grounding BingGroundingTool Web search (requires connection) Azure AI Search AzureAISearchTool Search your indexes Function Calling FunctionTool Call your Python functions OpenAPI OpenApiTool Call REST APIs MCP McpTool Model Context Protocol servers Memory Search MemorySearchTool Search agent memory stores SharePoint SharepointGroundingTool Search SharePoint content

See references/tools.md for all tool patterns.

Thread and Message Flow

Copy & paste — that's it
# 1. Create thread
thread = client.agents.threads.create()

# 2. Add message
client.agents.messages.create(
 thread_id=thread.id,
 role="user",
 content="What's the weather like?",
)

# 3. Create and process run
run = client.agents.runs.create_and_process(
 thread_id=thread.id,
 agent_id=agent.id,
)

# 4. Get response
if run.status == "completed":
 messages = client.agents.messages.list(thread_id=thread.id)
 for msg in messages:
 if msg.role == "assistant":
 print(msg.content[0].text.value)

Connections

Copy & paste — that's it
# List all connections
connections = client.connections.list()
for conn in connections:
 print(f"{conn.name}: {conn.connection_type}")

# Get specific connection
connection = client.connections.get(connection_name="my-search-connection")

See references/connections.md for connection patterns.

Datasets and Indexes

Copy & paste — that's it
# List datasets
datasets = client.datasets.list()

# List indexes
indexes = client.indexes.list()

See references/datasets-indexes.md for data operations.

Evaluation

Copy & paste — that's it
# Using OpenAI client for evals
openai_client = client.get_openai_client()

# Create evaluation with built-in evaluators
eval_run = openai_client.evals.runs.create(
 eval_id="my-eval",
 name="quality-check",
 data_source={
 "type": "custom",
 "item_references": [{"item_id": "test-1"}],
 },
 testing_criteria=[
 {"type": "fluency"},
 {"type": "task_adherence"},
 ],
)

See references/evaluation.md for evaluation patterns.

Async Client

Copy & paste — that's it
from azure.ai.projects.aio import AIProjectClient

async with AIProjectClient(
 endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
 credential=DefaultAzureCredential(),
) as client:
 agent = await client.agents.create_agent(...)
 # ... async operations

See references/async-patterns.md for async patterns.

Memory Stores

Copy & paste — that's it
# Create memory store for agent
memory_store = client.agents.create_memory_store(
 name="conversation-memory",
)

# Attach to agent for persistent memory
agent = client.agents.create_agent(
 model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
 name="memory-agent",
 tools=[MemorySearchTool()],
 tool_resources={"memory": {"store_ids": [memory_store.id]}},
)

Best Practices

  • Pick sync OR async and stay consistent. Do not mix azure.ai.projects sync clients with azure.ai.projects.aio async clients in the same call path. Choose one mode per module.

  • Always use context managers for clients and async credentials. Wrap every client in with AIProjectClient(...) as client: (sync) or async with AIProjectClient(...) as client: (async). For async DefaultAzureCredential from azure.identity.aio, also use async with credential: so tokens and transports are cleaned up.

  • Clean up agents when done: client.agents.delete_agent(agent.id)

  • Use create_and_process for simple runs, streaming for real-time UX

  • Use versioned agents for production deployments

  • Prefer connections for external service integration (AI Search, Bing, etc.)

SDK Comparison

Feature azure-ai-projects azure-ai-agents Level High-level (Foundry) Low-level (Agents) Client AIProjectClient AgentsClient Versioning create_version() Not available Connections Yes No Deployments Yes No Datasets/Indexes Yes No Evaluation Via OpenAI client No When to use Full Foundry integration Standalone agent apps

Reference Files