Repository: langchain
Issue: AzureMLChatOnlineEndpoint does NOT support HumanMessage with list of dict content 
Labels: 🤖:bug

### 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

```python
import json
import os
from langchain_core.messages import HumanMessage
from langchain_community.chat_models.azureml_endpoint import (
    AzureMLChatOnlineEndpoint,
    CustomOpenAIChatContentFormatter,
    AzureMLEndpointApiType
)
from langchain_groq import ChatGroq



# open the .config.json file
config = json.load(open(os.path.expanduser("../config.json")))
timeout = 60 * 5

# llm = AzureMLChatOnlineEndpoint(
#     endpoint_url=config["models"][0]["url"],
#     endpoint_api_type=AzureMLEndpointApiType.serverless,
#     endpoint_api_key=config["models"][0]["key"],
#     content_formatter=CustomOpenAIChatContentFormatter(),
#     timeout=timeout
# )

llm = ChatGroq(
    model="llama-3.1-8b-instant",  # "llama-3.2-11b-vision-preview",
    api_key=config["models"][1]["key"],
    timeout=timeout
)


message = HumanMessage(
    content=[
        {"type": "text", "text": "describe the weather in this image"},
    ],
)
response = llm.invoke([message])
print(response.content)

```

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

if the LLM is the Groq one no error is raised

if the LLM is the Azure one, the error is the following:
```
"C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Scripts\python.exe" C:\Users\...\git\MultiModelPlayground\mmp\MultiModelPlayground\test_folder\multimodal_azure_ai.py 
Traceback (most recent call last):
  File "C:\Users\...\git\MultiModelPlayground\mmp\MultiModelPlayground\test_folder\multimodal_azure_ai.py", line 214, in <module>
    response = llm.invoke([message])
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_core\language_models\chat_models.py", line 286, in invoke
    self.generate_prompt(
  File "C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_core\language_models\chat_models.py", line 786, in generate_prompt
    return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_core\language_models\chat_models.py", line 643, in generate
    raise e
  File "C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_core\language_models\chat_models.py", line 633, in generate
    self._generate_with_cache(
  File "C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_core\language_models\chat_models.py", line 851, in _generate_with_cache
    result = self._generate(
             ^^^^^^^^^^^^^^^
  File "C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_community\chat_models\azureml_endpoint.py", line 274, in _generate
    request_payload = self.content_formatter.format_messages_request_payload(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_community\chat_models\azureml_endpoint.py", line 105, in format_messages_request_payload
    chat_messages = [
                    ^
  File "C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_community\chat_models\azureml_endpoint.py", line 106, in <listcomp>
    CustomOpenAIChatContentFormatter._convert_message_to_dict(message)
  File "C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_community\chat_models\azureml_endpoint.py", line 65, in _convert_message_to_dict
    "content": ContentFormatterBase.escape_special_characters(content),
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_community\llms\azureml_endpoint.py", line 136, in escape_special_characters
    prompt = prompt.replace(escape_sequence, escaped_sequence)
             ^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'replace'

Process finished with exit code 1
```

And the method in which the error is actually raised is the following: 
**C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_community\llms\azureml_endpoint.py**
```python
@staticmethod
    def escape_special_characters(prompt: str) -> str:
        """Escapes any special characters in `prompt`"""
        escape_map = {
            "\\": "\\\\",
            '"': '\\"',
            "\b": "\\b",
            "\f": "\\f",
            "\n": "\\n",
            "\r": "\\r",
            "\t": "\\t",
        }

        # Replace each occurrence of the specified characters with escaped versions
        for escape_sequence, escaped_sequence in escape_map.items():
            prompt = prompt.replace(escape_sequence, escaped_sequence)

        return prompt
```

called inside the class:
**C:\Users\...\git\MultiModelPlayground\mmp - env - lc3\Lib\site-packages\langchain_community\chat_models\azureml_endpoint.py**
```
class CustomOpenAIChatContentFormatter(ContentFormatterBase):
    """Chat Content formatter for models with OpenAI like API scheme."""

    SUPPORTED_ROLES: List[str] = ["user", "assistant", "system"]

    @staticmethod
    def _convert_message_to_dict(message: BaseMessage) -> Dict:
        """Converts a message to a dict according to a role"""
        content = cast(str, message.content)
        if isinstance(message, HumanMessage):
            return {
                "role": "user",
                "content": ContentFormatterBase.escape_special_characters(content),
            }
        elif isinstance(message, AIMessage):
            return {
                "role": "assistant",
                "content": ContentFormatterBase.escape_special_characters(content),
            }
        elif isinstance(message, SystemMessage):
            return {
                "role": "system",
                "content": ContentFormatterBase.escape_special_characters(content),
            }
        elif (
            isinstance(message, ChatMessage)
            and message.role in CustomOpenAIChatContentFormatter.SUPPORTED_ROLES
        ):
            return {
                "role": message.role,
                "content": ContentFormatterBase.escape_special_characters(content),
            }
        else:
            supported = ",".join(
                [role for role in CustomOpenAIChatContentFormatter.SUPPORTED_ROLES]
            )
            raise ValueError(
                f"""Received unsupported role. 
                Supported roles for the LLaMa Foundation Model: {supported}"""
            )
```

### Description

I am trying to use the HumanMessage class that supports the `content` as a list of dict 
```python
message = HumanMessage(
    content=[
        {"type": "text", "text": "describe the weather in this image"},
    ],
)
```

as input for an AzureMLChatOnlineEndpoint LLM 
```python 
llm = AzureMLChatOnlineEndpoint(
    endpoint_url=config["models"][0]["url"],
    endpoint_api_type=AzureMLEndpointApiType.serverless,
    endpoint_api_key=config["models"][0]["key"],
    content_formatter=CustomOpenAIChatContentFormatter(),
    timeout=timeout
)

response = llm.invoke([message])
```
but the `CustomOpenAIChatContentFormatter` class expects that the content is a string (as you can see from the above Error description) since performs a `replace` on a attribute that can be, potentially a dict, raising the above mentioned error.


I fixed making my own `ContentFormatter` removing the references to the 
`ContentFormatterBase.escape_special_characters(content)`

but it would be nice to have the AzureMLChatOnlineEndpoint/CustomOpenAIChatContentFormatter to support natively the cases in which the Human Message content is a list of dict 

### System Info

System Information
------------------
> OS:  Windows
> OS Version:  10.0.22631
> Python Version:  3.11.7 | packaged by Anaconda, Inc. | (main, Dec 15 2023, 18:05:47) [MSC v.1916 64 bit (AMD64)]

Package Information
-------------------
> langchain_core: 0.3.15
> langchain: 0.3.7
> langchain_community: 0.3.5
> langsmith: 0.1.142
> langchain_groq: 0.2.1
> langchain_text_splitters: 0.3.2

Optional packages not installed
-------------------------------
> langgraph
> langserve

Other Dependencies
------------------
> aiohttp: 3.10.10
> async-timeout: Installed. No version info available.
> dataclasses-json: 0.6.7
> groq: 0.11.0
> httpx: 0.27.2
> httpx-sse: 0.4.0
> jsonpatch: 1.33
> numpy: 1.26.4
> orjson: 3.10.11
> packaging: 24.2
> pydantic: 2.9.2
> pydantic-settings: 2.6.1
> PyYAML: 6.0.2
> requests: 2.32.3
> requests-toolbelt: 1.0.0
> SQLAlchemy: 2.0.35
> tenacity: 9.0.0
> typing-extensions: 4.12.2