The Python SDKs provide comprehensive access to Azure AI services including Foundry Local, Azure Machine Learning, and Azure AI Search. This guide covers installation, authentication, and usage examples.
Use the OpenAI-compatible client for model inference:
# Get OpenAI client from projectwith project_client.get_openai_client() as openai_client: response = openai_client.responses.create( model="gpt-5.2", input="What is the size of France in square miles?", ) print(f"Response: {response.output_text}")
from foundry_local import FoundryLocalManager# Initialize manager (starts service if not running)manager = FoundryLocalManager()manager.start_service()# List available modelscatalog = manager.list_catalog_models()print(f"Available models: {len(catalog)}")# Download and load a modelalias = "qwen2.5-0.5b"model_info = manager.download_model(alias)model_info = manager.load_model(alias)print(f"Loaded model: {model_info.id}")print(f"Device: {model_info.device_type}")print(f"Execution provider: {model_info.execution_provider}")
# Get cache locationcache_path = manager.get_cache_location()print(f"Model cache: {cache_path}")# List cached modelscached = manager.list_cached_models()for model in cached: print(f"Cached: {model.alias} ({model.file_size_mb} MB)")# List loaded modelsloaded = manager.list_loaded_models()for model in loaded: print(f"Loaded: {model.alias}")# Unload a modelmanager.unload_model(alias)
from azure.search.documents import SearchClientdocuments = [ { "id": "doc1", "page_chunk": "Phoenix is a major city in Arizona.", "page_number": 104 }, { "id": "doc2", "page_chunk": "The Phoenix metropolitan area includes Glendale.", "page_number": 105 }]result = search_client.upload_documents(documents=documents)print(f"Uploaded {len(result)} documents")
from azure.core.exceptions import ( HttpResponseError, ServiceRequestError, ResourceNotFoundError)try: result = search_client.search(search_text="query")except ResourceNotFoundError: print("Index not found")except HttpResponseError as e: print(f"HTTP error: {e.status_code} - {e.message}")except ServiceRequestError: print("Network error or service unavailable")
In production, use managed identity instead of API keys:
from azure.identity import ManagedIdentityCredentialcredential = ManagedIdentityCredential()
Connection Pooling
Reuse client instances for better performance:
# Good: Reuse clientclient = SearchClient(...)for query in queries: results = client.search(query)# Bad: Create new client each timefor query in queries: client = SearchClient(...) results = client.search(query)
Async Operations
Use async clients for better concurrency:
from azure.search.documents.aio import SearchClientimport asyncioasync def search_async(): async with SearchClient(...) as client: results = await client.search("query") async for result in results: print(result)asyncio.run(search_async())