# AttributeError: 'NoneType' object has no attribute 'api\_key'

**URL:** https://community.pinecone.io/t/attributeerror-nonetype-object-has-no-attribute-api-key/3398
**Category:** Support
**Tags:** vector-database
**Created:** [October 14, 2023, 3:32pm UTC](https://community.pinecone.io/t/attributeerror-nonetype-object-has-no-attribute-api-key/3398 "2023-10-14T15:32:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![angelommf21](https://sea2.discourse-cdn.com/flex020/user_avatar/community.pinecone.io/angelommf21/32/1397_2.png) [@angelommf21](https://community.pinecone.io/u/angelommf21)
#### Post date: [October 14, 2023, 3:32pm UTC](https://community.pinecone.io/t/attributeerror-nonetype-object-has-no-attribute-api-key/3398/1 "2023-10-14T15:32:48Z")

</div>

Why am I seeing this error from a file in the pinecone-client library when running my code? I saw that the chain runs properly on its own, the only problem seems to be its integration in the streamlit app, but why is that?

###############################################

AttributeError: ‘NoneType’ object has no attribute ‘api\_key’

Traceback:

```auto
File "/home/angelo/miniforge3/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 541, in _run_script
    exec(code, module. __dict__ )File "/home/angelo/Desktop/chatbot_3.py", line 135, in <module>
    idx = pinecone.Index('pgdl')File "/home/angelo/miniforge3/lib/python3.10/site-packages/pinecone/index.py", line 57, in __init__
    openapi_client_config.api_key = openapi_client_config.api_key or {}

```

############################################################

class StreamHandler(BaseCallbackHandler):  
def **init** (self, container, initial\_text=“”, display\_method=‘markdown’):  
self.container = container  
self.text = initial\_text  
self.display\_method = display\_method

```
def on_llm_new_token(self, token: str, **kwargs) -> None:
    self.text += token
    display_function = getattr(self.container, self.display_method, None)
    if display_function is not None:
        display_function(self.text)
    else:
        raise ValueError(f"Invalid display_method: {self.display_method}")

```

def run\_chatbot\_app():  
# Initialize Pinecone  
pinecone.init(api\_key=“xxxxxxxxxxxxxxxxxxxxxxx”, environment=gcp-free")  
OPENAI\_API\_KEY = ‘xxxxxxxxxxxxxxxxxxxxxxx’  
idx = pinecone.Index(‘pgdl’)

```
# retriever
hs_retriever = PineconeHybridSearchRetriever(
    embeddings=embed, sparse_encoder=sparse_encoder, index=idx, top_k=1, alpha=0.5
)

# conversational memory
conv_mem = ConversationBufferWindowMemory(
    memory_key = 'chat_history',
    k = 5,
    return_messages = True
)
    
# prompt template
template = "(...)".
{context}

Human: {human_input}
Chatbot:"""

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

# Streamlit app
st.title('ChatPGDL')

# Input for the user to enter their query
query = st.text_input('Make a question)

if st.button('Buscar'):
    # Chatbot
    st.subheader('Resposta:')

    # llm setup
    chat_box = st.empty()
    stream_handler = StreamHandler(chat_box, display_method='write')
    llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY, model_name="gpt-3.5-turbo",
            callbacks=[stream_handler], streaming=True
    )
    # Create the chain
    chain = ConversationalRetrievalChain.from_llm(
        llm=llm,
        retriever=hs_retriever,
        condense_question_prompt=prompt,
        memory=conv_mem
    )

    # Call the chain with the query
    output = chain({"question": query})
    
    # Get the chatbot's response
    response = output['answer']

```

run\_chatbot\_app()

---

<div class="post-metadata">

### Author: ![bear](https://sea2.discourse-cdn.com/flex020/user_avatar/community.pinecone.io/bear/32/1488_2.png) [@bear](https://community.pinecone.io/u/bear)
#### Post date: [October 17, 2023, 8:37pm UTC](https://community.pinecone.io/t/attributeerror-nonetype-object-has-no-attribute-api-key/3398/2 "2023-10-17T20:37:50Z")

</div>

That ‘NoneType’ error generally occurs when there is no instance of the class you’re calling- pinecone.init likely failed. It looks like you may be missing a `"` in `environment="gcp-free"` - try it out and let me know if that’s not the case.

---

<div class="post-metadata">

### Author: ![angelommf21](https://sea2.discourse-cdn.com/flex020/user_avatar/community.pinecone.io/angelommf21/32/1397_2.png) [@angelommf21](https://community.pinecone.io/u/angelommf21)
#### Post date: [October 26, 2023, 6:48pm UTC](https://community.pinecone.io/t/attributeerror-nonetype-object-has-no-attribute-api-key/3398/3 "2023-10-26T18:48:08Z")

</div>

I figured out eventually that what I was missing was storing the Pinecone API key and environment in the streamlit cache. So that one was actually on Streamlit. But thanks for the help!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex020/uploads/pinecone/original/1X/d16664eba369eeee623700cf12feab5562b37eaf.jpeg) [@system](https://community.pinecone.io/u/system)
#### Post date: [November 9, 2023, 6:49pm UTC](https://community.pinecone.io/t/attributeerror-nonetype-object-has-no-attribute-api-key/3398/4 "2023-11-09T18:49:00Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
