convert_to_openai_messages#

langchain_core.messages.utils.convert_to_openai_messages(messages: BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any] | Sequence[BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]], *, text_format: Literal['string', 'block'] = 'string') dict | list[dict][source]#

Convert LangChain messages into OpenAI message dicts.

Added in version 0.3.11.

Parameters:
  • messages (BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any] | Sequence[BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]]) – Message-like object or iterable of objects whose contents are in OpenAI, Anthropic, Bedrock Converse, or VertexAI formats.

  • text_format (Literal['string', 'block']) –

    How to format string or text block contents:

    • ”string”:

      If a message has a string content, this is left as a string. If a message has content blocks that are all of type ‘text’, these are joined with a newline to make a single string. If a message has content blocks and at least one isn’t of type ‘text’, then all blocks are left as dicts.

    • ”block”:

      If a message has a string content, this is turned into a list with a single content block of type ‘text’. If a message has content blocks these are left as is.

Returns:

  • dict:

    If a single message-like object is passed in, a single OpenAI message dict is returned.

  • list[dict]:

    If a sequence of message-like objects are passed in, a list of OpenAI message dicts is returned.

Return type:

The return type depends on the input type

Example

from langchain_core.messages import (
    convert_to_openai_messages,
    AIMessage,
    SystemMessage,
    ToolMessage,
)

messages = [
    SystemMessage([{"type": "text", "text": "foo"}]),
    {"role": "user", "content": [{"type": "text", "text": "whats in this"}, {"type": "image_url", "image_url": {"url": "data:image/png;base64,'/9j/4AAQSk'"}}]},
    AIMessage("", tool_calls=[{"name": "analyze", "args": {"baz": "buz"}, "id": "1", "type": "tool_call"}]),
    ToolMessage("foobar", tool_call_id="1", name="bar"),
    {"role": "assistant", "content": "thats nice"},
]
oai_messages = convert_to_openai_messages(messages)
# -> [
#   {'role': 'system', 'content': 'foo'},
#   {'role': 'user', 'content': [{'type': 'text', 'text': 'whats in this'}, {'type': 'image_url', 'image_url': {'url': "data:image/png;base64,'/9j/4AAQSk'"}}]},
#   {'role': 'assistant', 'tool_calls': [{'type': 'function', 'id': '1','function': {'name': 'analyze', 'arguments': '{"baz": "buz"}'}}], 'content': ''},
#   {'role': 'tool', 'name': 'bar', 'content': 'foobar'},
#   {'role': 'assistant', 'content': 'thats nice'}
# ]