Azure / Azure/azure-sdk-for-python
Azure AI Search call returns ResourceNotFoundError
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 3.4k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 193
Description
- **Package Name**: azure-search-documents, azure-ai-inference
- **Package Version**: azure-search-documents==11.5.2, azure-ai-inference==1.0.0b9
- **Operating System**: Windows 11 24H2
- **Python Version**: 3.11.7
**Describe the bug**
Code raises a `ResourceNotFoundError` when attempting to retrieve search results using the `SearchClient`. The error occurs because the search operation returns a "Not Found" status, indicating that the specified index ("legal") does not exist or the endpoint is incorrect. Index name has been verified on Azure Web repeatedly, and alias has also been tried. Index exists for certain on Azure.
**To Reproduce**
Steps to reproduce the behavior:
1. Set up environment variables `SEARCH_ENDPOINT`, `SEARCH_KEY`, `CHAT_ENDPOINT`, and `CHAT_KEY` in a `.env` file.
2. Ensure the Azure Cognitive Search service is running and accessible.
3. Run the code with any query:
Code example:
```
from dotenv import load_dotenv
import os
load_dotenv()
CHAT_ENDPOINT = os.getenv("CHAT_ENDPOINT")
CHAT_KEY = os.getenv("CHAT_KEY")
SEARCH_ENDPOINT = os.getenv("SEARCH_ENDPOINT")
SEARCH_KEY = os.getenv("SEARCH_KEY")
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import SystemMessage, UserMessage
from azure.search.documents import SearchClient
from azure.core.credentials import AzureKeyCredential
chat_client = ChatCompletionsClient(
endpoint = CHAT_ENDPOINT,
credential = AzureKeyCredential(CHAT_KEY),
max_tokens = 500
)
search_client = SearchClient(
endpoint = SEARCH_ENDPOINT,
index_name = "legal",
credential = AzureKeyCredential(SEARCH_KEY)
)
def retrieve_context(query):
results = search_client.search(search_text=query, top=3)
return "\n".join([doc["content"] for doc in results])
def generate_response(query):
context = retrieve_context(query)
prompt = f"Use the following context to answer the question: \n{context}\nQuestion: {query}\nAnswer"
response = chat_client.complete(messages=[
SystemMessage(content="you"),
UserMessage(content=prompt)
])
return response.choices[0].message.content
query = "Which department published this file?"
answer = generate_response(query)
print(answer)
```
Error Details
```
(azure) PS C:\azuretest> python .\somecode.py
Traceback (most recent call last):
File "C:\azuretest\somecode.py", line 57, in
answer = generate_response(query)
File "C:\azuretest\somecode.py", line 46, in generate_response
context = retrieve_context(query)
File "C:\azuretest\somecode.py", line 42, in retrieve_context
return "\n".join([doc["content"] for doc in results])
File "C:\azuretest\somecode.py", line 42, in
return "\n".join([doc["content"] for doc in results])
File "C:\azuretest\azure\lib\site-packages\azure\search\documents\_paging.py", line 54, in __next__
return next(self._page_iterator)
File "C:\azuretest\azure\lib\site-packages\azure\core\paging.py", line 75, in __next__
self._response = self._get_next(self.continuation_token)
File "C:\azuretest\azure\lib\site-packages\azure\search\documents\_paging.py", line 125, in _get_next_cb
return self._client.documents.search_post(search_request=self._initial_query.request, **self._kwargs)
File "C:\azuretest\azure\lib\site-packages\azure\core\tracing\decorator.py", line 119, in wrapper_use_tracer
return func(*args, **kwargs)
File "C:\azuretest\azure\lib\site-packages\azure\search\documents\_generated\operations\_documents_operations.py", line 752, in search_post
map_error(status_code=response.status_code, response=response, error_map=error_map)
File "C:\azuretest\azure\lib\site-packages\azure\core\exceptions.py", line 163, in map_error
raise error
azure.core.exceptions.ResourceNotFoundError: Operation returned an invalid status 'Not Found'
```
**Expected behavior**
Should instead return the answer to the query. Does do so when called through an API test platform such as Postman using the exact same credentials, URLs and keys, with the only addition of the Bearer token for the web.
**Screenshots**
**Additional context**
Have uploaded a PDF to Azure, and am using that Azure Index Name or Alias to call it in both API and SDK, API one works, but SDK one doesn't.
Contributor guide
Assessment
This issue has not been assessed yet.