* PR2: Wire context provider pipeline and update all internal consumers - Replace AgentThread with AgentSession across all packages - Replace ContextProvider with BaseContextProvider across all packages - Replace context_provider param with context_providers (Sequence) - Replace thread= with session= in run() signatures - Replace get_new_thread() with create_session() - Add get_session(service_session_id) to agent interface - DurableAgentThread -> DurableAgentSession - Remove _notify_thread_of_new_messages from WorkflowAgent - Wire before_run/after_run context provider pipeline in RawAgent - Auto-inject InMemoryHistoryProvider when no providers configured * fix: update all tests for context provider pipeline, fix lazy-loaders, remove old test files * refactor: update all sample files for context provider pipeline (AgentThread→AgentSession, ContextProvider→BaseContextProvider) * fix: update remaining ag-ui references (client docstring, getting_started sample) * fix: make get_session service_session_id keyword-only to avoid confusion with session_id * refactor: rename _RunContext.thread_messages to session_messages * refactor: remove _threads.py, _memory.py, and old provider files; migrate devui to use plain message lists * rename: remove _new_ prefix from test files * refactor: rewrite SlidingWindowChatMessageStore as SlidingWindowHistoryProvider(InMemoryHistoryProvider) * fix: read full history from session state directly instead of reaching into provider internals * fix: update stale .pyi stubs, sample imports, and README references for new provider types * fix: remove stale message_store, _notify_thread_of_new_messages, and session_id.key references in samples * refactor: merge context_providers and sessions sample folders into sessions, remove aggregate_context_provider * refactor: UserInfoMemory stores state in session.state instead of instance attributes * feat: add Pydantic BaseModel support to session state serialization Pydantic models stored in session.state are now automatically serialized via model_dump() and restored via model_validate() during to_dict()/from_dict() round-trips. Models are auto-registered on first serialization; use register_state_type() for cold-start deserialization. Also export register_state_type as a public API. * fix mem0 * Update sample README links and descriptions for session terminology - Replace 'thread' with 'session' in sample descriptions across all READMEs - Update file links for renamed samples (mem0_sessions, redis_sessions, etc.) - Fix Threads section → Sessions section in main samples/README.md - Update tools, middleware, workflows, durabletask, azure_functions READMEs - Update architecture diagrams in concepts/tools/README.md - Update migration guides (autogen, semantic-kernel) * Fix broken Redis README link to renamed sample * Fix Mem0 OSS client search: pass scoping params as direct kwargs AsyncMemory (OSS) expects user_id/agent_id/run_id as direct kwargs, while AsyncMemoryClient (Platform) expects them in a filters dict. Adds tests for both client types. Port of fix from #3844 to new Mem0ContextProvider. * Fix rebase issues: restore missing _conversation_state.py and checkpoint decode logic - Add back _conversation_state.py (encode/decode_chat_messages) lost in rebase - Fix on_checkpoint_restore to decode cache/conversation with decode_chat_messages - Fix on_checkpoint_restore to use decode_checkpoint_value for pending requests - Add tests/workflow/__init__.py for relative import support - Fix test_agent_executor checkpoint selection (checkpoints[1] not superstep) * Add STORES_BY_DEFAULT ClassVar to skip redundant InMemoryHistoryProvider injection Chat clients that store history server-side by default (OpenAI Responses API, Azure AI Agent) now declare STORES_BY_DEFAULT = True. The agent checks this during auto-injection and skips InMemoryHistoryProvider unless the user explicitly sets store=False. * Fix broken markdown links in azure_ai and redis READMEs * Fix getting-started samples to use session API instead of removed thread/ContextProvider API * updates to workflow as agent * fix group chat import * Rename Thread→Session throughout, fix service_session_id propagation, remove stale AGUIThread - Fix: Propagate conversation_id from ChatResponse back to session.service_session_id in both streaming and non-streaming paths in _agents.py - Rename AgentThreadException → AgentSessionException - Remove stale AGUIThread from ag_ui lazy-loader - Rename use_service_thread → use_service_session in ag-ui package - Rename test functions from *_thread_* to *_session_* - Rename sample files from *_thread* to *_session* - Update docstrings and comments: thread → session - Update _mcp.py kwargs filter: add 'session' alongside 'thread' - Fix ContinuationToken docstring example: thread=thread → session=session - Fix _clients.py docstring: 'Agent threads' → 'Agent sessions' * Fix broken markdown links after thread→session file renames * fix azure ai test
10 KiB
Azure AI Agent Examples
This folder contains examples demonstrating different ways to create and use agents with the Azure AI client from the agent_framework.azure package. These examples use the AzureAIClient with the azure-ai-projects 2.x (V2) API surface (see changelog). For V1 (azure-ai-agents 1.x) samples using AzureAIAgentClient, see the Azure AI V1 examples folder.
Examples
| File | Description |
|---|---|
azure_ai_basic.py |
The simplest way to create an agent using AzureAIProjectAgentProvider. Demonstrates both streaming and non-streaming responses with function tools. Shows automatic agent creation and basic weather functionality. |
azure_ai_provider_methods.py |
Comprehensive guide to AzureAIProjectAgentProvider methods: create_agent() for creating new agents, get_agent() for retrieving existing agents (by name, reference, or details), and as_agent() for wrapping SDK objects without HTTP calls. |
azure_ai_use_latest_version.py |
Demonstrates how to reuse the latest version of an existing agent instead of creating a new agent version on each instantiation by using provider.get_agent() to retrieve the latest version. |
azure_ai_with_agent_as_tool.py |
Shows how to use the agent-as-tool pattern with Azure AI agents, where one agent delegates work to specialized sub-agents wrapped as tools using as_tool(). Demonstrates hierarchical agent architectures. |
azure_ai_with_agent_to_agent.py |
Shows how to use Agent-to-Agent (A2A) capabilities with Azure AI agents to enable communication with other agents using the A2A protocol. Requires an A2A connection configured in your Azure AI project. |
azure_ai_with_azure_ai_search.py |
Shows how to use Azure AI Search with Azure AI agents to search through indexed data and answer user questions with proper citations. Requires an Azure AI Search connection and index configured in your Azure AI project. |
azure_ai_with_bing_grounding.py |
Shows how to use Bing Grounding search with Azure AI agents to search the web for current information and provide grounded responses with citations. Requires a Bing connection configured in your Azure AI project. |
azure_ai_with_bing_custom_search.py |
Shows how to use Bing Custom Search with Azure AI agents to search custom search instances and provide responses with relevant results. Requires a Bing Custom Search connection and instance configured in your Azure AI project. |
azure_ai_with_browser_automation.py |
Shows how to use Browser Automation with Azure AI agents to perform automated web browsing tasks and provide responses based on web interactions. Requires a Browser Automation connection configured in your Azure AI project. |
azure_ai_with_code_interpreter.py |
Shows how to use AzureAIClient.get_code_interpreter_tool() with Azure AI agents to write and execute Python code for mathematical problem solving and data analysis. |
azure_ai_with_code_interpreter_file_generation.py |
Shows how to retrieve file IDs from code interpreter generated files using both streaming and non-streaming approaches. |
azure_ai_with_code_interpreter_file_download.py |
Shows how to download files generated by code interpreter using the OpenAI containers API. |
azure_ai_with_content_filtering.py |
Shows how to enable content filtering (RAI policy) on Azure AI agents using RaiConfig. Requires creating an RAI policy in Azure AI Foundry portal first. |
azure_ai_with_existing_agent.py |
Shows how to work with a pre-existing agent by providing the agent name and version to the Azure AI client. Demonstrates agent reuse patterns for production scenarios. |
azure_ai_with_existing_conversation.py |
Demonstrates how to use an existing conversation created on the service side with Azure AI agents. Shows two approaches: specifying conversation ID at the client level and using AgentSession with an existing conversation ID. |
azure_ai_with_application_endpoint.py |
Demonstrates calling the Azure AI application-scoped endpoint. |
azure_ai_with_explicit_settings.py |
Shows how to create an agent with explicitly configured AzureAIClient settings, including project endpoint, model deployment, and credentials rather than relying on environment variable defaults. |
azure_ai_with_file_search.py |
Shows how to use AzureAIClient.get_file_search_tool() with Azure AI agents to upload files, create vector stores, and enable agents to search through uploaded documents to answer user questions. |
azure_ai_with_hosted_mcp.py |
Shows how to integrate hosted Model Context Protocol (MCP) tools with Azure AI Agent using AzureAIClient.get_mcp_tool(). |
azure_ai_with_local_mcp.py |
Shows how to integrate local Model Context Protocol (MCP) tools with Azure AI agents. |
azure_ai_with_response_format.py |
Shows how to use structured outputs (response format) with Azure AI agents using Pydantic models to enforce specific response schemas. |
azure_ai_with_runtime_json_schema.py |
Shows how to use structured outputs (response format) with Azure AI agents using a JSON schema to enforce specific response schemas. |
azure_ai_with_search_context_agentic.py |
Shows how to use AzureAISearchContextProvider with agentic mode. Uses Knowledge Bases for multi-hop reasoning across documents with query planning. Recommended for most scenarios - slightly slower with more token consumption for query planning, but more accurate results. |
azure_ai_with_search_context_semantic.py |
Shows how to use AzureAISearchContextProvider with semantic mode. Fast hybrid search with vector + keyword search and semantic ranking for RAG. Best for simple queries where speed is critical. |
azure_ai_with_sharepoint.py |
Shows how to use SharePoint grounding with Azure AI agents to search through SharePoint content and answer user questions with proper citations. Requires a SharePoint connection configured in your Azure AI project. |
azure_ai_with_session.py |
Demonstrates session management with Azure AI agents, including automatic session creation for stateless conversations and explicit session management for maintaining conversation context across multiple interactions. |
azure_ai_with_image_generation.py |
Shows how to use AzureAIClient.get_image_generation_tool() with Azure AI agents to generate images based on text prompts. |
azure_ai_with_memory_search.py |
Shows how to use memory search functionality with Azure AI agents for conversation persistence. Demonstrates creating memory stores and enabling agents to search through conversation history. |
azure_ai_with_microsoft_fabric.py |
Shows how to use Microsoft Fabric with Azure AI agents to query Fabric data sources and provide responses based on data analysis. Requires a Microsoft Fabric connection configured in your Azure AI project. |
azure_ai_with_openapi.py |
Shows how to integrate OpenAPI specifications with Azure AI agents using dictionary-based tool configuration. Demonstrates using external REST APIs for dynamic data lookup. |
azure_ai_with_reasoning.py |
Shows how to enable reasoning for a model that supports it. |
azure_ai_with_web_search.py |
Shows how to use AzureAIClient.get_web_search_tool() with Azure AI agents to perform web searches and retrieve up-to-date information from the internet. |
Environment Variables
Before running the examples, you need to set up your environment variables. You can do this in one of two ways:
Option 1: Using a .env file (Recommended)
-
Copy the
.env.examplefile from thepythondirectory to create a.envfile:cp ../../../../.env.example ../../../../.env -
Edit the
.envfile and add your values:AZURE_AI_PROJECT_ENDPOINT="your-project-endpoint" AZURE_AI_MODEL_DEPLOYMENT_NAME="your-model-deployment-name"
Option 2: Using environment variables directly
Set the environment variables in your shell:
export AZURE_AI_PROJECT_ENDPOINT="your-project-endpoint"
export AZURE_AI_MODEL_DEPLOYMENT_NAME="your-model-deployment-name"
Required Variables
AZURE_AI_PROJECT_ENDPOINT: Your Azure AI project endpoint (required for all examples)AZURE_AI_MODEL_DEPLOYMENT_NAME: The name of your model deployment (required for all examples)
Authentication
All examples use AzureCliCredential for authentication by default. Before running the examples:
- Install the Azure CLI
- Run
az loginto authenticate with your Azure account - Ensure you have appropriate permissions to the Azure AI project
Alternatively, you can replace AzureCliCredential with other authentication options like DefaultAzureCredential or environment-based credentials.
Running the Examples
Each example can be run independently. Navigate to this directory and run any example:
python azure_ai_basic.py
python azure_ai_with_code_interpreter.py
# ... etc
The examples demonstrate various patterns for working with Azure AI agents, from basic usage to advanced scenarios like session management and structured outputs.