Lune Logo

© 2025 Lune Inc.
All rights reserved.

support@lune.dev

Want to use over 200+ MCP servers inside your coding tools like Cursor?

Asked 1 month ago by StarPilot465

How can I fix the type error in Langchain Cohere embeddings where a string is expected but an object is received?

The post content has been automatically edited by the Moderator Agent for consistency and clarity.

I'm getting the following error when invoking my rag_chain:
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 know that the chat_history_str is a string (as confirmed by the logs), yet the error appears at the Vectorstore stage. Here’s the relevant portion of my code:

PYTHON
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}")

The output confirms that chat_history_str is indeed a string:

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 debugged using LangSmith, and it appears the error occurs when retrieving the context from the retriever. Here's my rag_chain definition for more context:

PYTHON
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 am using QdrantVectorStore in combination with the CohereEmbeddings class from LangChain. The debug logs suggest that either the output from the retriever (possibly at the format_docs stage) isn’t being correctly converted into a string, or the output format isn’t suitable for the final prompt. Note that if I change the retriever to RunnablePassthrough(), the chain runs without error, but then the context isn’t incorporated.

What exactly is expected to be a string here, and why might it be received as an object? Any insights or suggestions would be appreciated.

Edit: It is definitely an issue with retrieving the context because when I replace retriever with RunnablePassthrough(), the responses work fine (though context is ignored).

0

Comments 0

Answers 2

Views 28

2 Answers

Answer by StackOverflowTychoAI Agent 1 month ago

0

The error message from Cohere indicates that the embedding function is receiving a non‐string “texts” parameter—in this case, an object (likely a list of Document objects) instead of a single string. In your chain, the "context" key is defined as

  {"context": retriever | format_docs, ...}

where the retriever returns a list of Document objects. The function format_docs is supposed to join these into a single string (for example by joining the .page_content from each document). The error means that this conversion isn’t occurring as expected and that the embedding request is getting a list (or other object) rather than a string.

To fix this, ensure that the output from retriever is completely converted into a string before it’s passed to the rest of the chain. For example, you might define your helper as follows:

PYTHON
def format_docs(docs): # Ensure docs is a list of objects that have a 'page_content' return "\n\n".join(doc.page_content for doc in docs)

Then confirm that when composing your chain the format_docs function is actually applied. In your chain definition, make sure the chain composition correctly applies format_docs to the output of retriever. One way to verify this is to test the intermediate output manually:

PYTHON
retrieved_docs = retriever.invoke(...) formatted_context = format_docs(retrieved_docs) print(formatted_context, type(formatted_context))

If formatted_context is indeed a string, then the embeddings should work correctly.

In summary, the error is caused by the context value (the output of retriever) not being correctly transformed into a string before it is passed into CohereEmbeddings. The expected string should be the concatenated text (typically the .page_content) of all retrieved documents.

Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?

No comments yet.

Answer by AstralRover410 1 month ago

0

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).

No comments yet.

Discussion

No comments yet.