Issue While Implementing Knowledge Agents – Retrieve API Errors

Jalpa Chudasama | CHECKER 0 Reputation points
2025-06-12T05:09:41.33+00:00

We attempted to implement Knowledge Agents using the official documentation provided here: 🔗 https://learn.microsoft.com/en-us/azure/search/search-agentic-retrieval-how-to-create

We are currently facing issues with the Retrieve API. Below is the step-by-step implementation and the problems encountered.



Step 1: Create Knowledge Agent


$searchUrl = 'test.search.windows.net';
$agentName = 'agentName';
$indexName = 'indexName';
$modelProviderUrl = 'https://test.openai.azure.com';
$searchApiKey = '*********';
$modelApiKey = '************';
$endpoint = "https://{$searchUrl}/agents/{$agentName}?api-version=2025-05-01-preview";
$data = [
    'name' => $agentName,
    'targetIndexes' => [[
        'indexName' => $indexName,
        'defaultRerankerThreshold' => 2.5,
        'defaultIncludeReferenceSourceData' => true,
        'defaultMaxDocsForReranker' => 200
    ]],
    'models' => [[
        'kind' => 'azureOpenAI',
        'azureOpenAIParameters' => [
        'resourceUri' => $modelProviderUrl,
        'apiKey' => $modelApiKey,
        'deploymentId' => 'text-embedding-3-large',
        'modelName' => 'gpt-4o'
        ]
    ]],
    'requestLimits' => [
        'maxOutputSize' => 5000,
        'maxRuntimeInSeconds' => 60
    ],
    'encryptionKey' => null
];
$ch = curl_init($endpoint);
$headers = [
'Content-Type: application/json',
'api-key: ' . $searchApiKey
];
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'PUT',
    CURLOPT_POSTFIELDS => json_encode($data),
    CURLOPT_HTTPHEADER => $headers
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
    echo 'cURL Error: ' . curl_error($ch) . "\n";
} else {
    echo "HTTP Status Code: " . $httpCode . "\n";
    echo "Response: " . $response . "\n";
}
curl_close($ch);


Step 2: Retrieve API Implementation


$searchUrl = 'test.search.windows.net';
$agentName = 'agentName';
$indexName = 'indexName';
$searchApiKey = '***********************';
$endpoint = "https://{$searchUrl}/agents/{$agentName}/retrieve?api-version=2025-05-01-preview";
$data = [
    'messages' => [
        [
        'role' => 'assistant',
            'content' => [[
            'type' => 'text',
            'text' => "You are a helpful assistant for Contoso Human Resources. You have access to a search index containing guidelines about health care coverage for Washington state. If you can't find the answer in the search, say you don't know."
            ]]
        ],
        [
        'role' => 'user',
            'content' => [[
            'type' => 'text',
            'text' => 'What are my vision benefits?'
            ]]
        ]
    ],
    'targetIndexParams' => [[
        'indexName' => $indexName,
        'filterAddOn' => "State eq 'WA'",
        'IncludeReferenceSourceData' => true,
        'rerankerThreshold' => 2.5,
        'maxDocsForReranker' => 250
    ]]
];
$ch = curl_init($endpoint);
$headers = [
    'Content-Type: application/json',
    // Note: We tried both of the following in different tests
    // 'api-key: ' . $searchApiKey
    'Authorization: Bearer ' . $searchApiKey
];
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => json_encode($data),
    CURLOPT_HTTPHEADER => $headers
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
    echo 'cURL Error: ' . curl_error($ch) . "\n";
} else {
    echo "HTTP Status Code: " . $httpCode . "\n";
    echo "<pre>";
    print_r(json_decode($response));
    echo "</pre>";
}
curl_close($ch);


Step 3: Generate Access Token


$tenant = '*************';
$client = '*************';
$secret = '*************';
$url = "https://login.microsoftonline.com/$tenant/oauth2/v2.0/token";
$data = http_build_query([
    'grant_type' => 'client_credentials',
    'client_id' => $client,
    'client_secret' => $secret,
    'scope' => 'https://search.azure.com/.default'
]);
$ch = curl_init($url);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
    CURLOPT_POSTFIELDS => $data
]);
$resp = curl_exec($ch);
curl_close($ch);
echo "<pre>";
print_r(json_decode($resp)->access_token);
echo "</pre>";

Issues Encountered with Retrieve API

  1. Using Access Token:
    • Status Code: 403 Forbidden
    • Issue: No response returned.
  2. Using Search API Key as Bearer Token:
    • Status Code: 401 Unauthorized
    • Issue: No response returned.
  3. Using Search API Key with api-key Header:
    • Status Code: 404 Not Found
    • Response:
Could not complete model action. The model endpoint returned status code '404' (NotFound). Resource not found
Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
1,339 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jalpa Chudasama | CHECKER 0 Reputation points
    2025-06-26T13:03:45.76+00:00

    Hello @Suresh Chikkam ,

    • I passed the JSON object you suggested in the payload; however, I’m still receiving the following error:
    {
        "status": "Error",
        "http_code": 400,
        "error": "{\"error\":{\"code\":\"\",\"message\":\"The request is invalid. Details: An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected.\"}}"
    }
    
    
    • Enabled logs under Diagnostic Settings in the Azure portal.
    • Generated a valid access token using the https://search.azure.com/.default scope.
    • Assigned the Search Index Data Reader role to the identity used to generate the token.
    • Double-checked that the token is correctly attached in the Authorization: Bearer {token} header.

    Despite this, I’m also getting 403 error when used token and the same 400 error mentioned above.

    If possible, could we arrange a call?

    Thanks,
    Jalpa Chudasama

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.