A fully managed platform in Microsoft Foundry for hosting, scaling, and securing AI agents built with any supported framework or model
Hello @Agarwal, Anjali
Thank you for sharing the code snippet and detailed error message.
The APIConnectionError / ConnectError you’re encountering indicates that the request is failing at the network/connection layer, meaning it is not reaching the Azure AI Foundry (OpenAI) service. This is typically related to endpoint configuration, authentication, or network restrictions, rather than an issue with the model or SDK usage.
Your code structure and SDK usage look correct. Based on the details provided, the issue is most likely caused by one or more of the following:
- Endpoint not reachable or incorrectly formatted
- Authentication not being resolved via
DefaultAzureCredential - Network/firewall/proxy restrictions
- Environment variable formatting issues
Recommended Troubleshooting Steps
1. Validate the Foundry Project Endpoint
Ensure the endpoint format is correct:
https://<your-resource>.services.ai.azure.com/api/projects/<your-project-id>
In your case:
https://demoprojectv1.services.ai.azure.com/api/projects/udemy-demo-proj
Also, update your .env file to remove quotes:
FOUNDRY_PROJECT_ENDPOINT=https://demoprojectv1.services.ai.azure.com/api/projects/udemy-demo-proj
Then verify it is reachable:
Open in browser:
https://demoprojectv1.services.ai.azure.com
Or test via curl:
curl https://demoprojectv1.services.ai.azure.com
If this fails, it indicates a DNS or network connectivity issue.
2. Verify Environment Variable Loading
Confirm values are being read correctly in your script:
print(foundry_project_endpoint)
Ensure there are no extra spaces or hidden characters.
3. Confirm Authentication
DefaultAzureCredential() requires a valid identity. Please ensure one of the following:
Login via Azure CLI:
az login
OR set service principal credentials:
AZURE_CLIENT_ID
If authentication is not properly established, the SDK may fail during connection initialization.
4. Verify Azure Permissions
Ensure the identity being used has:
- Reader role on the Foundry project
- Cognitive Services OpenAI User role on the underlying Azure OpenAI resource
5. Check Network / Firewall / Proxy
If you are on a corporate network:
Ensure outbound HTTPS (port 443) is allowed
Allow access to:
*.services.ai.azure.com
If using a proxy, configure:
set HTTPS_PROXY=http://<proxy>:<port>
You can also test connectivity:
nslookup demoprojectv1.services.ai.azure.com
6. SDK and Python Version Compatibility
You are using:
-
azure-ai-projects==2.0.0b2(preview SDK) - Python 3.13
Some Azure SDKs may have limited validation on newer Python versions.
Recommendation:
- Try with Python 3.10 or 3.11
- Upgrade SDK if a newer version is available
7. Isolate the Issue
Test basic client initialization:
from azure.identity import DefaultAzureCredential
If this fails, it confirms the issue is before the OpenAI API call (connection/auth level).
Please refer this
Troubleshoot endpoint and model discovery (Cannot find model): https://learn.microsoft.com/azure/ai-foundry/reference/region-support
HTTP 404 “Operation Not Found” (ensure correct endpoint & deployment name): https://learn.microsoft.com/azure/ai-foundry/openai/latest
I hope this will help you. Please feel free to let me know if you have any other queries.
Thank you!