Azure AI Studio SDK报错:Session.request()不支持with_credentials参数
Azure AI Studio SDK 错误解决指南
问题描述
运行从Azure AI Studio官方文档复制的代码时,触发错误:TypeError: Session.request() got an unexpected keyword argument 'with_credentials',同时发现对应Python API文档中未提及AIProjectClient的connections属性,需解决该问题。
原代码
from azure.identity import DefaultAzureCredential from azure.ai.projects import AIProjectClient project_connection_string="somevaluehere" project = AIProjectClient.from_connection_string( conn_str=project_connection_string, credential=DefaultAzureCredential()) from azure.core.credentials import AzureKeyCredential from azure.ai.projects.models import ConnectionType from azure.search.documents import SearchClient from azure.search.documents.indexes import SearchIndexClient # use the project client to get the default search connection search_connection = project.connections.get_default( connection_type=ConnectionType.AZURE_AI_SEARCH, with_credentials=True) # Create a client to create and manage search indexes index_client = SearchIndexClient( endpoint=search_connection.endpoint_url, credential=AzureKeyCredential(key=search_connection.key) ) # Create a client to run search queries search_client = SearchClient( index_name="your_index_name", endpoint=search_connection.endpoint_url, credential=AzureKeyCredential(key=search_connection.key) )
错误栈信息
Traceback (most recent call last): File "C:\Users\manuchadha\OneDrive - Microsoft\Manu_Personal\programs\2025learnresolution\client.py", line 75, in <module> search_connection = project.connections.get_default( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\tracing\decorator.py", line 94, in wrapper_use_tracer return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\ai\projects\operations\_patch.py", line 369, in get_default connection_properties_list = self.list(connection_type=connection_type, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\tracing\decorator.py", line 94, in wrapper_use_tracer return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\ai\projects\operations\_patch.py", line 439, in list connections_list: ListConnectionsResponse = self._list_connections( ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\tracing\decorator.py", line 94, in wrapper_use_tracer return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\ai\projects\operations\_operations.py", line 6597, in _list_connections pipeline_response: PipelineResponse = self._client._pipeline.run( # pylint: disable=protected-access ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\pipeline\_base.py", line 229, in run return first_node.send(pipeline_request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\pipeline\_base.py", line 86, in send response = self.next.send(request) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\pipeline\_base.py", line 86, in send response = self.next.send(request) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\pipeline\_base.py", line 86, in send response = self.next.send(request) ^^^^^^^^^^^^^^^^^^^^^^^ [Previous line repeated 2 more times] File "C:\Python311\Lib\site-packages\azure\core\pipeline\policies\_redirect.py", line 197, in send response = self.next.send(request) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\pipeline\policies\_retry.py", line 532, in send response = self.next.send(request) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\pipeline\policies\_authentication.py", line 147, in send response = self.next.send(request) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\pipeline\_base.py", line 86, in send response = self.next.send(request) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\pipeline\_base.py", line 86, in send response = self.next.send(request) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\pipeline\_base.py", line 86, in send response = self.next.send(request) ^^^^^^^^^^^^^^^^^^^^^^^ [Previous line repeated 2 more times] File "C:\Python311\Lib\site-packages\azure\core\pipeline\_base.py", line 118, in send self._sender.send(request.http_request, **request.context.options), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\azure\core\pipeline\transport\_requests_basic.py", line 355, in send response = self.session.request( # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: Session.request() got an unexpected keyword argument 'with_credentials'
解决步骤
1. 更新SDK包到最新预览版
该问题源于azure-ai-projects包版本与文档不匹配,connections功能属于预览特性,需安装最新预览版:
pip install --upgrade azure-ai-projects --pre
同时更新依赖包确保兼容性:
pip install --upgrade azure-core azure-identity
2. 修正代码参数
移除不被当前版本支持的with_credentials参数,并改用DefaultAzureCredential直接认证搜索服务:
from azure.identity import DefaultAzureCredential from azure.ai.projects import AIProjectClient from azure.search.documents import SearchClient from azure.search.documents.indexes import SearchIndexClient from azure.ai.projects.models import ConnectionType project_connection_string="somevaluehere" project = AIProjectClient.from_connection_string( conn_str=project_connection_string, credential=DefaultAzureCredential()) # 移除with_credentials参数 search_connection = project.connections.get_default( connection_type=ConnectionType.AZURE_AI_SEARCH ) # 使用DefaultAzureCredential认证,无需提取密钥 index_client = SearchIndexClient( endpoint=search_connection.endpoint_url, credential=DefaultAzureCredential() ) search_client = SearchClient( index_name="your_index_name", endpoint=search_connection.endpoint_url, credential=DefaultAzureCredential() )
3. 备选方案:直接使用Search SDK
若更新包后仍无connections属性,可跳过AIProjectClient,直接创建搜索服务客户端:
from azure.identity import DefaultAzureCredential from azure.search.documents import SearchClient from azure.search.documents.indexes import SearchIndexClient # 直接指定搜索服务端点 search_endpoint = "https://your-search-service.search.windows.net" index_client = SearchIndexClient( endpoint=search_endpoint, credential=DefaultAzureCredential() ) search_client = SearchClient( index_name="your_index_name", endpoint=search_endpoint, credential=DefaultAzureCredential() )
内容的提问来源于stack exchange,提问作者Manu Chadha
相关产品推荐
相关产品推荐

