calute.api_server.converters

calute.api_server.converters#

Message conversion utilities for the API server.

This module provides utilities for converting between OpenAI-format messages and Calute’s internal message format. It includes: - Bidirectional message format conversion - Support for system, user, and assistant message roles - Proper handling of message content extraction

The conversion utilities enable seamless interoperability between OpenAI-compatible API requests and Calute’s agent processing pipeline.

Example

>>> from calute.api_server.converters import MessageConverter
>>> from calute.types.oai_protocols import ChatMessage
>>> messages = [ChatMessage(role="user", content="Hello")]
>>> history = MessageConverter.convert_openai_to_calute(messages)
class calute.api_server.converters.MessageConverter[source]#

Bases: object

Converts between OpenAI and Calute message formats.

Utility class providing static methods for bidirectional conversion between OpenAI-format ChatMessage objects and Calute’s internal MessagesHistory format. Handles role mapping and content extraction for all supported message types (system, user, assistant).

This converter is used internally by the API server routers to translate incoming OpenAI-compatible requests into the format expected by Calute’s agent execution pipeline.

Example

>>> from calute.api_server.converters import MessageConverter
>>> from calute.types.oai_protocols import ChatMessage
>>> msgs = [
...     ChatMessage(role="system", content="You are helpful."),
...     ChatMessage(role="user", content="Hello!"),
... ]
>>> history = MessageConverter.convert_openai_to_calute(msgs)
>>> len(history.messages)
2
static convert_openai_to_calute(messages: list[calute.types.oai_protocols.ChatMessage]) MessagesHistory[source]#

Convert a list of OpenAI-format messages to Calute’s internal format.

Iterates through each ChatMessage and maps it to the corresponding Calute message type based on the role field:

  • "system" -> SystemMessage

  • "user" -> UserMessage

  • "assistant" -> AssistantMessage

Any None content is converted to an empty string.

Parameters

messages – List of OpenAI ChatMessage objects to convert. Each message must have a role field set to one of "system", "user", or "assistant".

Returns

A MessagesHistory instance containing the converted messages in the same order as the input list.

Raises

ValueError – If a message has an unrecognized role (i.e., not "system", "user", or "assistant").

Example

>>> from calute.api_server.converters import MessageConverter
>>> from calute.types.oai_protocols import ChatMessage
>>> messages = [ChatMessage(role="user", content="Hi")]
>>> history = MessageConverter.convert_openai_to_calute(messages)
>>> history.messages[0].content
'Hi'