Error Handling for Azure Open AI in Python
A guide to handle common errors and exceptions when using Azure Open AI services in Python
Introduction
Azure Open AI is a set of cloud-based services that provide access to powerful artificial intelligence models for various tasks such as natural language processing, computer vision, text generation, and more. To use these services, you need to have an Azure account and a subscription key that grants you access to the API endpoints. You also need to use a Python library such as requests or azure-core to send HTTP requests and receive responses from the services.
However, when using Azure Open AI services, you may encounter some errors or exceptions that prevent you from getting the desired results or cause your program to crash. These errors can be caused by various factors, such as network issues, invalid parameters, authentication failures, quota limits, service errors, or unexpected responses. Therefore, it is important to write a logic for error handling that can handle these scenarios and provide appropriate feedback or actions to the user or the program.
Error Handling Logic
The error handling logic for Azure Open AI in Python can be divided into three main steps:
· Send the request and check the status code
· Parse the response and handle the error message
· Handle the token expiration and renewal
Send the Request and Check the Status Code
The first step is to send the request to the Azure Open AI service using the requests or azure-core library. You need to provide the service URL, the subscription key, the request headers, and the request body as parameters. For example, to use the text generation service, you can use the following code:
import requests
# Replace with your subscription key
subscription_key = "xxxxxxxxxxxxxxxxxxxxxxxxx"
# Replace with your service URL
service_url = "[URL]"
# Replace with your request headers
headers = {
"Ocp-Apim-Subscription-Key": subscription_key,
"Content-Type": "application/json"
}
# Replace with your request body
body = {
"model": "Copilot",
"prompt": "Write a logic for error handling for azure open AI in python.",
"length": 100
}
# Send the request
response = requests.post(service_url, headers=headers, json=body)
After sending the request, you need to check the status code of the response. The status code is a three-digit number that indicates the outcome of the request. A status code of 200 means that the request was successful and the response contains the expected result. However, if the status code is not 200, it means that there was an error or an exception that occurred during the request. You can use the following code to check the status code and raise an exception if it is not 200:
# Check the status code
status_code = response.status_code
if status_code != 200:
# Raise an exception with the status code
raise Exception(f"Request failed with status code {status_code}")
Parse the Response and Handle the Error Message
The second step is to parse the response and handle the error message if there was an error or an exception. The response from the Azure Open AI service is a JSON object that contains the result or the error information. You can use the json method of the response object to parse the response and get the JSON object. For example, to get the result from the text generation service, you can use the following code:
# Parse the response
response_json = response.json()
# Get the result
result = response_json["text"]
However, if there was an error or an exception, the response JSON object will contain an error field that has the error code and the error message. You can use the following code to get the error information and handle it accordingly:
# Parse the response
response_json = response.json()
# Check if there is an error field
if "error" in response_json:
# Get the error code and message
error_code = response_json["error"]["code"]
error_message = response_json["error"]["message"]
# Handle the error based on the error code
if error_code == "401":
# The subscription key is invalid or expired
print("Invalid or expired subscription key. Please check your subscription key and try again.")
elif error_code == "403":
# The subscription key does not have access to the service or the model
print("Access denied. Please check your subscription key and the service or model you are using.")
elif error_code == "429":
# The request rate or the quota limit has been exceeded
print("Too many requests. Please wait for some time and try again.")
elif error_code == "500":
# The service encountered an internal error
print("Service error. Please try again later.")
else:
# Some other error occurred
print(f"Unknown error. Error code: {error_code}, Error message: {error_message}")
Handle the Token Expiration and Renewal
The third step is to handle the token expiration and renewal if you are using a token-based authentication instead of a subscription key. A token is a short-lived credential that grants you access to the Azure Open AI service for a limited time. You can obtain a token by sending a request to the token service endpoint with your subscription key. For example, to get a token for the text generation service, you can use the following code:
import requests
# Replace with your subscription key
subscription_key = "xxxxxxxxxxxxxxxxxxxxxxxxx"
# Replace with your token service URL
token_service_url = "[URL]"
# Replace with your service scope
service_scope = "[URL]"
# Send the request
token_response = requests.post(token_service_url, headers={"Ocp-Apim-Subscription-Key": subscription_key}, data={"scope": service_scope})
# Check the status code
token_status_code = token_response.status_code
Recommended by LinkedIn
if token_status_code != 200:
# Raise an exception with the status code
raise Exception(f"Token request failed with status code {token_status_code}")
# Get the token
token = token_response.text
After obtaining the token, you can use it to send the request to the Azure Open AI service by replacing the subscription key header with the authorization header. For example, to use the text generation service with the token, you can use the following code:
import requests
# Replace with your service URL
service_url = "[URL]"
# Replace with your request headers
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
# Replace with your request body
body = {
"model": "Copilot",
"prompt": "Write a logic for error handling for azure open AI in python.",
"length": 100
}
# Send the request
response = requests.post(service_url, headers=headers, json=body)
However, the token has an expiration time, which is usually 10 minutes. After the token expires, you will get a 401 error when you send the request with the expired token. Therefore, you need to handle the token expiration and renewal by checking the expiration time and requesting a new token before the old one expires. You can use the following code to check the expiration time and renew the token if needed:
import requests
import time
# Replace with your subscription key
subscription_key = "xxxxxxxxxxxxxxxxxxxxxxxxx"
# Replace with your token service URL
token_service_url = "[URL]"
# Replace with your service scope
service_scope = "[URL]"
# Initialize the token and the expiration time
token = None
token_expiration_time = None
# Define a function to get a new token
def get_new_token():
global token, token_expiration_time
# Send the request
token_response = requests.post(token_service_url, headers={"Ocp-Apim-Subscription-Key": subscription_key}, data={"scope": service_scope})
# Check the status code
token_status_code = token_response.status_code
if token_status_code != 200:
# Raise an exception with the status code
raise Exception(f"Token request failed with status code {token_status_code}")
# Get the token
token = token_response.text
# Get the expiration time
token_expiration_time = time.time() + 600 # 10 minutes
# Define a function to check and renew the token
def check_and_renew_token():
global token, token_expiration_time
# Check if the token is None or expired
if token is None or time.time() > token_expiration_time:
# Get a new token
get_new_token()
Then, before sending the request to the Azure Open AI service, you can call the check_and_renew_token function to ensure that you have a valid token. For example, to use the text generation service with the token, you can use the following code:
import requests
# Replace with your service URL
service_url = "[URL]"
# Replace with your request headers
headers = {
"Content-Type": "application/json"
}
# Replace with your request body
body = {
"model": "Copilot",
"prompt": "Write a logic for error handling for azure open AI in python.",
"length": 100
}
# Check and renew the token
check_and_renew_token()
# Add the authorization header with the token
headers["Authorization"] = f"Bearer {token}"
# Send the request
response = requests.post(service_url, headers=headers, json=body)
Conclusion
In this document, we have explained how to write a logic for error handling for Azure Open AI in Python, covering different scenarios including token handling. We have shown how to send the request and check the status code, how to parse the response and handle the error message, and how to handle the token expiration and renewal. We hope that this document will help you to use the Azure Open AI services more effectively and efficiently in your Python projects.
Handling errors in Azure Open AI can be a breeze with try-except blocks, logging, and checking error attributes for efficient debugging. Don't hesitate to explore further