### Install azure-ai-projects package Source: https://learn.microsoft.com/python/api/overview/azure/ai-projects-readme Install the required client library using pip. ```bash pip install azure-ai-projects ``` -------------------------------- ### Verify package installation Source: https://learn.microsoft.com/python/api/overview/azure/ai-projects-readme Check the installed version of the azure-ai-projects package. ```bash pip show azure-ai-projects ``` -------------------------------- ### Install aiohttp for asynchronous support Source: https://learn.microsoft.com/python/api/overview/azure/ai-projects-readme Install the aiohttp package required for asynchronous client operations. ```bash pip install aiohttp ``` -------------------------------- ### Example error output Source: https://learn.microsoft.com/python/api/overview/azure/ai-projects-readme Sample output for an unauthorized credential error. ```text Status code: 401 (Unauthorized) Operation returned an invalid status 'Unauthorized' ``` -------------------------------- ### Initialize synchronous AIProjectClient Source: https://learn.microsoft.com/python/api/overview/azure/ai-projects-readme Construct a synchronous client using a context manager and DefaultAzureCredential. ```python import os from azure.ai.projects import AIProjectClient from azure.identity import DefaultAzureCredential with ( DefaultAzureCredential() as credential, AIProjectClient( endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"], credential=credential ) as project_client, ): ``` -------------------------------- ### Initialize asynchronous AIProjectClient Source: https://learn.microsoft.com/python/api/overview/azure/ai-projects-readme Construct an asynchronous client using an async context manager. ```python import os import asyncio from azure.ai.projects.aio import AIProjectClient from azure.identity.aio import DefaultAzureCredential async with ( DefaultAzureCredential() as credential, AIProjectClient( endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"], credential=credential ) as project_client, ): ``` -------------------------------- ### Configure custom logging Source: https://learn.microsoft.com/python/api/overview/azure/ai-projects-readme Manually configure the Python logging library to output client logs to stdout or a file. ```python import sys import logging # Acquire the logger for this client library. Use 'azure' to affect both # `azure.core` and `azure.ai.projects' libraries. logger = logging.getLogger("azure") # Set the desired logging level. logging.INFO or logging.DEBUG are good options. logger.setLevel(logging.DEBUG) # Direct logging output to stdout: handler = logging.StreamHandler(stream=sys.stdout) # Or direct logging output to a file: # handler = logging.FileHandler(filename="sample.log") logger.addHandler(handler) # Optional: change the default logging format. Here we add a timestamp. #formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(name)s:%(message)s") #handler.setFormatter(formatter) ``` -------------------------------- ### Perform operations with OpenAI client Source: https://learn.microsoft.com/python/api/overview/azure/ai-projects-readme Obtain an authenticated OpenAI client from the project client to perform response operations. ```python with project_client.get_openai_client() as openai_client: response = openai_client.responses.create( model=os.environ["FOUNDRY_MODEL_NAME"], input="What is the size of France in square miles?", ) print(f"Response output: {response.output_text}") response = openai_client.responses.create( model=os.environ["FOUNDRY_MODEL_NAME"], input="And what is the capital city?", previous_response_id=response.id, ) print(f"Response output: {response.output_text}") ``` -------------------------------- ### Enable non-redacted logging Source: https://learn.microsoft.com/python/api/overview/azure/ai-projects-readme Configure the client constructor to disable redaction of sensitive information in logs. Requires logging level to be set to DEBUG. ```python project_client = AIProjectClient( credential=DefaultAzureCredential(), endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"], logging_enable=True ) ``` -------------------------------- ### Handle HttpResponseError exceptions Source: https://learn.microsoft.com/python/api/overview/azure/ai-projects-readme Catch and inspect service call errors to retrieve status codes and detailed error messages. ```python from azure.core.exceptions import HttpResponseError ... try: result = project_client.connections.list() except HttpResponseError as e: print(f"Status code: {e.status_code} ({e.reason})") print(e.message) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.