[Python] ImportError: cannot import name 'Client' from 'pinecone' (unknown location)

Discussão em 'Python' iniciado por Stack, Julho 24, 2025.

  1. Stack

    Stack Membro Participativo

    The problem with this piece of code is that I am unable to import Client from the pinecone library. I tried to uninstalling and reinstalling different versions none of them worked. I also tried it with the pinecone-client library but none of it works. Any advice would be appreciated.

    Context of the code: I building a simple RAG which reads a txt file about Costco which provides an output depending on the input provided to the model.I am using the API's Pinecone and Huggingface.

    import os
    from langchain_community.document_loaders import TextLoader
    from langchain_community.embeddings import HuggingFaceEmbeddings
    from langchain_community.vectorstores import Pinecone
    from langchain_community.llms import HuggingFaceHub
    from langchain_core.prompts import PromptTemplate
    from langchain.chains import RetrievalQA # This is still in langchain core
    from dotenv import load_dotenv

    import pinecone
    print(pinecone.__version__)
    from pinecone import * # Importing the new Pinecone client
    print(pinecone.Client)

    # Initialize Pinecone client with new SDK
    pinecone_api_key = os.getenv("PINECONE_API_KEY")
    pinecone_environment = "us-east-1" # Your Pinecone environment (must match your index region)

    client = Client(api_key=pinecone_api_key, environment=pinecone_environment)

    index_name = "gentlecat"

    # Check if index exists, else create
    if index_name not in [idx.name for idx in client.list_indexes()]:
    client.create_index(name=index_name, dimension=384, metric="cosine")

    # Connect to the index
    index = client.index(index_name)

    # Load & split documents
    loader = TextLoader(r"filepath", encoding="utf-8")
    documents = loader.load()
    text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=4)
    docs = text_splitter.split_documents(documents)

    # Setup embeddings (make sure model outputs 384 dims)
    embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

    # Create Pinecone vectorstore with new client
    vectorstore = Pinecone.from_documents(docs, embeddings, index_name=index_name, client=client)

    # Setup LLM
    llm = HuggingFaceHub(
    repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
    model_kwargs={"temperature": 0.8, "top_p": 0.8, "top_k": 50},
    huggingfacehub_api_token=os.getenv("HUGGINGFACE_API_KEY"),
    )

    # Setup prompt template
    template = """You are a helpful assistant. Answer based on the Costco business context.

    Context: {context}
    Question: {question}
    Answer:"""

    prompt = PromptTemplate(template=template, input_variables=["context", "question"])

    # Create RetrievalQA chain
    qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    retriever=vectorstore.as_retriever(),
    chain_type_kwargs={"prompt": prompt},
    )

    # Example query
    query = "What is Costco's primary business model?"
    answer = qa_chain.run(query)
    print("Answer:", answer)


    Output:

    7.3.0
    Traceback (most recent call last):
    File "c:\filepath\main.py", line 14, in <module>
    print(pinecone.Client)
    File "C:\Users\FilePath\Python\Lib\site-packages\pinecone\utils\lazy_imports.py", line 65, in __getattr__
    raise AttributeError(f"module '{self._original_module.__name__}' has no attribute '{name}'")
    AttributeError: module 'pinecone' has no attribute 'Client'```

    Continue reading...

Compartilhe esta Página