The Z.ai Ruby SDK requires specific Ruby versions to ensure optimal performance, security, and compatibility with modern Ruby features.
| Version | Status | End of Life | Recommended |
|---|---|---|---|
| 3.3.x | ✅ Supported | Current | Yes |
| 3.2.8+ | ✅ Supported | TBD | Minimum |
| 3.1.x | ❌ Not Supported | 2025-03-31 | No |
| 3.0.x | ❌ Not Supported | 2024-03-31 | No |
| Version | Status | End of Life | Recommended |
|---|---|---|---|
| 10.0.4.0+ | ✅ Supported | TBD | Yes |
| 9.4.x | ❌ Not Supported | 2024-12-31 | No |
The SDK leverages Ruby 3.2+ features when available:
Ruby 3.2+ includes the YJIT (Yet Another Just-In-Time) compiler which provides:
- 20-30% performance improvement for typical workloads
- Lower memory usage
- Better throughput for API calls
The SDK automatically enables YJIT when running on Ruby 3.2+:
# Automatic YJIT optimization in lib/zai/client.rb
if Zai::Compatibility.ruby_32_plus? && defined?(RubyVM::YJIT)
RubyVM::YJIT.enable
endRuby 3.2+ introduces the data keyword for immutable structs:
# Example of data class usage in the SDK
data class ChatMessage
attr_reader :role, :content, :name
def initialize(role:, content:, name: nil)
@role = role
@content = content
@name = name
end
endBenefits:
- 50% faster instantiation
- Reduced memory allocation
- Immutable by default
- Better for concurrent operations
Ruby 3.2+ improves pattern matching for response parsing:
# Pattern matching for API responses
case response
in { id:, model:, choices: [{ message: { content: } }] }
# Extract content directly
in { error: { message:, type: } }
# Handle errors
else
# Fallback handling
endRuby 3.2+ includes memory management improvements:
- Compacting garbage collector
- Reduced fragmentation
- Better memory usage for large responses
JRuby 10.0.4.0+ provides unique advantages:
JRuby can utilize multiple CPU cores:
# Parallel processing for batch operations
client = Zai::JRuby::ConcurrentClient.new(api_key: ENV["ZAI_API_KEY"])
requests = Array.new(10) { { messages: "Process me" } }
responses = client.parallel_completions(requests)Access to Java libraries for performance:
# Java HTTP client with HTTP/2 support
client = Zai::JRuby::JavaIntegration.create_http_client("https://api.z.ai")- Better for concurrent API calls
- Improved throughput for batch operations
- Lower latency for streaming responses
# Using rbenv
rbenv install 3.3.0
rbenv local 3.3.0
# Using rvm
rvm install 3.3.0
rvm use 3.3.0
# Install the SDK
gem install zai-ruby-sdk# Using rbenv
rbenv install jruby-10.0.4.0
rbenv local jruby-10.0.4.0
# Install with JRuby
jruby -S gem install zai-ruby-sdkIf you're currently using Ruby 3.0 or 3.1:
- Backup your project: Ensure you have version control
- Update Ruby: Install Ruby 3.2.8 or later
- Update Gemfile: Ensure all dependencies are compatible
- Test thoroughly: Run your full test suite
- Update CI/CD: Update your CI pipelines
# Check for outdated dependencies
bundle outdated
# Update dependencies
bundle updateMost gems work with YJIT, but some might need updates:
# Disable YJIT if you encounter issues
if defined?(RubyVM::YJIT)
RubyVM::YJIT.disable
end| Ruby Version | Requests/sec | Memory Usage | CPU Usage |
|---|---|---|---|
| 3.0.0 | 100 | 100MB | 50% |
| 3.1.0 | 110 | 95MB | 48% |
| 3.2.8 (no YJIT) | 115 | 90MB | 45% |
| 3.2.8 (with YJIT) | 140 | 85MB | 40% |
| 3.3.0 (with YJIT) | 150 | 80MB | 38% |
| JRuby 10.0.4.0 | 180 | 120MB | 60% |
- Use Ruby 3.3.0 for production environments
- Enable YJIT for performance-critical applications
- Consider JRuby for high-throughput, concurrent workloads
- Regular updates to stay within supported versions
- Monitor performance to identify optimization opportunities
ERROR: Error installing zai-ruby-sdk:
zai-ruby-sdk requires Ruby version >= 3.2.8
Solution: Upgrade Ruby to 3.2.8 or later
If you're experiencing performance issues:
- Ensure you're running Ruby 3.2.8+ with YJIT enabled
- Check for memory leaks using
memory_profiler - Consider JRuby for concurrent workloads
- Profile your application with
benchmark
Issue: Slow startup time
# JRuby-specific optimization
if defined?(JRUBY_VERSION)
require 'jruby/profiler'
JRuby.profiler.enable
endIssue: Native gem conflicts
# Use pure-Ruby alternatives when available
require 'zai/pure_ruby' if defined?(JRUBY_VERSION)