Crates.io | harmony-protocol |
lib.rs | harmony-protocol |
version | 0.1.0 |
created_at | 2025-09-24 23:32:48.403675+00 |
updated_at | 2025-09-24 23:32:48.403675+00 |
description | Reverse-engineered OpenAI Harmony response format library for structured conversation handling |
homepage | https://github.com/terraprompt/harmony-protocol |
repository | |
max_upload_size | |
id | 1853919 |
size | 295,000 |
A reverse-engineered Rust library implementing OpenAI's Harmony response format for structured conversational AI interactions.
This library does NOT include an AI model. It provides the conversation formatting and parsing layer that works with OpenAI's models that understand the Harmony format. You still need:
<|start|>
, <|message|>
, <|end|>
tokens)What this library does: Formats conversations β [Your OpenAI Model] β Parses responses
This library provides a complete implementation of the Harmony response format used by OpenAI's open-weight model series (gpt-oss). It enables parsing and rendering of structured conversations with support for:
Add to your Cargo.toml
:
[dependencies]
harmony-protocol = { git = "https://github.com/yourusername/harmony-protocol" }
use harmony_protocol::{
load_harmony_encoding, HarmonyEncodingName,
chat::{Role, Message, Conversation, SystemContent}
};
fn main() -> anyhow::Result<()> {
// Load the encoding
let enc = load_harmony_encoding(HarmonyEncodingName::HarmonyGptOss)?;
// Create a conversation
let convo = Conversation::from_messages([
Message::from_role_and_content(
Role::System,
SystemContent::new()
.with_required_channels(["analysis", "commentary", "final"])
),
Message::from_role_and_content(Role::User, "Hello, world!"),
]);
// Render for completion (ready to send to OpenAI model)
let input_tokens = enc.render_conversation_for_completion(&convo, Role::Assistant, None)?;
println!("Generated {} tokens ready for OpenAI model", input_tokens.len());
// TODO: Send input_tokens to your OpenAI model and get response_tokens
// let response_tokens = your_openai_client.complete(input_tokens).await?;
// Parse the model's response back to structured messages
// let messages = enc.parse_messages_from_completion_tokens(response_tokens, Some(Role::Assistant))?;
Ok(())
}
use harmony_protocol::chat::{
SystemContent, ToolDescription, ToolNamespaceConfig, Message, Role
};
fn main() -> anyhow::Result<()> {
let tools = vec![
ToolDescription::new(
"calculate",
"Performs mathematical calculations",
Some(serde_json::json!({
"type": "object",
"properties": {
"expression": {"type": "string"}
},
"required": ["expression"]
}))
)
];
let function_namespace = ToolNamespaceConfig::new("functions", None, tools);
let system_content = SystemContent::new()
.with_browser_tool()
.with_tools(function_namespace);
let message = Message::from_role_and_content(Role::System, system_content);
Ok(())
}
use harmony_protocol::{StreamableParser, load_harmony_encoding, HarmonyEncodingName};
use harmony_protocol::chat::Role;
fn main() -> anyhow::Result<()> {
let encoding = load_harmony_encoding(HarmonyEncodingName::HarmonyGptOss)?;
let mut parser = StreamableParser::new(encoding.clone(), Some(Role::Assistant))?;
// In practice, response_tokens would come from your OpenAI model's streaming API
let response_tokens = vec![200006, 1234, 5678]; // These would be from OpenAI
// Process tokens as they arrive from the model
for token in response_tokens {
parser.process(token)?;
// Get content delta for real-time streaming UI updates
if let Ok(Some(delta)) = parser.last_content_delta() {
print!("{}", delta); // Show new content to user immediately
}
}
// Get final structured messages after streaming is complete
let messages = parser.into_messages();
println!("\nParsed {} messages from model output", messages.len());
Ok(())
}
The Harmony format structures conversations using special tokens:
<|start|>system<|message|>You are ChatGPT, a large language model trained by OpenAI.
Knowledge cutoff: 2024-06
Reasoning: medium
# Valid channels: analysis, commentary, final. Channel must be included for every message.<|end|>
<|start|>user<|message|>What is 2 + 2?<|end|>
<|start|>assistant<|channel|>analysis<|message|>I need to perform a simple arithmetic calculation.<|end|>
<|start|>assistant<|channel|>final<|message|>2 + 2 equals 4.<|end|>
The library supports multiple communication channels for organized model outputs:
Channels can be configured as required, and the system automatically handles analysis dropping when final responses are complete.
use harmony_protocol::chat::ToolDescription;
fn main() {
let custom_tool = ToolDescription::new(
"weather",
"Gets current weather for a location",
Some(serde_json::json!({
"type": "object",
"properties": {
"location": {"type": "string"},
"units": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}))
);
println!("Created custom tool: {}", custom_tool.name);
}
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Chat Module β β Encoding Module β β Registry Module β
β β β β β β
β β’ Message βββββΊβ β’ Rendering βββββΊβ β’ Configurationsβ
β β’ Conversation β β β’ Parsing β β β’ Token Mappingsβ
β β’ Content Types β β β’ Streaming β β β’ Vocab Loading β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β² β²
β β
βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ
β Tiktoken Module β β Extensions β
β β β β
β β’ BPE Encoding β β β’ Public Vocabs β
β β’ Tokenization β β β’ Hash Verify β
β β’ Thread Safety β β β’ Remote Loadingβ
βββββββββββββββββββ βββββββββββββββββββ
Token | ID | Purpose |
---|---|---|
`< | start | >` |
`< | message | >` |
`< | end | >` |
`< | channel | >` |
`< | call | >` |
`< | return | >` |
`< | constrain | >` |
TIKTOKEN_ENCODINGS_BASE
: Custom vocabulary file directoryTIKTOKEN_RS_CACHE_DIR
: Custom cache directorypython-binding
: Enable PyO3 Python bindingswasm-binding
: Enable WebAssembly support# Run all tests (13 tests covering unit + integration)
cargo test
# Run performance benchmarks
cargo bench
# Run specific examples
cargo run --example basic_usage
cargo run --example tool_integration
cargo run --example streaming_parser
cargo run --example channel_management
The test suite includes comprehensive validation against canonical examples and edge cases.
The library includes 4 comprehensive examples:
basic_usage.rs
- Message creation and conversation renderingtool_integration.rs
- Custom tools and function callingstreaming_parser.rs
- Real-time token processingchannel_management.rs
- Multi-channel workflowsSee examples/README.md
for detailed usage instructions.
cd python
python setup.py build_rust
pip install -e .
import harmony_protocol as hr
# Same API as Rust, but in Python
encoding = hr.load_harmony_encoding(hr.HarmonyEncodingName.harmony_gpt_oss())
conversation = hr.Conversation.from_messages([
hr.Message.from_role_and_content(hr.Role.user(), "Hello!")
])
tokens = encoding.render_conversation(conversation)
cd www
npm run build # Requires wasm-pack
npm run serve # Open http://localhost:8000
Run benchmarks to see performance characteristics:
cargo bench
Results show the library can handle:
This project is licensed under the Apache License 2.0.
This is a reverse-engineered implementation for educational and research purposes. It is not affiliated with or endorsed by OpenAI.