Skip to main content

Any Message Could be a String, for LLM Usage

Project description

str-message

Any Message Could be a String, for LLM Usage.

A universal message format that seamlessly works with OpenAI's Chat Completion API, Response API, and Agents SDK. Supports multi-turn conversations, tools, and MCP (Model Context Protocol).

Installation

pip install str-message

Quick Start

import agents
import openai
from str_message import Conversation, Message, UserMessage
from str_message.extra.func_defs import func_def_get_current_time
from str_message.extra.mcps import aws_knowledge_mcp_tool
from str_message.utils.might_reasoning import might_reasoning
from str_message.utils.might_temperature import might_temperature

MODEL = "gpt-5-mini"

# Create agent with custom tool and MCP
agent = agents.Agent(
    "assistant",
    model=agents.OpenAIResponsesModel(
        model=MODEL,
        openai_client=openai.AsyncOpenAI(),
    ),
    model_settings=agents.ModelSettings(
        temperature=might_temperature(MODEL, 0.0, default=None),
        reasoning=might_reasoning(MODEL, "low", default=None),
    ),
    instructions="You are a helpful assistant.",
    tools=[
        func_def_get_current_time().agents_tool,  # Custom tool
        aws_knowledge_mcp_tool,  # MCP tool
    ],
)

conv = Conversation()

# 4-turn conversation demonstrating reasoning, tool, MCP, and regular chat
user_says = [
    "Solve this: If I have 3 apples and buy 2 more, then give 1 away, how many do I have?",  # Reasoning
    "What time is it in Tokyo?",  # Custom tool
    "What is AWS S3?",  # MCP
    "Thank you!",  # Regular chat
]

for user_say in user_says:
    conv.add_message(UserMessage(content=user_say))

    input_messages = Message.to_response_input_param(conv.messages)

    run_result = await agents.run.Runner().run(agent, input_messages, context={})

    # Update conversation with agent's response
    conv.messages[:] = [
        Message.from_any(item) for item in run_result.to_input_list()
    ]

    # Track usage and cost
    if usage := run_result.context_wrapper.usage:
        conv.add_usage(usage, model=MODEL)

    conv.clean_messages()

print(f"Total cost: ${conv.total_cost:.4f}")

Features

  • Universal Format: Single message format works across all OpenAI APIs
  • Multi-turn Conversations: Built-in conversation management with Conversation class
  • Tool Support: Native support for function calling and tool execution
  • MCP Compatible: Built-in MCP (Model Context Protocol) integration
  • Usage Tracking: Automatic token usage and cost tracking
  • Type Safe: Full type annotations with Pydantic models

Key Classes

  • Message: Universal message container
  • Conversation: Manages multi-turn conversations and usage tracking
  • UserMessage, AssistantMessage, ToolCallOutputMessage: Typed message builders
  • FuncDef: Type-safe function/tool definitions

Links

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

str_message-0.6.0.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

str_message-0.6.0-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

File details

Details for the file str_message-0.6.0.tar.gz.

File metadata

  • Download URL: str_message-0.6.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.14 Darwin/25.0.0

File hashes

Hashes for str_message-0.6.0.tar.gz
Algorithm Hash digest
SHA256 d0d4bbe2c305c7afd7521e357d02cbd02c42a4c5a03e5f0b2e69e51dea017ba3
MD5 5b41ce5b5dda660b22aa0e1f86eae15e
BLAKE2b-256 a94df409c756f9998b86de1a6774137255e67610568985548cea92b0328556e8

See more details on using hashes here.

File details

Details for the file str_message-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: str_message-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 28.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.14 Darwin/25.0.0

File hashes

Hashes for str_message-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 882bec0581a520956cffa70f7ce13f2939182c22276c1a1b38b0e9d16ade517a
MD5 6aecf42742cc99ec37307ddadfadd55c
BLAKE2b-256 7384e29bb94ed6da4c0307a86b78ca5aff29c3eb1e80d198f39711eed04e9c48

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page