Repository: langchain
Issue: Map Reduce Chain bug: "TypeError: unsupported operand type(s) for +=: 'NoneType' and 'NoneType'"
Labels: 🤖:bug,investigate

### Checked other resources

- [X] I added a very descriptive title to this issue.
- [X] I searched the LangChain documentation with the integrated search.
- [X] I used the GitHub search to find a similar question and didn't find it.
- [X] I am sure that this is a bug in LangChain rather than my code.
- [X] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

### Example Code

```
from langchain.chains import (
    StuffDocumentsChain, LLMChain, ReduceDocumentsChain, MapReduceChain
)
from langchain.chains.combine_documents.map_reduce import MapReduceDocumentsChain
from langchain.text_splitter import CharacterTextSplitter


def baseChain(llm, chat_prompt, output_name):
    base_chain = LLMChain(llm=llm['instance'], prompt=chat_prompt, output_key=output_name, verbose=True)
    return base_chain


def stuffDocChain(reduce_llm_chain):
    combine_documents_chain = StuffDocumentsChain(
        llm_chain=reduce_llm_chain,
        document_variable_name="input_text",
    )
    return combine_documents_chain


# Combines and iteravely reduces the mapped documents
def combine_and_reduce(combine_documents_chain, map_max_tokens):
    reduce_documents_chain = ReduceDocumentsChain(
        # This is final chain that is called.
        combine_documents_chain=combine_documents_chain,
        # If documents exceed context for `combine_documents_chain`
        collapse_documents_chain=combine_documents_chain,
        # The maximum number of tokens to group documents into
        token_max=map_max_tokens)
    return reduce_documents_chain


# Combining documents by mapping a chain over them, then combining results with reduce chain
def map_reduce_step(map_llm_chain, reduce_documents_chain):
    combine_documents = MapReduceDocumentsChain(
        # Map chain
        llm_chain=map_llm_chain,
        # Reduce chain
        reduce_documents_chain=reduce_documents_chain,
        # The variable name in the llm_chain to put the documents in
        document_variable_name="input_text")

    return combine_documents


def mapReduceChain(combine_documents, chunk_size, chunk_overlap):
    map_reduce = MapReduceChain(
        combine_documents_chain=combine_documents,
        text_splitter=CharacterTextSplitter(separator="", chunk_size=chunk_size, chunk_overlap=chunk_overlap),
        verbose=True)  # TODO add seperator protos
    return map_reduce



def run_map_chain(llm, map_prompt, combine_prompt, chunk_size, chunk_overlap, inputs, output_name, map_max_tokens, thread_logs):
    # Processing to required prompt templates
    chunk_prompt_template, combine_prompt_template = map_process_prompt(map_prompt, combine_prompt)

    # LLM to use in map and reduce stages
    map_llm_chain = baseChain(llm, chunk_prompt_template, output_name)
    reduce_llm_chain = baseChain(llm, combine_prompt_template, output_name)

    # Takes a list of documents and combines them into a single string
    combine_documents_chain = stuffDocChain(reduce_llm_chain)

    # Combines and iteravely reduces the mapped documents
    reduce_documents_chain = combine_and_reduce(combine_documents_chain, map_max_tokens)

    # Combining documents by mapping a chain over them, then combining results with reduce chain
    combine_documents = map_reduce_step(map_llm_chain, reduce_documents_chain)

    map_chain = mapReduceChain(combine_documents, chunk_size, chunk_overlap)

    # Calculate input tokens
    total_input = str(chunk_prompt_template) + str(combine_prompt_template) + str(inputs)
    total_tokens = num_tokens_from_string(total_input, "cl100k_base")

    logger.info(f"Total input tokens are: {total_tokens}")

    with get_openai_callback() as cb:
        start_time = time.time()
        output = map_chain.__call__(inputs)
        time_taken = time.time() - start_time
        logger.info(f"Time take for LLM Request to respond :{time_taken} seconds")
        logger.info(f"Total Tokens: {cb.total_tokens} key: {llm['key_index']}")
        logger.info(f"Prompt Tokens: {cb.prompt_tokens} key: {llm['key_index']}")
        logger.info(f"Completion Tokens: {cb.completion_tokens} key: {llm['key_index']}")
        
        if isinstance(output["output_text"], dict):
            output_str = json.dumps(output["output_text"])
        
        else:
            output_str = output["output_text"]
            
        formatted_input = total_input
        model_cost_per_token = get_model_cost(llm)

    return output

```

### Error Message and Stack Trace (if applicable)

```


Traceback (most recent call last):  File "/var/task/handler/langchain_map_reduce_handler.py", line 151, in run    output = run_map_chain(llm, map_prompt, combine_prompt, chunk_size, chunk_overlap, inputs, output_name,  File "/var/task/handler/langchain_map_reduce_handler.py", line 83, in run_map_chain    output = map_chain.__call__(inputs)  File "/var/task/langchain_core/_api/deprecation.py", line 148, in warning_emitting_wrapper    return wrapped(*args, **kwargs)  File "/var/task/langchain/chains/base.py", line 378, in __call__    return self.invoke(  File "/var/task/langchain/chains/base.py", line 163, in invoke    raise e  File "/var/task/langchain/chains/base.py", line 153, in invoke    self._call(inputs, run_manager=run_manager)  File "/var/task/langchain/chains/mapreduce.py", line 105, in _call    outputs = self.combine_documents_chain.run(  File "/var/task/langchain_core/_api/deprecation.py", line 148, in warning_emitting_wrapper    return wrapped(*args, **kwargs)  File "/var/task/langchain/chains/base.py", line 595, in run    return self(args[0], callbacks=callbacks, tags=tags, metadata=metadata)[  File "/var/task/langchain_core/_api/deprecation.py", line 148, in warning_emitting_wrapper    return wrapped(*args, **kwargs)  File "/var/task/langchain/chains/base.py", line 378, in __call__    return self.invoke(  File "/var/task/langchain/chains/base.py", line 163, in invoke    raise e  File "/var/task/langchain/chains/base.py", line 153, in invoke    self._call(inputs, run_manager=run_manager)  File "/var/task/langchain/chains/combine_documents/base.py", line 137, in _call    output, extra_return_dict = self.combine_docs(  File "/var/task/langchain/chains/combine_documents/map_reduce.py", line 226, in combine_docs    map_results = self.llm_chain.apply(  File "/var/task/langchain/chains/llm.py", line 250, in apply    raise e  File "/var/task/langchain/chains/llm.py", line 247, in apply    response = self.generate(input_list, run_manager=run_manager)  File "/var/task/langchain/chains/llm.py", line 138, in generate    return self.llm.generate_prompt(  File "/var/task/langchain_core/language_models/chat_models.py", line 560, in generate_prompt    return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)  File "/var/task/langchain_core/language_models/chat_models.py", line 426, in generate    llm_output = self._combine_llm_outputs([res.llm_output for res in results])  File "/var/task/langchain_openai/chat_models/base.py", line 504, in _combine_llm_outputs    overall_token_usage[k] += vTypeError: unsupported operand type(s) for +=: 'NoneType' and 'NoneType' | [ERROR] 2024-10-10T15:56:01.430Z 6a9966a6-e97c-4860-bac6-8be0464b1bb2 unsupported operand type(s) for +=: 'NoneType' and 'NoneType' Traceback (most recent call last): File "/var/task/handler/langchain_map_reduce_handler.py", line 151, in run output = run_map_chain(llm, map_prompt, combine_prompt, chunk_size, chunk_overlap, inputs, output_name, File "/var/task/handler/langchain_map_reduce_handler.py", line 83, in run_map_chain output = map_chain.__call__(inputs) File "/var/task/langchain_core/_api/deprecation.py", line 148, in warning_emitting_wrapper return wrapped(*args, **kwargs) File "/var/task/langchain/chains/base.py", line 378, in __call__ return self.invoke( File "/var/task/langchain/chains/base.py", line 163, in invoke raise e File "/var/task/langchain/chains/base.py", line 153, in invoke self._call(inputs, run_manager=run_manager) File "/var/task/langchain/chains/mapreduce.py", line 105, in _call outputs = self.combine_documents_chain.run( File "/var/task/langchain_core/_api/deprecation.py", line 148, in warning_emitting_wrapper return wrapped(*args, **kwargs) File "/var/task/langchain/chains/base.py", line 595, in run return self(args[0], callbacks=callbacks, tags=tags, metadata=metadata)[ File "/var/task/langchain_core/_api/deprecation.py", line 148, in warning_emitting_wrapper return wrapped(*args, **kwargs) File "/var/task/langchain/chains/base.py", line 378, in __call__ return self.invoke( File "/var/task/langchain/chains/base.py", line 163, in invoke raise e File "/var/task/langchain/chains/base.py", line 153, in invoke self._call(inputs, run_manager=run_manager) File "/var/task/langchain/chains/combine_documents/base.py", line 137, in _call output, extra_return_dict = self.combine_docs( File "/var/task/langchain/chains/combine_documents/map_reduce.py", line 226, in combine_docs map_results = self.llm_chain.apply( File "/var/task/langchain/chains/llm.py", line 250, in apply raise e File "/var/task/langchain/chains/llm.py", line 247, in apply response = self.generate(input_list, run_manager=run_manager) File "/var/task/langchain/chains/llm.py", line 138, in generate return self.llm.generate_prompt( File "/var/task/langchain_core/language_models/chat_models.py", line 560, in generate_prompt return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs) File "/var/task/langchain_core/language_models/chat_models.py", line 426, in generate llm_output = self._combine_llm_outputs([res.llm_output for res in results]) File "/var/task/langchain_openai/chat_models/base.py", line 504, in _combine_llm_outputs overall_token_usage[k] += v TypeError: unsupported operand type(s) for +=: 'NoneType' and 'NoneType'
  | 2024-10-10T15:56:01.433Z |  

<br class="Apple-interchange-newline">```

### Description

MaP Reduce chain breaks internally during token calculation intermitently

### System Info


System Information
------------------
> OS:  Darwin
> OS Version:  Darwin Kernel Version 23.5.0: Wed May  1 20:13:18 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6030
> Python Version:  3.9.19 (main, Jul 21 2024, 14:15:50) 
[Clang 15.0.0 (clang-1500.3.9.4)]

Package Information
-------------------
> langchain_core: 0.1.52
> langchain: 0.1.20
> langchain_community: 0.0.38
> langsmith: 0.1.17
> langchain_aws: 0.1.4
> langchain_openai: 0.1.4
> langchain_text_splitters: 0.0.2

Packages not installed (Not Necessarily a Problem)
--------------------------------------------------
The following packages were not found:

> langgraph
> langserve