Bug when using OPENAI_FUNCTIONS agent
Labels: name: area: agent, name: auto:bug, name: area: models

### System Info

python=3.10
langchain=0.0.320

### Who can help?

@hwchase17

### Information

- [X] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [X] LLMs/Chat Models
- [ ] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [X] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [X] Agents / Agent Executors
- [X] Tools / Toolkits
- [ ] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

# Bug

https://python.langchain.com/docs/modules/agents/how_to/custom-functions-with-openai-functions-agent
I am trying to reproduce this example. But I got the below error. 
The error happends in the below function https://api.python.langchain.com/en/latest/_modules/langchain/chat_models/openai.html#ChatOpenAI:~:text=%3Dchunk)-,def%20_generate(,-self%2C%0A%20%20%20%20%20%20%20%20messages

After I printed the "message_dicts" in the function  I found that one item in the list has a None "content" value.

``` python
[{'role': 'system', 'content': 'You are a helpful AI assistant.'}, {'role': 'user', 'content': 'What is the current price of Microsoft stock? How it has performed over past 6 months?'}, {'role': 'assistant', 'content': None, 'function_call': {'name': 'get_current_stock_price', 'arguments': '{\n  "ticker": "MSFT"\n}'}}, {'role': 'function', 'content': '{"price": 326.6700134277344, "currency": "USD"}', 'name': 'get_current_stock_price'}]
```
The **None** value will lead to an error comlainning that: 
```
{
  "error": {
    "code": null,
    "param": null,
    "message": "'content' is a required property - 'messages.2'",
    "type": "invalid_request_error"
  }
}
```
# My findings: 
If we use the function_call features of OPENAI, it will always return a None value in the content. But it seems like, the langchain implementation does not deal with this situation. 


# My fix:


After I add one line of code in the _generate function to replace the `None` value with an empty string `''`:
```python
        message_dicts = [{**item, 'content': ('' if item['content'] is None else item['content'])} for item in message_dicts] # this line is added 
        response = self.completion_with_retry(
            messages=message_dicts, run_manager=run_manager, **params
        )
        print(response)
        return self._create_chat_result(response)

```
Then the function works normally.

 Is this a bug in using the langchain OPENAI_FUNCTIONS agent? 










### Expected behavior

# My fix:


After I add one line of code in the _generate function to replace the `None` value with an empty string `''`:
```python
        message_dicts = [{**item, 'content': ('' if item['content'] is None else item['content'])} for item in message_dicts] # this line is added 
        response = self.completion_with_retry(
            messages=message_dicts, run_manager=run_manager, **params
        )
        print(response)
        return self._create_chat_result(response)

```
Then the function works normally.

The output (with some printing) is like this:
``` 
[{'role': 'system', 'content': 'You are a helpful AI assistant.'}, {'role': 'user', 'content': 'What is the current price of Microsoft stock? How it has performed over past 6 months?'}]
{
  "created": 1698074688,
  "usage": {
    "completion_tokens": 18,
    "prompt_tokens": 199,
    "total_tokens": 217
  },
  "model": "gpt-35-turbo",
  "id": "chatcmpl-8Cr5M7Zi8wv40g9dEg4ZbkNV0KBSc",
  "choices": [
    {
      "finish_reason": "function_call",
      "index": 0,
      "message": {
        "role": "assistant",
        "function_call": {
          "name": "get_current_stock_price",
          "arguments": "{\n  \"ticker\": \"MSFT\"\n}"
        },
        "content": null
      }
    }
  ],
  "object": "chat.completion"
}

Invoking: `get_current_stock_price` with `{'ticker': 'MSFT'}`


{'price': 328.9849853515625, 'currency': 'USD'}[{'role': 'system', 'content': 'You are a helpful AI assistant.'}, {'role': 'user', 'content': 'What is the current price of Microsoft stock? How it has performed over past 6 months?'}, {'role': 'assistant', 'content': None, 'function_call': {'name': 'get_current_stock_price', 'arguments': '{\n  "ticker": "MSFT"\n}'}}, {'role': 'function', 'content': '{"price": 328.9849853515625, "currency": "USD"}', 'name': 'get_current_stock_price'}]
{
  "created": 1698074692,
  "usage": {
    "completion_tokens": 24,
    "prompt_tokens": 228,
    "total_tokens": 252
  },
  "model": "gpt-35-turbo",
  "id": "chatcmpl-8Cr5QoQfx5gkIS9k7mGn2fs7lMkPc",
  "choices": [
    {
      "finish_reason": "function_call",
      "index": 0,
      "message": {
        "role": "assistant",
        "function_call": {
          "name": "get_stock_performance",
          "arguments": "{\n  \"ticker\": \"MSFT\",\n  \"days\": 180\n}"
        },
        "content": null
      }
    }
  ],
  "object": "chat.completion"
}

Invoking: `get_stock_performance` with `{'ticker': 'MSFT', 'days': 180}`


{'percent_change': 7.626317098142607}[{'role': 'system', 'content': 'You are a helpful AI assistant.'}, {'role': 'user', 'content': 'What is the current price of Microsoft stock? How it has performed over past 6 months?'}, {'role': 'assistant', 'content': None, 'function_call': {'name': 'get_current_stock_price', 'arguments': '{\n  "ticker": "MSFT"\n}'}}, {'role': 'function', 'content': '{"price": 328.9849853515625, "currency": "USD"}', 'name': 'get_current_stock_price'}, {'role': 'assistant', 'content': None, 'function_call': {'name': 'get_stock_performance', 'arguments': '{\n  "ticker": "MSFT",\n  "days": 180\n}'}}, {'role': 'function', 'content': '{"percent_change": 7.626317098142607}', 'name': 'get_stock_performance'}]
{
  "created": 1698074694,
  "usage": {
    "completion_tokens": 36,
    "prompt_tokens": 251,
    "total_tokens": 287
  },
  "model": "gpt-35-turbo",
  "id": "chatcmpl-8Cr5SRASyFLFbp80CJhx5hgdVXDLM",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The current price of Microsoft stock is $328.98 USD. Over the past 6 months, the stock has performed with a 7.63% increase in price."
      }
    }
  ],
  "object": "chat.completion"
}
The current price of Microsoft stock is $328.98 USD. Over the past 6 months, the stock has performed with a 7.63% increase in price.

```

 Is this a bug in using the langchain OPENAI_FUNCTIONS agent?