You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用服务主体调用Azure AI Foundry中Mistral-Nemo模型报Bad Request错误

使用服务主体调用Azure AI Foundry中Mistral-Nemo模型时返回Bad Request错误

错误详情

Error: Operation returned an invalid status 'Bad Request'
Traceback (most recent call last):
  File "c:\Users\guptswapmax\TestFalcon\TestFalcon.py", line 32, in <module>
        response = client.complete(
                   ~~~~~~~~~~~~~~~^
        messages=[
        ^^^^^^^^^^
    ...<5 lines>...
        model=model_name
        ^^^^^^^^^^^^^^^^
    )
    ^
  File "c:\Users\abc\TestFalcon\.venv\Lib\site-packages\azure\ai\inference\_patch.py", line 738, in complete
    raise HttpResponseError(response=response)
azure.core.exceptions.HttpResponseError: Operation returned an invalid status 'Bad Request'

已确认信息

  • 服务主体已分配「Cognitive Support User」和「Cognitive User Contributor」角色,权限配置无误
  • 使用Azure API Key + AzureKeyCredential执行相同代码,可正常调用模型

调用代码

import os
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import SystemMessage, UserMessage
from azure.identity import DefaultAzureCredential, ClientSecretCredential
import logging
from azure.core.pipeline.policies import HttpLoggingPolicy

endpoint = "https://abcdomain.services.ai.azure.com/models"
model_name  = "Mistral-Nemo"

Client_Id = "xxxxxxxxxxxxxxxxxxxxxxxx"
Client_Secret = "xxxxxxxxxxxxxxxxxxx"
Tenant_Id = "xxxxxxxxxxxxxxxxxxxxxxxxx"    

cred = ClientSecretCredential(
    tenant_id=Tenant_Id,
    client_id=Client_Id,
    client_secret=Client_Secret    
)

client = ChatCompletionsClient(
        endpoint=endpoint,
        credential=cred,
        credential_scopes=["https://cognitiveservices.azure.com/.default"]
    )

try:
   result = client.complete(
    messages=[
        SystemMessage(content="You are a helpful assistant."),
        UserMessage(content="How many languages are in the world?"),
    ],
    temperature=0.8,
    top_p=0.1,
    max_tokens=2048,
    stream=True,
    model=model_name
)
except Exception as e:
    print("Error:", e)

可能的修复方向

  • 调整端点格式:当前端点为https://abcdomain.services.ai.azure.com/models,尝试将端点改为https://abcdomain.services.ai.azure.com,再执行调用测试。Azure AI Foundry的推理服务通常不需要在端点中包含/models路径。
  • 更换权限范围:将credential_scopes的值从https://cognitiveservices.azure.com/.default替换为https://ai.azure.com/.default,重新认证后测试。
  • 禁用流式传输:暂时将stream=True改为stream=False,确认是否是流式请求在服务主体认证下的兼容性问题。
  • 开启详细日志:添加日志策略获取完整请求响应细节,定位Bad Request的具体原因,修改客户端初始化代码如下:
client = ChatCompletionsClient(
    endpoint=endpoint,
    credential=cred,
    credential_scopes=["https://cognitiveservices.azure.com/.default"],
    logging_policy=HttpLoggingPolicy(logger=logging.getLogger(), logging_level=logging.DEBUG)
)

内容的提问来源于stack exchange,提问作者Bokambo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 05:38:21