TypeError: 'module' object is not callable when building chatbot with OpenAI

I’m working on a chatbot project using the OpenAI library and keep getting this error. My Python version is 3.8.3.

Here’s my code:

api_file = open("config\\key.txt", "r")
api_key = api_file.read()
api_file.close()

import openai
from dotenv import load_dotenv

openai.api_key = api_key
load_dotenv()
api_client = openai.Completion()

def ChatResponse(user_input, conversation_log=None):
    log_file = openai("", "r")
    default_conversation = log_file.read()
    log_file.close()
    
    if conversation_log is None:
        conversation_log = default_conversation
    
    chat_prompt = f'{conversation_log}User: {user_input}\nBot: '
    result = api_client.create(
        model="text-davinci-002",
        prompt=chat_prompt,
        temperature=0.5,
        max_tokens=60,
        top_p=0.3,
        frequency_penalty=0.5,
        presence_penalty=0
    )
    bot_response = result.choices[0].text.strip()
    updated_log = default_conversation + f"\nUser: {user_input}\nBot: {bot_response}"
    log_file = open("", "w")
    log_file.write(updated_log)
    log_file.close()
    return bot_response

while True:
    user_question = input("Ask me: ")
    ChatResponse(user_question)

The error I encountered is:

line 13, in ChatResponse
    log_file = openai("", "r")
TypeError: 'module' object is not callable

What is causing this issue and how can I fix it?

Beyond the typo, there’s another issue that’ll cause problems. You’re doing api_client = openai.Completion() but that’s not how the OpenAI library works. You need to call openai.Completion.create() directly on the module instead. Depending on your OpenAI version, you might want openai.ChatCompletion.create() for better results. I hit the same issue when migrating between library versions - the API changed big time between v0.x and v1.x. Run pip show openai to check your version and look at the docs for the right syntax.

heads up - you’re missing error handling for when the file doesn’t exist. log_file = open("", "r") will crash after you fix the openai typo since there’s no filename. wrap it in try/except or use os.path.exists() to check first. learned this one the hard way lol

The problem’s on line 13 - you wrote log_file = openai("", "r") but you’re treating the openai module like a function. It’s not callable.

You want Python’s open() function:

log_file = open("", "r")  # not openai("", "r")

I’ve done this exact same thing when rushing through API work. Easy mistake when the module name’s stuck in your head.

Also, your file path is empty. You need something like:

log_file = open("conversation_log.txt", "r")

Same thing happens later where you’re writing the file. Double-check all your file paths are actually specified.

I’m seeing the same bug others mentioned, but here’s a better approach I’ve used for chatbot projects.

Skip wrestling with file handling and API calls manually - I built my last chatbot using Latenode. The OpenAI integration becomes drag and drop, so no more typos like openai("", "r") when you meant open("", "r").

Latenode handles conversation logging automatically. No manually reading/writing files or managing conversation state. Just connect OpenAI node to a database node and it stores everything.

I deployed a customer service bot this way last month. Took maybe 30 minutes to set up vs the hours I used to spend debugging Python file operations and API calls.

Your current approach will work once you fix the typo, but you’ll hit more issues with file locking, error handling, and scaling. Latenode removes all that complexity.