Repository: langchain
Issue: AzureSearch. Error when program terminates
Labels: Ɑ: vector store

### Checked other resources

- [X] I added a very descriptive title to this issue.
- [X] I searched the LangChain documentation with the integrated search.
- [X] I used the GitHub search to find a similar question and didn't find it.
- [X] I am sure that this is a bug in LangChain rather than my code.
- [X] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

### Example Code

#Failing code: 


import os

#loading environment variables from .env file
from dotenv import load_dotenv
load_dotenv()

from langchain_community.vectorstores.azuresearch import AzureSearch
from langchain_openai import AzureOpenAIEmbeddings

from azure.search.documents.indexes.models import (
    ScoringProfile,
    SearchableField,
    SearchField,
    SearchFieldDataType,
    SimpleField,
    TextWeights,
)

# This module is responsible for integration with Azure Search and uses Langchain framework for this
# It contains following functions:
# search - search for similar documents in Azure Search. return top 5 results
# ingest - gets as parameters a list of documents(chunks) and metadata per document and ingests them into Azure Search

# Azure Search configuration
AZURE_SEARCH_SERVICE_ENDPOINT = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")  
AZURE_SEARCH_API_KEY = os.getenv("AZURE_SEARCH_API_KEY")    
AZURE_SEARCH_INDEX_NAME = os.getenv("AZURE_SEARCH_INDEX_NAME")

# Azure OpenAI configuration
AZURE_OPENAI_KEY = os.getenv("AZURE_OPENAI_KEY")        
AZURE_OPENAI_DEPLOYMENT = os.getenv("AZURE_OPENAI_DEPLOYMENT")        
AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
AZURE_OPENAI_API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION")


# initialize AzureOpenAIEmbeddings 
embeddings: AzureOpenAIEmbeddings = \
    AzureOpenAIEmbeddings(azure_deployment=AZURE_OPENAI_DEPLOYMENT,
                          openai_api_version=AZURE_OPENAI_API_VERSION, 
                          azure_endpoint=AZURE_OPENAI_ENDPOINT,
                          api_key=AZURE_OPENAI_KEY)
    
#define search index custom schema

fields = [
    SimpleField(
        name="chunk_id",
        type=SearchFieldDataType.String,
        key=True,
        filterable=True,
    ),
     SimpleField(
        name="parent_id",
        type=SearchFieldDataType.String,
        key=True,
        filterable=True,
    ),
    SearchableField(
        name="chunk",
        type=SearchFieldDataType.String,
        searchable=True,
    ),
    SearchField(
        name="text_vector",
        type=SearchFieldDataType.Collection(SearchFieldDataType.Single),
        searchable=True,
        vector_search_dimensions=len(embeddings.embed_query("Text")),
        vector_search_profile_name="myHnswProfile",
    ),
    # Additional field to store the title
    SearchableField(
        name="title",
        type=SearchFieldDataType.String,
        searchable=True,
    ),
]

#create Langchain AzureSearch object
vector_search: AzureSearch = \
    AzureSearch(azure_search_endpoint=AZURE_SEARCH_SERVICE_ENDPOINT,
                                            azure_search_key=AZURE_SEARCH_API_KEY,
                                            index_name=AZURE_SEARCH_INDEX_NAME,
                                            embedding_function=embeddings.embed_query,
    # Configure max retries for the Azure client
    additional_search_client_options={"retry_total": 3},
    fields=fields,
)

# ingest - gets as parameters a list of documents(chunks) and metadata per document and ingests them into Azure Search
#TODO - implement async version of ingest
def ingest(documents: list, metadata):
    #check the input is valid list and non empty if not return exception
    if not isinstance(documents, list) or not documents:
        raise ValueError("Input must be a non-empty list")
    if not isinstance(metadata, list) or not metadata:
        raise ValueError("Metadata must be a non-empty list")
    if len(documents) != len(metadata):
        raise ValueError("Documents and metadata must be of the same length")
    
    # Ingest documents into Azure Search
    vector_search.add_documents(documents, metadata)


def search(query: str, search_type='similarity', top_k=5):
    #check the input is valid string and non empty if not raise exception
    if not isinstance(query, str) or not query:
        raise ValueError("Search query must be a non-empty string")
    # Search for similar documents
    docs = vector_search.similarity_search(query=query, k=top_k, search_type=search_type)
    return docs[0].page_content  


docs = search("Waht is Microsoft's Fabric?", search_type='hybrid', top_k=5)



### Error Message and Stack Trace (if applicable)

Exception ignored in: <function AzureSearch.__del__ at 0x123c86020>
Traceback (most recent call last):
  File "/Users/vladfeigin/myprojects/dai-demos/.venv/lib/python3.11/site-packages/langchain_community/vectorstores/azuresearch.py", line 393, in __del__
  File "/opt/homebrew/Cellar/python@3.11/3.11.10/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/events.py", line 765, in get_event_loop_policy
  File "/opt/homebrew/Cellar/python@3.11/3.11.10/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/events.py", line 758, in _init_event_loop_policy
ImportError: sys.meta_path is None, Python is likely shutting down

### Description

Running AzureSearch , hybrid search. The program executes properly but fails on termination 

### System Info

System Information
------------------
> OS:  Darwin
> OS Version:  Darwin Kernel Version 24.0.0: Tue Sep 24 23:39:07 PDT 2024; root:xnu-11215.1.12~1/RELEASE_ARM64_T6000
> Python Version:  3.11.10 (main, Sep  7 2024, 01:03:31) [Clang 15.0.0 (clang-1500.3.9.4)]

Package Information
-------------------
> langchain_core: 0.2.41
> langchain: 0.2.16
> langchain_community: 0.2.16
> langsmith: 0.1.136
> langchain_openai: 0.1.23
> langchain_text_splitters: 0.2.4
> langchainhub: 0.1.21

Optional packages not installed
-------------------------------
> langgraph
> langserve

Other Dependencies
------------------
> aiohttp: 3.10.5
> async-timeout: Installed. No version info available.
> dataclasses-json: 0.6.7
> httpx: 0.27.2
> jsonpatch: 1.33
> numpy: 1.26.4
> openai: 1.44.0
> orjson: 3.10.7
> packaging: 24.1
> pydantic: 2.9.0
> PyYAML: 6.0.2
> requests: 2.32.3
> requests-toolbelt: 1.0.0
> SQLAlchemy: 2.0.34
> tenacity: 8.5.0
> tiktoken: 0.7.0
> types-requests: 2.32.0.20240907
> typing-extensions: 4.12.2