VS Code Prompt Flow中AzureOpenAI连接建立失败求助
解决Azure OpenAI Prompt Flow连接创建失败问题
问题核心
- 通过
az login成功登录Azure,但创建Azure OpenAI连接时无API Key输入提示,终端报401未授权及API Key验证错误 - 相同凭据在同事环境可正常使用,已重装相关工具及扩展,问题仍存在
错误日志
File "c:\Users\<USERFOLDER>\.vscode\extensions\prompt-flow.prompt-flow-1.20.0\pfutil\_pf_vsc_connection.py", line 52, in upsert_connection upsert_connection_new(args) File "c:\Users\<USERFOLDER>\.vscode\extensions\prompt-flow.prompt-flow-1.20.0\pfutil\_pf_vsc_connection.py", line 65, in upsert_connection_new connection = _upsert_connection_from_file(args.file) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\_cli\_pf\_connection.py", line 224, in _upsert_connection_from_file existing_connection = _get_pf_client().connections.get(connection.name, raise_error=False) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\_sdk\_telemetry\activity.py", line 265, in wrapper return f(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\_sdk\operations\_local_azure_connection_operations.py", line 111, in get return self._get(name, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\_sdk\operations\_local_azure_connection_operations.py", line 130, in _get return ArmConnectionOperations._direct_get( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\azure\operations\_arm_connection_operations.py", line 62, in _direct_get return _Connection._from_core_connection(provider.get(name=name)) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\core\_connection_provider\_workspace_connection_provider.py", line 461, in get connection_dict = self._build_connection_dict( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\core\_connection_provider\_workspace_connection_provider.py", line 336, in _build_connection_dict rest_obj: WorkspaceConnectionPropertiesV2BasicResource = cls.open_url( ^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\core\_connection_provider\_workspace_connection_provider.py", line 148, in open_url raise OpenURLFailedUserError( promptflow.core._errors.OpenURLFailedUserError: Open url /subscriptions/<My Subscription Credentials>/providers/Microsoft.MachineLearningServices/workspaces/<My Workspace>/connections/<My API Name>/listsecrets?api-version=2023-04-01-preview failed with status code: 401, action: listsecrets, reason: Unauthorized During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\Users\<USERFOLDER>\.vscode\extensions\prompt-flow.prompt-flow-1.20.0\pfutil\pfutil.py", line 4, in <module> main() File "c:\Users\<USERFOLDER>\.vscode\extensions\prompt-flow.prompt-flow-1.20.0\pfutil\_pf_vsc_main.py", line 37, in main operate_connection(args) File "c:\Users\<USERFOLDER>\.vscode\extensions\prompt-flow.prompt-flow-1.20.0\pfutil\_pf_vsc_connection.py", line 29, in operate_connection upsert_connection(args) File "c:\Users\<USERFOLDER>\.vscode\extensions\prompt-flow.prompt-flow-1.20.0\pfutil\_pf_vsc_connection.py", line 58, in upsert_connection connection = ConnectionOperations().create_or_update(connection) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\_sdk\_telemetry\activity.py", line 265, in wrapper return f(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\_sdk\operations\_connection_operations.py", line 87, in create_or_update orm_object = connection._to_orm_object() ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\_sdk\entities\_connection.py", line 247, in _to_orm_object secrets = self._validate_and_encrypt_secrets() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow\_sdk\entities\_connection.py", line 116, in _validate_and_encrypt_secrets raise ValidationException( promptflow.exceptions.ValidationException: Connection '<My API Name>' secrets ['api_key'] value invalid, please fill them.
解决步骤
1. 验证Azure账号权限
错误日志中401 Unauthorized是核心诱因,说明当前账号对目标机器学习工作区的连接资源无listsecrets权限:
- 登录Azure门户,进入目标工作区,检查账号是否被分配机器学习工作区参与者或认知服务参与者角色
- 用Azure CLI命令确认权限:
az role assignment list --assignee <你的Azure登录邮箱> --scope /subscriptions/<订阅ID>/resourceGroups/<资源组名>/providers/Microsoft.MachineLearningServices/workspaces/<工作区名> - 若权限不足,联系管理员添加对应角色
2. 重置本地Azure凭据缓存
即使az login显示成功,本地可能存在过期凭据:
- 执行命令清空缓存:
az account clear - 重新登录并切换到正确订阅:
az login az account set --subscription <目标订阅ID> - 完全重启VS Code,避免会话残留
3. 手动创建连接文件
绕过自动输入流程,直接编写连接定义文件:
- 在flow项目目录下创建
connections/azure_openai.yaml,内容如下:$schema: https://azuremlschemas.azureedge.net/promptflow/latest/AzureOpenAIConnection.schema.json name: <你的连接名称> type: azure_openai api_key: <你的Azure OpenAI API密钥> api_base: <你的API基础地址,格式为https://<资源名称>.openai.azure.com/> api_type: azure api_version: 2023-05-15 - 在VS Code的Prompt Flow面板中导入该文件完成连接创建
4. 清理Prompt Flow本地缓存及依赖
- 删除VS Code扩展缓存目录:
C:\Users\<USERFOLDER>\.vscode\extensions\prompt-flow.prompt-flow-1.20.0\ - 删除Python依赖目录:
C:\Users\<USERFOLDER>\AppData\Roaming\Python\Python311\site-packages\promptflow - 重新安装最新依赖:
pip install promptflow promptflow-tools --upgrade
内容的提问来源于stack exchange,提问作者MakE
相关产品推荐
相关产品推荐

