sorbet-baml
Ruby-idiomatic conversion from Sorbet types to BAML (Boundary AI Markup Language) for efficient LLM prompting.
Why BAML?
BAML uses approximately 60% fewer tokens than JSON Schema while maintaining complete type information, making your LLM interactions more efficient and cost-effective.
🚀 Token Efficiency Comparison
Real-world comparison from production agentic workflows using complex nested types, enums, and arrays.
Quick Example
# Define a Sorbet struct
class User < T::Struct
const :name, String
const :age, Integer
const :email, T.nilable(String)
end
# Convert to BAML (Ruby-idiomatic API)
require 'sorbet-baml'
User.to_baml
Generated BAML:
class User {
name string
age int
email string?
}
Key Features
🎯 Ruby-Idiomatic API
Every T::Struct and T::Enum gets a natural .to_baml
method that feels native to Ruby.
🧠 Smart Defaults
Field descriptions and dependencies included automatically for better LLM understanding.
📝 Field Descriptions
Extracts comments from source code to provide crucial context for autonomous agents.
🔗 Dependency Management
Automatically includes all referenced types with proper topological sorting.
✅ Type-Safe
Full Sorbet type checking throughout the gem with 100% test coverage.
🛠️ Tool Definitions
Generate BAML tool specifications for function calling and agentic workflows.
🏁 Production Ready
Complete type support, dependency management, and comprehensive test coverage.
Complete Type Support
Basic Types
String
→string
Integer
→int
Float
→float
T::Boolean
→bool
Symbol
→string
Date/DateTime/Time
→string
Complex Types
T.nilable(T)
→T?
(optional types)T::Array[T]
→T[]
(arrays)T::Hash[K,V]
→map<K,V>
(hash maps)T.any(T1, T2)
→T1 | T2
(union types)
Structured Types
T::Struct
→class Name { ... }
(classes with fields)T::Enum
→enum Name { "value1" "value2" }
(enums)- Nested structs with proper reference handling
- Automatic dependency resolution with topological sorting
Perfect for Agentic Workflows
# Define your autonomous research workflow types
class TaskDecomposition < T::Struct
# The main research topic being investigated
const :research_topic, String
# Target complexity level for the decomposition
const :complexity_level, ComplexityLevel
# Autonomously generated list of research subtasks
const :subtasks, T::Array[String]
end
# Generate BAML for LLM agents
prompt = <<~PROMPT
You are an autonomous research agent. Analyze this topic and decompose it.
Schema for your output:
#{TaskDecomposition.to_baml}
Topic: "Impact of AI on healthcare delivery systems"
PROMPT
# Use with any LLM provider
response = llm_client.chat(prompt)
result = TaskDecomposition.from_json(response.content)
Installation
Add to your Gemfile:
gem 'sorbet-baml'
Or install directly:
gem install sorbet-baml