ooai_llm.messages

Message normalization and lightweight message estimation helpers.

Purpose:

Normalize common message inputs into both LangChain-style message objects and OpenAI/LiteLLM-style message dictionaries.

Design:
  • Accept ergonomic inputs such as a bare string, a sequence of mapping objects, or an existing sequence of LangChain messages.

  • Convert inputs lazily so importing ooai_llm does not require the full LangChain runtime until message handling is used.

  • Keep the public surface small and provider-agnostic so the same helpers can later be reused for embeddings, tool traces, and other higher-level features.

Examples

>>> normalized = normalize_messages("hello")
>>> normalized.openai_messages[0]["role"]
'user'
>>> normalized.message_count
1

Attributes

Classes

NormalizedMessages

Normalized message payload in LangChain and OpenAI-compatible forms.

MessageEstimate

Optional message-derived estimate attached to model info.

Functions

normalize_messages(→ NormalizedMessages)

Normalize message input into LangChain and OpenAI-compatible forms.

Module Contents

type ooai_llm.messages.MessageMapping = Mapping[str, Any][source]
type ooai_llm.messages.MessageLike = str | MessageMapping | Any[source]
type ooai_llm.messages.MessagesLike = str | Sequence[MessageLike][source]
class ooai_llm.messages.NormalizedMessages(/, **data: Any)[source]

Bases: pydantic.BaseModel

Normalized message payload in LangChain and OpenAI-compatible forms.

Parameters:
  • langchain_messages – LangChain-style message objects.

  • openai_messages – OpenAI/LiteLLM-style message dictionaries.

model_config[source]

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

langchain_messages: list[Any] = None[source]
openai_messages: list[dict[str, Any]] = None[source]
property message_count: int[source]

Return the number of normalized messages.

class ooai_llm.messages.MessageEstimate(/, **data: Any)[source]

Bases: pydantic.BaseModel

Optional message-derived estimate attached to model info.

Parameters:
  • message_count – Number of normalized messages.

  • estimated_input_tokens – Estimated prompt/input tokens when available.

  • fits_context_window – Whether the estimated prompt fits the model’s context window when both values are known.

  • warning – Optional best-effort warning or limitation note.

model_config[source]

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

message_count: int = 0[source]
estimated_input_tokens: int | None = None[source]
fits_context_window: bool | None = None[source]
warning: str | None = None[source]
ooai_llm.messages.normalize_messages(messages: MessagesLike) NormalizedMessages[source]

Normalize message input into LangChain and OpenAI-compatible forms.

Parameters:

messages – Bare prompt string, message mapping, or sequence of either.

Returns:

Normalized message payload.