Skip to main content

Convert any sentence or chat messages array to the official chat template for OpenAI, Mistral, and Qwen models.

Project description

chatunify

Convert any sentence or chat messages array into the official chat template for OpenAI, Mistral, and Qwen models.

Installation

pip install chatunify

Quick Start

from chatunify import apply_template

# Plain string — auto-wrapped as a user message
result = apply_template("Hello!", model="gpt-4o")
result = apply_template("Hello!", model="mistral-large-latest")
result = apply_template("Hello!", model="qwen2.5-72b-instruct")

# Full messages array
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What is the capital of France?"},
]
result = apply_template(messages, model="mistral-large-latest")

Supported Models

Provider Model name patterns Template
OpenAI gpt-*, o1, o3, o4, chatgpt-* JSON messages array (API format)
Mistral V1 mistral-7b-v0.1 <s> [INST] user [/INST] assistant</s>
Mistral V2 mistral-7b-v0.2, mixtral-*, mistral-small/medium <s>[INST] user[/INST] assistant</s>
Mistral V3 mistral-large, mistral-nemo, codestral, *-2407+ <s>[INST]user[/INST]assistant</s>
Qwen qwen*, qwq* ChatML <|im_start|>role\ncontent<|im_end|>

Template Outputs

OpenAI

Returns a pretty-printed JSON string of the messages array — the canonical OpenAI API input format.

[
  {"role": "system", "content": "You are helpful."},
  {"role": "user", "content": "Hello!"}
]

Mistral (V3 / Tekken)

<s>[INST]You are helpful.

Hello![/INST]

Qwen (ChatML)

<|im_start|>system
You are helpful.<|im_end|>
<|im_start|>user
Hello!<|im_end|>
<|im_start|>assistant

API

apply_template(input, model)

Parameter Type Description
input str or list[dict] Plain string or list of {"role": ..., "content": ...} dicts
model str Model name (e.g. "gpt-4o", "mistral-large-latest", "qwen2.5-72b-instruct")

Returns: str — the formatted prompt string.

detect_provider(model)

Returns (provider, variant) tuple for a given model name. Useful for debugging routing.

from chatunify import detect_provider

detect_provider("gpt-4o")               # ("openai", None)
detect_provider("mistral-large-latest") # ("mistral", "v3")
detect_provider("qwen2.5-72b-instruct") # ("qwen", None)

Running Tests

python tests/test_templates.py

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

chatunify-0.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

chatunify-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file chatunify-0.1.0.tar.gz.

File metadata

  • Download URL: chatunify-0.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.12

File hashes

Hashes for chatunify-0.1.0.tar.gz
Algorithm Hash digest
SHA256 199c7506888fd593d598bbe63cd38054f9128361a8dc8430a47815a7e4871ad0
MD5 861b32bf58d5bdb28ba78724a1429475
BLAKE2b-256 a24c51a7cdda3f3077dc84cea81302d59f7b6b45b18c5c477af3594aaa6da301

See more details on using hashes here.

File details

Details for the file chatunify-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: chatunify-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.12

File hashes

Hashes for chatunify-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b593213605489ab36aade0ec7ee94849b8a0fd9f2a3cfa0c05cf5c7b05259da
MD5 1a586c4d09f34d8756cb19d0baa03c0d
BLAKE2b-256 44d02eae7b9b6ebb8e7325c7c732066b830f303143347ed503c28a6bb6338cbd

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