TypeError("'NoneType' object is not callable")
Labels: name: area: agent, name: auto:bug

### System Info

OS: Redhat 8
Python: 3.9
Langchain: 0.0.246

### Who can help?

_No response_

### Information

- [X] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [ ] LLMs/Chat Models
- [ ] Embedding Models
- [X] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

So i initially reported this bug with mlflow, bug upon further investigation it is related to langchain.

Following code is simple representation of bigger code. I will also post that in bottom.

===================================================================

Code:
<pre><code>
import os
from langchain import PromptTemplate, LLMChain
from langchain.llms import HuggingFacePipeline


os.environ["CURL_CA_BUNDLE"] = ""
if True:  # run the following code to download the model flan-t5-small from huggingface.co
  from transformers import pipeline
  
  model = pipeline(model="google/flan-t5-small")  #'text2text-generation'
  model.save_pretrained("/tmp/model/flan-t5-small")

llm = HuggingFacePipeline.from_model_id(
    model_id="/tmp/model/flan-t5-small",
    task="text2text-generation",
    model_kwargs={"temperature": 1e-10},
)

template = """Translate everything you see after this into French:

{input}
"""

prompt = PromptTemplate(template=template, input_variables=["input"])

llm_chain = LLMChain(prompt=prompt, llm=llm)

print(llm_chain("my name is John"))  # works

llm_chain.save("llm_chain.json")

from langchain.chains import load_chain

m = load_chain("llm_chain.json")

print(m("my name is John"))
</code></pre>

Error trace:
<code><pre>
{'input': 'my name is John', 'text': " toutefois, je suis en uvre à l'heure"}
Traceback (most recent call last):
  File "a.py", line 37, in <module>
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/chains/base.py", line 258, in __call__
    raise e
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/chains/base.py", line 252, in __call__
    self._call(inputs, run_manager=run_manager)
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/chains/llm.py", line 92, in _call
    response = self.generate([inputs], run_manager=run_manager)
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/chains/llm.py", line 102, in generate
    return self.llm.generate_prompt(
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/base.py", line 451, in generate_prompt
    return self.generate(prompt_strings, stop=stop, callbacks=callbacks, **kwargs)
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/base.py", line 582, in generate
    output = self._generate_helper(
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/base.py", line 488, in _generate_helper
    raise e
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/base.py", line 475, in _generate_helper
    self._generate(
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/base.py", line 961, in _generate
    self._call(prompt, stop=stop, run_manager=run_manager, **kwargs)
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/huggingface_pipeline.py", line 168, in _call
    response = self.pipeline(prompt)
TypeError: 'NoneType' object is not callable
</code></pre>

===================================================================

Original Code where this bug occurred (MLFlow):
<code></pre>
import mlflow
from datetime import datetime
import logging

logging.getLogger("mlflow").setLevel(logging.DEBUG)

from langchain import PromptTemplate, LLMChain, HuggingFaceHub
from langchain.llms import HuggingFacePipeline
import os


def now_str():
  return datetime.now().strftime("%Y%m%d%H%M%S")


os.environ["CURL_CA_BUNDLE"] = ""
if True:
  from transformers import pipeline
  
  model = pipeline(model="google/flan-t5-small")  #'text2text-generation'
  model.save_pretrained("/tmp/model/flan-t5-small")

llm = HuggingFacePipeline.from_model_id(
    model_id="/tmp/model/flan-t5-small",
    task="text2text-generation",
    model_kwargs={"temperature": 1e-10},
)

template = """Translate everything you see after this into French:

{input}
"""

prompt = PromptTemplate(template=template, input_variables=["input"])

llm_chain = LLMChain(prompt=prompt, llm=llm)

print(llm_chain.run("my name is John"))  # This is working !!
### Output: {'input': 'my name is John', 'text': "j'ai le nom de John"}


experiment_id = mlflow.create_experiment(f"HF_LLM_{now_str()}")

with mlflow.start_run(experiment_id=experiment_id) as run:
    logged_model = mlflow.langchain.log_model(
        lc_model=llm_chain,
        artifact_path="HF_LLM",
    )

m = mlflow.langchain.load_model(logged_model.model_uri)
m.run("my name is John")
</code></pre>

Error Trace:
<code><pre>
Traceback (most recent call last):
  File "a.py", line 51, in <module>
    m.run("my name is John")
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/chains/base.py", line 451, in run
    return self(args[0], callbacks=callbacks, tags=tags, metadata=metadata)[
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/chains/base.py", line 258, in __call__
    raise e
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/chains/base.py", line 252, in __call__
    self._call(inputs, run_manager=run_manager)
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/chains/llm.py", line 92, in _call
    response = self.generate([inputs], run_manager=run_manager)
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/chains/llm.py", line 102, in generate
    return self.llm.generate_prompt(
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/base.py", line 451, in generate_prompt
    return self.generate(prompt_strings, stop=stop, callbacks=callbacks, **kwargs)
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/base.py", line 582, in generate
    output = self._generate_helper(
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/base.py", line 488, in _generate_helper
    raise e
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/base.py", line 475, in _generate_helper
    self._generate(
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/base.py", line 961, in _generate
    self._call(prompt, stop=stop, run_manager=run_manager, **kwargs)
  File "/home/haru/miniconda3/envs/mlflow-dev-env/lib/python3.8/site-packages/langchain/llms/huggingface_pipeline.py", line 168, in _call
    response = self.pipeline(prompt)
TypeError: 'NoneType' object is not callable
</code></pre>

===================================================================

Another Example:
<code><pre>
from datetime import datetime
import logging
logging.getLogger("mlflow").setLevel(logging.DEBUG)

from langchain import PromptTemplate, LLMChain, HuggingFaceHub
from langchain.llms import HuggingFacePipeline
import os


def now_str():
    return datetime.now().strftime("%Y%m%d%H%M%S")


os.environ["CURL_CA_BUNDLE"] = ''
if (True): # run the following code to download the model flan-t5-large from huggingface.co
    from transformers import pipeline
    model= pipeline(model="google/flan-t5-large") #'text2text-generation'
    model.save_pretrained("/tmp/model/flan-t5-large")

llm = HuggingFacePipeline.from_model_id(model_id="/tmp/model/flan-t5-large", task="text2text-generation", model_kwargs={"temperature":1e-10})

template = """Translate everything you see after this into French:

{input}
"""

prompt = PromptTemplate(template=template, input_variables=["input"])

llm_chain = LLMChain(
    prompt=prompt,
    llm=llm
)

llm_chain("my name is John") # This is working !!
#Output: {'input': 'my name is John', 'text': "j'ai le nom de John"}


experiment_id = mlflow.create_experiment(f'HF_LLM_{now_str()}')
    
with mlflow.start_run(experiment_id=experiment_id) as run:
        logged_model = mlflow.langchain.log_model(
            lc_model=llm_chain,
            artifact_path="HF_LLM",
        )

=============== This throws error ==============
input_str = "My name is John"

loaded_model = mlflow.pyfunc.load_model(logged_model.model_uri)
output = loaded_model.predict(
    [
        {
             "input": input_str
        },
        {
              "input": "Do you like coffee?"
        }
    ]
)

print(output)
</code></pre>

Error Trace:
<code><pre>
2023/08/07 08:57:08 WARNING mlflow.langchain.api_request_parallel_processor: Request #0 failed with TypeError("'NoneType' object is not callable")
2023/08/07 08:57:08 WARNING mlflow.langchain.api_request_parallel_processor: Request #1 failed with TypeError("'NoneType' object is not callable")
---------------------------------------------------------------------------
MlflowException                           Traceback (most recent call last)
<ipython-input-19-29b0feddacd1> in <cell line: 3>()
      3 with project.setup_mlflow(mf) as mlflow:
      4     loaded_model = mlflow.pyfunc.load_model(logged_model.model_uri)
----> 5     output = loaded_model.predict(
      6         [
      7             {

/hadoopfs/fs1/dataiku/data_dir/code-envs/python/mlflow_25_python_39/lib/python3.9/site-packages/mlflow/pyfunc/__init__.py in predict(self, data)
    426                     raise
    427 
--> 428         return self._predict_fn(data)
    429 
    430     @experimental

/hadoopfs/fs1/dataiku/data_dir/code-envs/python/mlflow_25_python_39/lib/python3.9/site-packages/mlflow/langchain/__init__.py in predict(self, data)
    654                 "Input must be a pandas DataFrame or a list of strings or a list of dictionaries",
    655             )
--> 656         return process_api_requests(lc_model=self.lc_model, requests=messages)
    657 
    658 

/hadoopfs/fs1/dataiku/data_dir/code-envs/python/mlflow_25_python_39/lib/python3.9/site-packages/mlflow/langchain/api_request_parallel_processor.py in process_api_requests(lc_model, requests, max_workers)
    138         # after finishing, log final status
    139         if status_tracker.num_tasks_failed > 0:
--> 140             raise mlflow.MlflowException(
    141                 f"{status_tracker.num_tasks_failed} tasks failed. See logs for details."
    142             )

MlflowException: 2 tasks failed. See logs for details.
</code></pre>

Looks like langchain doesn't restore the pipeline.

### Expected behavior

Langhain loads the chain successfully.