Azure Functions Integration for Microsoft Agent Framework

1.0.0b260409 · active · verified Thu Apr 16

agent-framework-azurefunctions is a Python library that enables hosting Microsoft Agent Framework agents on Azure Durable Functions. It provides capabilities for agents to persist state, replay conversation history, and automatically recover from failures. Currently, the library is in a beta status (version 1.0.0b260409) and is actively developed as part of the broader Microsoft Agent Framework ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define an `AgentFunctionApp` and register a simple AI agent powered by Azure OpenAI. To run this, you need Azure Functions Core Tools, a `local.settings.json` file for environment variables (including `AZURE_OPENAI_ENDPOINT`, `AZURE_OPENAI_CHAT_DEPLOYMENT_NAME`, `AzureWebJobsStorage`, `DURABLE_TASK_SCHEDULER_CONNECTION_STRING`, `TASKHUB_NAME`), and a `host.json` for extension bundle configuration. The code configures an `AgentFunctionApp` which will be picked up by the Azure Functions runtime when deployed or run locally.

import os
from agent_framework.azure import AgentFunctionApp, AzureOpenAIChatClient
from azure.identity import AzureCliCredential

# Ensure environment variables are set for local execution/testing
os.environ['AZURE_OPENAI_ENDPOINT'] = os.environ.get('AZURE_OPENAI_ENDPOINT', 'https://your-resource.openai.azure.com/')
os.environ['AZURE_OPENAI_CHAT_DEPLOYMENT_NAME'] = os.environ.get('AZURE_OPENAI_CHAT_DEPLOYMENT_NAME', 'gpt-4o-mini')

# You might need to set up 'DURABLE_TASK_SCHEDULER_CONNECTION_STRING' and 'TASKHUB_NAME' 
# in local.settings.json or environment variables for Durable Functions. 
# For local development with Azurite and Durable Task Emulator, you'd typically have:
# DURABLE_TASK_SCHEDULER_CONNECTION_STRING='Endpoint=http://localhost:8080;TaskHub=default;Authentication=None'
# TASKHUB_NAME='default'
# AzureWebJobsStorage='UseDevelopmentStorage=true'

# Create an Azure OpenAI client
# For production, consider DefaultAzureCredential or ManagedIdentityCredential
client = AzureOpenAIChatClient(credential=AzureCliCredential())

# Create an agent
agent = client.as_agent(
    name="Assistant",
    instructions="You are a helpful assistant."
)

# Register the agent with the Functions app
# This creates the necessary HTTP endpoints and durable entities.
# For a real Azure Function app, this file would typically be named 'function_app.py'
# and would be discovered by the Azure Functions runtime.
app = AgentFunctionApp(agents=[agent], enable_health_check=True)

# To run this locally, you would typically use Azure Functions Core Tools:
# 1. Install Azure Functions Core Tools (npm install -g azure-functions-core-tools@4 --unsafe-perm true)
# 2. In your project directory, create local.settings.json and host.json as described in the documentation.
# 3. Start Azurite and Durable Task Emulator (e.g., via `azd env up` or manually)
# 4. Run `func start` in your terminal.

# The agent's HTTP endpoint would then be available at /api/agents/Assistant/run
# This quickstart code itself doesn't start the Azure Functions host, it defines the app.

view raw JSON →