$ show openai/harmony
This is the canonical implementation of OpenAI's Harmony response format, the structured prompt format used by the gpt-oss open-weight model series. The repository provides a high-performance Rust core for rendering conversations into token sequences and parsing model outputs back into structured messages. It supports multi-channel reasoning (analysis, commentary, final), tool/function calling with JSON Schema-defined parameters, structured outputs, and streaming incremental parsing. The library is packaged with first-class Python bindings via PyO3 and a JavaScript/WASM build for browser use. A Next.js demo visualizes the token-level rendering. The codebase demonstrates strong engineering: a custom BPE tokenizer (tiktoken) implementation in Rust, a streaming parser that gracefully handles partial UTF-8 sequences, and a JSON Schema-to-TypeScript converter for tool definitions. Tests are comprehensive with parity between Rust and Python test suites.
The LLM identified 3 files worth highlighting for reuse:
A standalone, high-performance Byte Pair Encoding (BPE) tokenizer implementation in Rust, equivalent to OpenAI's Python tiktoken library. It features thread-local regex caching to avoid contention, FxHashMap for fast lookups, and a byte_pair_merge algorithm optimized for small pieces. The encode/decode logic handles both ordinary and special tokens, including unstable token detection for completions. This module is fully self-contained (no dependencies on the rest of the crate) and could be extracted as an independent Rust crate for any BPE tokenization need.
The StreamableParser implements incremental token-by-token parsing of the Harmony format, handling partial UTF-8 sequences gracefully by buffering undecoded bytes and emitting replacement characters when sequences are invalid. It manages state transitions (ExpectStart, Header, Content) and correctly handles stop tokens, multi-token UTF-8 sequences, and tool call headers with adjacent special tokens. The design is reusable for any streaming token processing scenario where incremental decoding with error recovery is required.
The json_schema_to_typescript function converts a JSON Schema (OpenAPI style) into a TypeScript type definition string suitable for embedding in prompt formats. It handles complex schemas including nested objects, oneOf, nullable types, enums with descriptions and default values, arrays, and property-level comments. This is a self-contained utility that could be extracted and used in any code generation pipeline that needs to render JSON Schema as TypeScript types for LLM prompts or API documentation.