sorbet-baml

Ruby-idiomatic conversion from Sorbet types to BAML (Boundary AI Markup Language) for efficient LLM prompting.

Gem Version Total Downloads License Sorbet Compatible

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

JSON Schema: ~680 tokens
BAML: ~320 tokens (53% reduction)

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

Complex Types

Structured Types

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

Ready to get started?

Read the Getting Started Guide →