Azure/azure-search-vector-samples

Cannot convert the literal '0' to the expected type 'Edm.String'

Closed this issue ยท 2 comments

Hello there ๐Ÿ‘‹

While trying to upload my documents to my index on Azure AI search:

Upload the documents to the index

with open('../data/processed/data.json', 'r') as file:  
    documents = json.load(file)

search_client = SearchClient(
    endpoint=service_endpoint, 
    index_name=index_name, 
    credential=credential
)
result = search_client.upload_documents(documents)
print(f"Uploaded {len(documents)} documents")

Error

I get the following error:

HttpResponseError: () The request is invalid. Details: Cannot convert the literal '0' to the expected type 'Edm.String'.
Code: 
Message: The request is invalid. Details: Cannot convert the literal '0' to the expected type 'Edm.String'.

Structure of the document

The document is loaded from a JSON file as an array of dictionnaries. The tilte_vector and content_vector represent embeddings of the title and the content fields respectively.
Item form my array of dictionaries have the following structure:

{
   'id': 0,
   'title': 'About Papers Inc. ',
   'file_name': 'About Papers Inc.pdf',
   'content': "About Papers Inc. \n \nPapers Inc. is a dynamic",
   'title_vector': [0.0232033543, -0.0197890475, ... 0.0308263171, 0.0126190009],
   'content_vector': [0.0232033543, -0.0197890475, ... 0.0308263171, 0.0126190009]
}

Rest of the code

Below is the code is used to create the index.

Configure the index

# Initialize the index client
index_client = SearchIndexClient(
    endpoint=service_endpoint, 
    credential=credential
)

# Prepare the fields
fields = [
    SimpleField(name="id", type=SearchFieldDataType.String, 
        key=True, sortable=True, filterable=True, facetable=True),
    SearchableField(name="file_name", type=SearchFieldDataType.String),
    SearchableField(name="title", type=SearchFieldDataType.String),
    SearchableField(name="content", type=SearchFieldDataType.String),
    SearchField(name="title_vector", type=SearchFieldDataType.Collection(SearchFieldDataType.Single),
                searchable=True, vector_search_dimensions=1536, vector_search_profile_name="myHnswProfile"),
    SearchField(name="content_vector", type=SearchFieldDataType.Collection(SearchFieldDataType.Single),
                searchable=True, vector_search_dimensions=1536, vector_search_profile_name="myHnswProfile"),
]

Configure the vector search configuration

...

Configure semantic search

semantic_config = SemanticConfiguration(
    name="my-semantic-config",
    prioritized_fields=SemanticPrioritizedFields(
        title_field=SemanticField(field_name="title"),
        # keywords_fields=[SemanticField(field_name="category")],
        content_fields=[SemanticField(field_name="content")]
    )
)

Create the index

# Init the semantic search settings with the configuration
semantic_search = SemanticSearch(configurations=[semantic_config])

# Init the search index with the semantic settings
index = SearchIndex(name=index_name, fields=fields,
                    vector_search=vector_search,
                    semantic_search=semantic_search
)

# Create the index
result = index_client.create_or_update_index(index)
print(f' {result.name} created')

Your help would be much appreciated.

Correct, when using the push API you need to convert the number 0 to the string 0. Example:

doc['id'] = str(doc['id'])
# rest of your code

Correct, when using the push API you need to convert the number 0 to the string 0. Example:

doc['id'] = str(doc['id'])
# rest of your code

It worked perfectly. I was stuck because of this.
Thank you so much Matt!