Akshitha Rao
Akshitha Rao

Reputation: 51

Langchain Cohere embeddings says "invalid type: parameter texts is of type object but should be of type string" inspite of receiving strings

This part of the code, specifically the part where rag_chain is invoked causes an error: Retrying langchain_cohere.embeddings.CohereEmbeddings.embed_with_retry.<locals>._embed_with_retry in 4.0 seconds as it raised UnprocessableEntityError: status_code: 422, body: {'message': 'invalid type: parameter texts is of type object but should be of type string. For proper usage, please refer to https://docs.cohere.com/v1/reference/embed'}.

question = input("Ask your question: ")
chat_history.append(f"user: {question}")


print("********************************************")
print(chat_history, type(chat_history))
print("********************************************")


while question != "Bye":

    chat_history_str = "\n".join(chat_history)
    print (chat_history_str, type(chat_history_str))
    print("++++++++++++++++++++++++++++++++++++++++++++")
    response = rag_chain.invoke(
        {
            'question': question, 'chat_history': chat_history_str
        }
    )
    print(response)
    print("----------------------------------------------")
    chat_history.append(f"AI: {response}")

Here, from the output, it is clear that chat_history_str is a string as this is what it shows:

Ask your question: i am injured
********************************************
['user: i am injured'] <class 'list'>
********************************************
user: i am injured <class 'str'>
++++++++++++++++++++++++++++++++++++++++++++
Retrying langchain_cohere.embeddings.CohereEmbeddings.embed_with_retry.<locals>._embed_with_retry in 4.0 seconds as it raised UnprocessableEntityError: status_code: 422, body: {'message': 'invalid type: parameter texts is of type object but should be of type string. For proper usage, please refer to https://docs.cohere.com/v1/reference/embed'}.
Retrying langchain_cohere.embeddings.CohereEmbeddings.embed_with_retry.<locals>._embed_with_retry in 4.0 seconds as it raised UnprocessableEntityError: status_code: 422, body: {'message': 'invalid type: parameter texts is of type object but should be of type string. For proper usage, please refer to https://docs.cohere.com/v1/reference/embed'}.

I logged on to LangSmith to debug, and it shows the error at the Vectorstore stage.

Langsmith trace

For more context, my rag_chain is as follows:

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough().pick("question"), 
     "input": RunnablePassthrough().pick("question"), "chat_history": RunnablePassthrough().pick("chat_history")}
    | final_prompt
    | llm
    | StrOutputParser()
)

I have used QdrantVectorStore and CohereEmbeddings class from langchain.

I set debug as true, and from what it showed, it looks like the problem is either while getting the context from the retriever (it may be at format_docs stage; the retriever output doesn't seem to reach there at all), or the output format is not suitable for the final prompt. In either case, getting the question or the chat history seems to work fine.

What exactly is supposed to be a string here, that's being received as an object?

Edit: It is definitely a problem while getting the context as changing retriever to RunnablePassthrough() gave responses without errors, though without considering the context. Outside the chain, retriever.invoke works perfectly fine.

Upvotes: 0

Views: 34

Answers (1)

Akshitha Rao
Akshitha Rao

Reputation: 51

I understood my mistake. It was a problem with how the input was being received by the retriever. The input was a json which had to be converted to a string in a separate function and the output should be piped to the retriever.

Another issue was that langchain-qdrant is compatible with python versions < 3.13 and mine was 3.13.1. So I installed Python3.10 and was good to go (Main problem was the first point; the python version change was just to resolve some minor compatibility issues).

Upvotes: 0

Related Questions