Skip to main content

tml-renderers

tml-renderers turns structured text, image, and audio chat into model-ready tokens and media for training and inference. It supports Thinking Machines Lab models, including Inkling, Inkling-Small, Inkling 2 Small, Inkling 2 Mini, and TML-d24-varloop-research, through TMLv0, and the supported third-party models served on Tinker through model-specific renderers.

See the changelog for the changes in each release.

Installation

Always use the latest release:

pip install --upgrade tml-renderers

tml-renderers is a compiled extension that requires PyTorch 2.11 or newer.

Quickstart

Create a short conversation with native chat objects and render it for inference or training:

from tml_renderers import chat, renderers

user = chat.Message(
    content=chat.Text("Hello, world!"),
    author=chat.Author(chat.AuthorKind.User),
)
assistant = chat.Message(
    content=chat.Text("Hello!"),
    author=chat.Author(chat.AuthorKind.Model),
)
native_messages = chat.MessageList([user, assistant])

renderer = renderers.Qwen3()
spans, parser = renderer.render_for_completion([user])
training_examples = renderer.render_for_sft(native_messages)

To continue from the exact sampled token prefix, bridge the parsed semantic history to the next user or tool messages instead of rerendering earlier turns:

sampled = chat.TokenSpan(chat.EncodedTextTokenSpan(sampled_token_ids))
parsed = parser.parse_tokens(sampled_token_ids)
follow_up = chat.Message(
    content=chat.Text("Tell me more."),
    author=chat.Author(chat.AuthorKind.User),
)

spans, suffix, parser = renderer.bridge_to_next_turn(
    [*spans, sampled],
    [user, *parsed],
    [follow_up],
)

The first return value preserves every token in the supplied prefix and appends suffix. The returned parser is configured for the combined semantic history.

Supported models

Provider / family Models Renderer classes
thinkingmachines Inkling, Inkling-Small Inkling (TMLv0)
thinkingmachines Inkling 2 Small, Inkling 2 Mini Inkling2 (TMLv0)
thinkingmachines TML-d24-varloop-research TmlD24Varloop (TMLv0)
Qwen Qwen3 (including vision), Qwen3.5, Qwen3.8 Qwen3, Qwen35, Qwen38
DeepSeek V3, V3.1, V4, V4.1 Flash DeepSeekV3, DeepSeekV31, DeepSeekV4, DeepSeekV41
Kimi K2, K2.5, K3.1-Pro KimiK2, KimiK25, KimiK31Pro
GLM 5.2, 5.3, 5.3 Flash Glm52, Glm53, Glm53Flash
GPT-OSS GPT-OSS GptOss
Nemotron Nemotron 3, Nemotron 3.5, Nemotron 3.5 Super VL Nemotron3, Nemotron3VL

Glm52 clears reasoning from earlier turns. Glm53 and Glm53Flash keep it. Pass clear_thinking=True to clear it, as Z.ai recommends. Use the same setting for rollout and SFT.

OpenAI message compatibility

Convert OpenAI-style message dictionaries before passing them to a renderer:

from tml_renderers import chat, renderers

openai_dicts = [
    {"role": "user", "content": "Hello, world!"},
    {"role": "assistant", "content": "Hello!"},
]
openai_messages = chat.OpenAIMessage.from_openai_chat_dicts(openai_dicts)
round_tripped = chat.OpenAIMessage.to_openai_chat_dicts(openai_messages)

renderer = renderers.Qwen3()
training_examples = renderer.render_for_sft(openai_messages)

The renderer also accepts openai_messages for completion. The conversion supports text, reasoning, images, tool calls, and tool results; it handles message formats, not the OpenAI network API. An assistant dict keeps its reasoning in a top-level reasoning_content. As in an OpenAI request, tools are not a message: pass the tools list as chat.OpenAIMessage.from_openai_chat_dicts(openai_dicts, tools=tools).

The from_oss_* and to_oss_* methods read and write tinker_cookbook message dicts. They are deprecated.

Anthropic Messages compatibility

chat.MessageList.from_anthropic_messages(messages, *, system=None, tools=None) reads an Anthropic Messages request body, and to_anthropic_messages() writes one:

from tml_renderers import chat

messages = chat.MessageList.from_anthropic_messages(
    [
        {"role": "user", "content": "Weather in Paris?"},
        {
            "role": "assistant",
            "content": [
                {"type": "thinking", "thinking": "Use the tool.", "signature": ""},
                {"type": "tool_use", "id": "t1", "name": "get_weather", "input": {"city": "Paris"}},
            ],
        },
        {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": "Sunny"}]},
    ],
    system="Be brief.",
    tools=[{"name": "get_weather", "input_schema": {"type": "object"}}],
)
body = messages.to_anthropic_messages()  # {"messages": [...], "system": ..., "tools": [...]}

The conversion supports text, image, thinking, tool_use, and tool_result blocks:

  • A tool_result with is_error: true reads as ToolError parts. A tool message with ToolError parts writes is_error: true. A tool message that mixes Text and ToolError parts raises ValueError.
  • A {"role": "system"} message in messages reads as a system message in place. A system message after the conversation starts writes back in place.
  • An integer in a tool_use input must fit in 64 bits.

The reader ignores these keys, so to_anthropic_messages() does not write them back: cache_control, citations, and the thinking signature; a tool's defer_loading, strict, and eager_input_streaming; and a system message's clear_at and output_config. A turn-scoped system message applies to every later turn.

The reader raises ValueError for content that has no TML form that round-trips:

  • The blocks redacted_thinking, document, search_result, and container_upload, the server-tool blocks (server_tool_use, *_tool_result, mcp_tool_use, and mcp_tool_result), and the tool_addition and tool_removal blocks of a system message.
  • An image with a file source.
  • A tool with a type other than "custom", such as a server tool.

Malformed input also raises ValueError: a wrong value type, an unknown role or block type, or a tool_result without an earlier tool_use.

Rendering and parsing (TMLv0)

Rendering converts structured chat messages into the token and media-span sequence consumed by the model. renderers.Inkling and renderers.Inkling2 use the same TMLv0 grammar for both inference and training:

  • render_for_completion(...) renders an inference prompt and returns a parser for turning generated model tokens back into messages.
  • render_for_sft(...) renders supervised-training examples with aligned, per-token loss weights.

The parser reconstructs structured messages from generated token spans:

from tml_renderers import chat, renderers

renderer = renderers.Inkling()
tokenizer = renderer.tokenizer

messages = [
    chat.Message(
        content=chat.Text("Hello"),
        author=chat.Author(chat.AuthorKind.User),
    )
]

spans, parser = renderer.render_for_completion(messages)
parsed = parser.parse(spans)

training_examples = renderer.render_for_sft(messages)

For incremental sampling, open a parser stream and feed it only the newly sampled token IDs. It returns the same messages as a batch parse of the same tokens:

completed_messages = []
with parser.parse_stream() as stream:
    for token_ids in sampled_token_chunks:
        for update in stream.feed(token_ids):
            event = update.update
            if isinstance(event, chat.StreamingMessageHeader):
                print(f"[{event.author.kind}] ", end="")
            elif isinstance(event, chat.StreamingContent):
                print(event.content.text, end="", flush=True)
            elif isinstance(event, chat.Message):
                completed_messages.append(event)
    for update in stream.finish():
        if isinstance(update.update, chat.Message):
            completed_messages.append(update.update)

finish() validates the end of the stream and releases the parser. Leaving the context unfinished discards partial state and releases the parser. Parse exceptions expose completed_messages, which contains any complete prefix parsed before the error, and completed, which pairs each of those messages with its sampled token count. Inkling and Inkling2 raise parse_errors.ParseError, a ValueError subclass; the other renderers raise ValueError.

The package includes native tokenizers for these renderers. It does not need Hugging Face chat templates, remote code, or network access. Inkling uses tokenizers.O200kBaseChatTokenizer() and Inkling2 uses tokenizers.H200kBaseChatTokenizer(). Both share the same special-token IDs.

from tml_renderers import tokenizers

tokenizer.encode_ordinary("hello")
tokenizer.decode([24912])
tokenizer.encode_special("message_user")   # -> 200000
tokenizer.decode_special(200000)            # -> "message_user"

qwen_tokenizer = tokenizers.Qwen3Tokenizer()
qwen_tokenizer.encode_special("im_start")   # -> 151644

Full method surface:

method maps on invalid / partial UTF-8
encode_ordinary(text) text → ids — (special-token markup is encoded as literal text)
encode_special(name) name → id raises if name isn't a special token
decode(ids) ids → text lossy — invalid/partial bytes become U+FFFD, never raises (tiktoken parity)
decode_strict(ids) ids → text raises ValueError on invalid/partial UTF-8
decode_bytes(ids) ids → bytes raw bytes, no UTF-8 handling
decode_special(id) id → name None if id isn't a special token

The tokenizer also provides is_special_token(id), special_tokens(), eos_token, bos_token, and all_special_tokens.

render_for_completion accepts a MessageList, a list[Message], or a list[OpenAIMessage].

To render at one thinking effort, pass effort (a float in [0, 1]) to renderers.Inkling or renderers.Inkling2. The renderer then adds the effort message to every completion and SFT render. Do not also put a ThinkingEffort message in the input.

Multimodal inputs

tml-renderers includes multimodal codecs for the TMLv0 renderer:

  • An ImagePointer is rendered as an ImageAssetPointerTokenSpan with the expected image-token count.
  • An AudioPointer is rendered as a DmelTokenSpan by Inkling. Inkling2 does not accept audio.

Image and audio messages use the same completion and training APIs as text:

from tml_renderers import chat, renderers

renderer = renderers.Inkling()

image_msg = chat.Message(
    content=chat.ImagePointer(
        location="image.png",
        format=chat.ImageFormat.Png,
        width=512,
        height=512,
    ),
    author=chat.Author(chat.AuthorKind.User),
)

audio_msg = chat.Message(
    content=chat.AudioPointer(
        location="clip.wav",
        format=chat.AudioFormat.Wav,
        num_frames=48_000,
        sample_rate=16_000,
    ),
    author=chat.Author(chat.AuthorKind.User),
)

spans, _ = renderer.render_for_completion([image_msg, audio_msg])
dmel = next(s.span for s in spans if type(s.span).__name__ == "DmelTokenSpan")
print(dmel.dmel.shape)  # [num_audio_tokens, num_dmel_bins], uint8

Media locations are represented as pointers. Audio is read from local files; tml-renderers never fetches remote media.

Token spans

Renderers work with token-span types you can construct directly:

from tml_renderers.chat import (
    EncodedTextTokenSpan,
    ImageAssetPointerTokenSpan,
    ImageFormat,
    PaddingTokenSpan,
    TokenSpan,
)

text = EncodedTextTokenSpan([101, 102])
image = ImageAssetPointerTokenSpan(
    location="image.png", format=ImageFormat.Png, width=512, height=512, num_tokens=256
)
padding = PaddingTokenSpan(4)

# Wrap in the oneof only where a call boundary asks for a TokenSpan.
wrapped = TokenSpan(text)

DmelTokenSpan is the encoded-audio span produced by the renderer. Its dmel field is a uint8 tensor of shape [num_audio_tokens, num_dmel_bins].

chat.DmelTokenSpan(tensor, target_audio_token=...) copies a 2-D CPU torch.uint8 tensor. The .dmel getter returns a tensor that shares the span's storage, so writes to it change the span.

Using with Tinker

In Tinker Cookbook, use the recommended renderer for your model. Cookbook converts rendered text, image, and audio inputs for sampling and SFT.

The lower-level tml_renderers.tinker helpers are also available for direct Tinker SDK integration. Provide local media files or base64 image data.

Types

  • Core: Message, MessageList, Author, AuthorKind, MessageChannel, MessageMetadata
  • Content: Text, Thinking, ThinkingEffort, ImagePointer, AudioPointer, InvokeTool, StructuredToolCall, ToolError, ToolDeclareJson, ModelEndSampling
  • Token spans: TokenSpan, EncodedTextTokenSpan, ImageAssetPointerTokenSpan, DmelTokenSpan, PaddingTokenSpan
  • Formats: ImageFormat, AudioFormat
  • Tool args: ToolArg, ToolMetadata. ToolArg.value is a JSON-encoded string, matching the OpenAI function.arguments convention.
  • Tool declarations: ToolSpecJson (one declared tool with name, description, type_name, and JSON-schema parameters), grouped into ToolDeclareJson. The constructor accepts a dict for parameters and serializes it.

Examples

  • examples/run_tml_v0.py — low-level rendering and parsing of text and audio.
  • examples/cookbook_tinker_demo.py — native and OpenAI-style messages, including tool calls and results. See the public tool-call example.

License

Apache-2.0. The wheel includes LICENSE, NOTICE, and THIRD_PARTY_LICENSES.md.

Metadata

Release files for tml-renderers 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for tml-renderers 0.5.0
File Interpreter ABI Platform Actions
tml_renderers-0.5.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details for tml_renderers-0.5.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
tml_renderers-0.5.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for tml_renderers-0.5.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
tml_renderers-0.5.0-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for tml_renderers-0.5.0-cp311-abi3-macosx_11_0_arm64.whl

Total release size: 52.7 MB

Release files / tml_renderers-0.5.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL tml_renderers-0.5.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 17.8 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
465f2c7e33ab3f098610aa5086b06ce68e7d964053700e7fd261f873e054db14
BLAKE2b-256 checksum
How to use checksums
e5676730625c8e153fa22eaabdf1c69a12012d0f2d345454b6a534207e1b89b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.14

Release files / tml_renderers-0.5.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL tml_renderers-0.5.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 17.6 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
e1c3cc69ade3409e7d5c7c0152dd9e0d6b5faf548a20a99da2614f9297ba99f8
BLAKE2b-256 checksum
How to use checksums
9bce3d67d88a2906993df5373a7216b4170c48a0e45387bfbfc059ea7aaf4b7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.14

Release files / tml_renderers-0.5.0-cp311-abi3-macosx_11_0_arm64.whl

Download URL tml_renderers-0.5.0-cp311-abi3-macosx_11_0_arm64.whl
Size 17.3 MB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
04413ef191fe52a12d0a23a5eb0338c2daaf58935b5457f625f97799a405f9be
BLAKE2b-256 checksum
How to use checksums
4dd000e2de3d26716d15c3c4fd2c8f62c522487ab9a0e71bf723d40212e4e430
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.14

Security for tml-renderers 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page