Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Togethercomputer Together python Message Construction

From Leeroopedia
Attribute Value
Principle Name Message_Construction
Overview Pattern for constructing structured conversation messages for chat completion APIs.
Domain NLP, API_Client, Inference
Repository togethercomputer/together-python
Last Updated 2026-02-15 16:00 GMT

Description

Message construction defines the schema for building conversation histories as ordered lists of role-tagged messages. Each message has a role (system, user, assistant, or tool) and content (a text string, a multimodal content list, or None). This follows the OpenAI-compatible chat format adopted by Together AI and many other LLM providers.

The conversation is represented as a List[Dict[str, Any]] where each dictionary maps to a ChatCompletionMessage schema. Messages are ordered chronologically, with the model generating a response that continues the conversation from the final message.

Message Roles

  • system -- Sets the model's behavior, persona, or constraints. Typically the first message in the conversation. Not all models support system messages.
  • user -- Represents human input: questions, instructions, or prompts. This is the primary input role.
  • assistant -- Represents the model's prior responses. Used to provide conversation history or few-shot examples. May include tool_calls when the model invokes functions.
  • tool -- Returns the result of a function call back to the model. Paired with a preceding assistant message that contained tool_calls.

Content Types

Message content can take three forms:

  • String -- Plain text content (most common).
  • Content list -- A list of typed content parts for multimodal inputs. Each part specifies a type (text, image_url, video_url, audio_url) and the corresponding data.
  • None -- Used for assistant messages that consist solely of tool calls with no text content.

Usage

Use message construction when preparing input for any chat completion request. Every call to client.chat.completions.create() requires a messages parameter containing at least one message.

When to use:

  • Building single-turn question/answer interactions
  • Constructing multi-turn conversation histories
  • Providing system instructions to control model behavior
  • Sending multimodal inputs (images, video, audio) to vision/multimodal models
  • Implementing function/tool calling workflows

When not to use:

  • For text completion (non-chat) APIs, which accept a plain string prompt instead of messages
  • For embedding APIs, which accept text strings or lists of strings

Theoretical Basis

Chat models are trained with conversation-format input where each turn is tagged with a role identifier. This structure maps to the model's training format:

  • System messages correspond to the system prompt in the model's chat template, configuring behavior before the conversation begins.
  • User messages map to human turns in the conversational training data.
  • Assistant messages map to model turns, enabling few-shot prompting and conversation history injection.
  • Tool messages extend the assistant-tool interaction loop, allowing models to call external functions and incorporate their results.

The ordered list structure ensures the model receives context in the correct temporal sequence, which is critical for coherent multi-turn conversations. The content list format for multimodal messages allows mixing text and media references within a single message turn.

Knowledge Sources

Source Type URI
Together AI Chat Completions API Doc Together AI Chat Overview
Together AI Function Calling Doc Together AI Function Calling
Together AI Vision Models Doc Together AI Vision Overview

Related

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment