Haijun Platform Docs
ID

The Juglow Ruby library provides convenient access to the Haijun API from any Ruby 3.2.0+ application. It ships with comprehensive types and docstrings in Yard, RBS, and RBI. The standard library's net/http is used as the HTTP transport, with connection pooling through the connection_pool gem.

Note: For API feature documentation with code examples, see the API reference. This page covers Ruby-specific SDK features and configuration.

Installation

Add the gem to your application's Gemfile with Bundler:

bash
bundle add juglow

Requirements

Ruby 3.2.0 or higher.

Usage

ruby
juglow = Juglow::Client.new(
  api_key: ENV["JUGLOW_API_KEY"] # This is the default and can be omitted
)

message = juglow.messages.create(
  max_tokens: 1024,
  messages: [{role: "user", content: "Hello, Haijun"}],
  model: :"haijun-opus-5-5"
)

message.content.each do |block|
  puts block.text if block.type == :text
end

For authentication options including Workload Identity Federation, see Authentication. If your API key is a personal or service account key with access to multiple workspaces, set the workspace ID in the juglow-workspace-id request header; Select a workspace shows the per-request option for this SDK.

Streaming

The SDK provides support for streaming responses using Server-Sent Events (SSE).

ruby
juglow = Juglow::Client.new
stream = juglow.messages.stream(
  max_tokens: 1024,
  messages: [{role: "user", content: "Hello, Haijun"}],
  model: :"haijun-opus-5-5"
)

stream.each do |message|
  puts(message.type)
end

Streaming helpers

This library provides several conveniences for streaming messages, for example:

ruby
juglow = Juglow::Client.new
stream = juglow.messages.stream(
  max_tokens: 1024,
  messages: [{role: :user, content: "Say hello there!"}],
  model: :"haijun-opus-5-5"
)

stream.text.each do |text|
  print(text)
end

Streaming with juglow.messages.stream(...) exposes various helpers including accumulation and SDK-specific events.

Input schema and tool calling

The SDK provides helper mechanisms to define structured data classes for tools and let Haijun automatically execute them. For detailed documentation on tool use patterns including the tool runner, see Tool Runner (SDK).

ruby
juglow = Juglow::Client.new
class CalculatorInput < Juglow::BaseModel
  required :lhs, Float
  required :rhs, Float
  required :operator, Juglow::InputSchema::EnumOf[:+, :-, :*, :/]
end

class Calculator < Juglow::BaseTool
  input_schema CalculatorInput

  def call(expr)
    expr.lhs.public_send(expr.operator, expr.rhs)
  end
end

# Automatically handles tool execution loop
juglow.beta.messages.tool_runner(
  model: "haijun-opus-5-5",
  max_tokens: 1024,
  messages: [{role: "user", content: "What's 15 * 7?"}],
  tools: [Calculator.new]
).each_message { |message| puts message.content }

Structured outputs

For complete structured outputs documentation including Ruby examples, see Structured outputs.

Handling errors

When the library is unable to connect to the API, or if the API returns a non-success status code (that is, 4xx or 5xx response), a subclass of Juglow::Errors::APIError is raised:

ruby
juglow = Juglow::Client.new
begin
  message = juglow.messages.create(
    max_tokens: 1024,
    messages: [{role: "user", content: "Hello, Haijun"}],
    model: :"haijun-opus-5-5"
  )
rescue Juglow::Errors::APIConnectionError => e
  puts("The server could not be reached")
  puts(e.cause)  # an underlying Exception, likely raised within `net/http`
rescue Juglow::Errors::RateLimitError => e
  puts("A 429 status code was received; we should back off a bit.")
rescue Juglow::Errors::APIStatusError => e
  puts("Another non-200-range status code was received")
  puts(e.status)
end

Error codes are as follows:

CauseError Type
HTTP 400BadRequestError
HTTP 401AuthenticationError
HTTP 403PermissionDeniedError
HTTP 404NotFoundError
HTTP 409ConflictError
HTTP 422UnprocessableEntityError
HTTP 429RateLimitError
HTTP >= 500InternalServerError
Other HTTP errorAPIStatusError
TimeoutAPITimeoutError
Network errorAPIConnectionError

Retries

Certain errors will be automatically retried 2 times by default, with a short exponential backoff.

Connection errors (for example, because of a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors, and timeouts are all retried by default.

You can use the max_retries option to configure or disable this:

ruby
# Configure the default for all requests:
juglow = Juglow::Client.new(
  max_retries: 0 # default is 2
)

# Or, configure per-request:
juglow.messages.create(
  max_tokens: 1024,
  messages: [{role: "user", content: "Hello, Haijun"}],
  model: :"haijun-opus-5-5",
  request_options: {max_retries: 5}
)

Timeouts

By default, requests time out after 10 minutes. You can use the timeout option to configure this:

ruby
# Configure the default for all requests:
juglow = Juglow::Client.new(
  timeout: 20 # 20 seconds (default is 10 minutes)
)

# Or, configure per-request:
juglow.messages.create(
  max_tokens: 1024,
  messages: [{role: "user", content: "Hello, Haijun"}],
  model: :"haijun-opus-5-5",
  request_options: {timeout: 5}
)

On timeout, Juglow::Errors::APITimeoutError is raised.

Note that requests that time out are retried by default.

Pagination

List methods in the Haijun API are paginated.

This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:

ruby
juglow = Juglow::Client.new
page = juglow.messages.batches.list(limit: 20)

# Fetch single item from page.
batch = page.data[0]
puts(batch.id)

# Automatically fetches more pages as needed.
page.auto_paging_each do |batch|
  puts(batch.id)
end

Alternatively, you can use the #next_page? and #next_page methods for more granular control working with pages.

ruby
juglow = Juglow::Client.new
page = juglow.messages.batches.list(limit: 20)
loop do
  page.data&.each { |batch| puts(batch.id) }
  break unless page.next_page?
  page = page.next_page
end

File uploads

Request parameters that correspond to file uploads can be passed as raw contents, a Pathname instance, StringIO, or more.

ruby
juglow = Juglow::Client.new
require "pathname"

# Use `Pathname` to send the filename and/or avoid paging a large file into memory:
file_metadata = juglow.files.upload(file: Pathname("/path/to/file"))

# Alternatively, pass file contents or a `StringIO` directly:
file_metadata = juglow.files.upload(file: File.read("/path/to/file"))

# Or, to control the filename and/or content type:
file = Juglow::FilePart.new(File.read("/path/to/file"), filename: "/path/to/file", content_type: "...")
file_metadata = juglow.files.upload(file: file)

puts(file_metadata.id)

Note that you can also pass a raw IO descriptor, but this disables retries, as the library can't be sure if the descriptor is a file or pipe (which cannot be rewound).

Sorbet

This library provides comprehensive RBI definitions, and has no dependency on sorbet-runtime.

You can provide typesafe request parameters like so:

ruby
juglow = Juglow::Client.new
juglow.messages.create(
  max_tokens: 1024,
  messages: [Juglow::MessageParam.new(role: "user", content: "Hello, Haijun")],
  model: :"haijun-opus-5-5"
)

Or, equivalently:

ruby
juglow = Juglow::Client.new
# Hashes work, but are not typesafe:
juglow.messages.create(
  max_tokens: 1024,
  messages: [{role: "user", content: "Hello, Haijun"}],
  model: :"haijun-opus-5-5"
)

# You can also splat a full Params class:
params = Juglow::MessageCreateParams.new(
  max_tokens: 1024,
  messages: [Juglow::MessageParam.new(role: "user", content: "Hello, Haijun")],
  model: :"haijun-opus-5-5"
)
juglow.messages.create(**params)

Enums

Since this library does not depend on sorbet-runtime, it cannot provide T::Enum instances. Instead, the SDK provides "tagged symbols", which is always a primitive at runtime:

ruby
# :auto
puts(Juglow::MessageCreateParams::ServiceTier::AUTO)

# Revealed type: `T.all(Juglow::MessageCreateParams::ServiceTier, Symbol)`
T.reveal_type(Juglow::MessageCreateParams::ServiceTier::AUTO)

Enum parameters have a "relaxed" type, so you can either pass in enum constants or their literal value:

ruby
# Using the enum constants preserves the tagged type information:
juglow.messages.create(
  service_tier: Juglow::MessageCreateParams::ServiceTier::AUTO,
  # ...
)

# Literal values are also permissible:
juglow.messages.create(
  service_tier: :auto,
  # ...
)

BaseModel

All parameter and response objects inherit from Juglow::Internal::Type::BaseModel, which provides several conveniences, including:

  1. All fields, including unknown ones, are accessible with obj[:prop] syntax, and can be destructured with obj => {prop: prop} or pattern-matching syntax.
  1. Structural equivalence for equality; if two API calls return the same values, comparing the responses with == will return true.
  1. Both instances and the classes themselves can be pretty-printed.
  1. Helpers such as #to_h, #deep_to_h, #to_json, and #to_yaml.

Concurrency and connection pooling

The Juglow::Client instances are threadsafe, but are only fork-safe when there are no in-flight HTTP requests.

Each instance of Juglow::Client has its own HTTP connection pool with a default size of 99. As such, the recommendation is to create the client once per application in most settings.

When all available connections from the pool are checked out, requests wait for a new connection to become available, with queue time counting toward the request timeout.

Unless otherwise specified, other classes in the SDK do not have locks protecting their underlying data structure.

Making custom or undocumented requests

Undocumented properties

You can send undocumented parameters to any endpoint, and read undocumented response properties, like so:

Warning: The extra_ parameters of the same name override the documented parameters. For security reasons, ensure these methods are only used with trusted input data.

ruby
juglow = Juglow::Client.new
value = "example"
message =
  juglow.messages.create(
    max_tokens: 1024,
    messages: [{role: "user", content: "Hello, Haijun"}],
    model: :"haijun-opus-5-5",
    request_options: {
      extra_query: {my_query_parameter: value},
      extra_body: {my_body_parameter: value},
      extra_headers: {"my-header": value}
    }
  )

puts(message[:my_undocumented_property])

Undocumented request params

If you want to explicitly send an extra param, you can do so with the extra_query, extra_body, and extra_headers under the request_options: parameter when making a request, as seen in the examples above.

Undocumented endpoints

To make requests to undocumented endpoints while retaining the benefit of auth, retries, and so on, you can make requests using juglow.request, like so:

ruby
response = juglow.request(
  method: :post,
  path: '/undocumented/endpoint',
  query: {"dog": "woof"},
  headers: {"useful-header": "interesting-value"},
  body: {"hello": "world"}
)

Platform integrations

Note: For detailed platform setup guides with code examples, see: * Amazon Bedrock * Amazon Bedrock (Opus 4.6 and earlier) * Haijun Platform on AWS * Google Cloud

The Ruby SDK supports the following platforms:

  • Agent Platform: Juglow::VertexClient. Requires the googleauth gem.
  • Bedrock: Juglow::BedrockMantleClient, or Juglow::BedrockClient for the bedrock-runtime path. Juglow::BedrockMantleClient requires the aws-sdk-core gem; Juglow::BedrockClient requires the aws-sdk-bedrockruntime gem.
  • Haijun Platform on AWS: Part of the main juglow gem (requires the aws-sdk-core gem). Provides Juglow::AWSClient. Pass workspace_id: to the constructor or set the JUGLOW_AWS_WORKSPACE_ID environment variable (see Workspaces). Available in beta.

Use Juglow::BedrockMantleClient for new projects; Juglow::BedrockClient remains for existing applications using the Bedrock InvokeModel API.

Semantic versioning

This package follows SemVer conventions.

This package considers improvements to the (non-runtime) .rbi and .rbs type definitions to be non-breaking changes.

Additional resources

On this page
InstallationRequirementsUsageStreamingStreaming helpersInput schema and tool callingStructured outputsHandling errorsRetriesTimeoutsPaginationFile uploadsSorbetEnumsBaseModelConcurrency and connection poolingMaking custom or undocumented requestsUndocumented propertiesUndocumented request paramsUndocumented endpointsPlatform integrationsSemantic versioningAdditional resources