[Python] Error: Operation returned an invalid status 'Bad Request' on client.complete

Discussão em 'Python' iniciado por Stack, Maio 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am using below code to get data from "Mistral-Nemo" model hosted as SaaS in Azure AI Foundry. It gives me below error:


    An unhandled exception of type 'Azure.RequestFailedException' occurred in Azure.AI.Inference.dll: 'Auth token validation failed. Please provide a valid key or a valid AAD token
    Status: 401 (Unauthorized)
    ​

    I have looked into the permissions and make sure my SP has required permissions and it also has "Coginitive Support User" and "Cognitive User Contributor" role and trying to implement using Service Principal

    When i use the same code with Azure API Key and AzureKeyCredential it works. https://github.com/MicrosoftDocs/az...rence/includes/use-chat-completions/csharp.md

    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_deployment_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_deployment_name,
    )
    except Exception as e:
    print("Error:", e)

    Continue reading...

Compartilhe esta Página