# jxnl/instructor-rb architecture

> Architecture diagram of the GitHub repository jxnl/instructor-rb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jxnl/instructor-rb
- Repository: https://github.com/jxnl/instructor-rb

## Overview

Instructor-rb is a Ruby library that patches OpenAI and Anthropic clients to request schema-shaped LLM outputs, parse tool responses, validate them into EasyTalk models, and retry parsing or validation failures. The principal workflow is client setup, provider-specific request construction, API response parsing, then shared model validation. The README also advertises streaming and retries, but the sampled runtime source shows retries and iterable responses rather than streaming. Edges reflect the excerpts and README; unsampled capabilities are not connected speculatively.

Main entry points: [`lib/instructor/openai/patch.rb`](https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/openai/patch.rb) (OpenAI patch), [`lib/instructor/anthropic/patch.rb`](https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/anthropic/patch.rb) (Anthropic patch), [`lib/instructor.rb`](https://github.com/jxnl/instructor-rb/blob/main/lib/instructor.rb) (Instructor API), [`lib/instructor/mode.rb`](https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/mode.rb) (Tool modes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_setup["Library setup"]
  node_entry["Instructor API<br/>[instructor.rb]"]
  node_mode["Tool modes<br/>[mode.rb]"]
end

subgraph group_openai["OpenAI integration"]
  node_o_patch["OpenAI patch<br/>[patch.rb]"]
  node_o_response["OpenAI parser<br/>[response.rb]"]
end

subgraph group_anthropic["Anthropic integration"]
  node_a_patch["Anthropic patch<br/>[patch.rb]"]
  node_a_response["Anthropic parser<br/>[response.rb]"]
end

subgraph group_shared["Shared processing"]
  node_base["Shared patch logic<br/>[patch.rb]"]
  node_validated["Validated model"]
end

node_user(("Ruby application"))
node_openai_service{{"OpenAI API"}}
node_anthropic_service{{"Anthropic API"}}

node_user -->|"configures"| node_entry
node_entry -->|"patches client"| node_o_patch
node_entry -->|"patches client"| node_a_patch
node_entry -->|"selects default"| node_mode
node_user -->|"calls chat"| node_o_patch
node_user -->|"calls messages"| node_a_patch
node_o_patch -->|"resolves choice"| node_mode
node_o_patch -->|"uses shared methods"| node_base
node_a_patch -->|"uses shared methods"| node_base
node_o_patch -->|"posts request"| node_openai_service
node_o_patch -->|"parses response"| node_o_response
node_a_patch -->|"posts request"| node_anthropic_service
node_a_patch -->|"parses response"| node_a_response
node_o_patch -->|"returns validated model"| node_validated
node_a_patch -->|"returns validated model"| node_validated

click node_entry "https://github.com/jxnl/instructor-rb/blob/main/lib/instructor.rb"
click node_mode "https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/mode.rb"
click node_o_patch "https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/openai/patch.rb"
click node_o_response "https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/openai/response.rb"
click node_a_patch "https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/anthropic/patch.rb"
click node_a_response "https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/anthropic/response.rb"
click node_base "https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/base/patch.rb"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_mode toneBlue
class node_o_patch,node_o_response toneAmber
class node_a_patch,node_a_response,node_openai_service,node_anthropic_service toneMint
class node_base,node_validated toneRose
class node_user toneIndigo
```

## Components

### Library setup

- [Instructor API](https://github.com/jxnl/instructor-rb/blob/main/lib/instructor.rb): `lib/instructor.rb`
- [Tool modes](https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/mode.rb): `lib/instructor/mode.rb`

### OpenAI integration

- [OpenAI patch](https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/openai/patch.rb): `lib/instructor/openai/patch.rb`
- [OpenAI parser](https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/openai/response.rb): `lib/instructor/openai/response.rb`

### Anthropic integration

- [Anthropic patch](https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/anthropic/patch.rb): `lib/instructor/anthropic/patch.rb`
- [Anthropic parser](https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/anthropic/response.rb): `lib/instructor/anthropic/response.rb`

### Shared processing

- [Shared patch logic](https://github.com/jxnl/instructor-rb/blob/main/lib/instructor/base/patch.rb): `lib/instructor/base/patch.rb`
- **Validated model**

### Other components

- **Ruby application**
- **OpenAI API**
- **Anthropic API**

## Connections

- Ruby application → Instructor API: configures (evidence: `README.md`)
- Instructor API → OpenAI patch: patches client (evidence: `lib/instructor.rb`)
- Instructor API → Anthropic patch: patches client (evidence: `lib/instructor.rb`)
- Instructor API → Tool modes: selects default (evidence: `lib/instructor.rb`)
- Ruby application → OpenAI patch: calls chat (evidence: `README.md`)
- Ruby application → Anthropic patch: calls messages (evidence: `README.md`)
- OpenAI patch → Tool modes: resolves choice (evidence: `lib/instructor/openai/patch.rb`)
- OpenAI patch → Shared patch logic: uses shared methods (evidence: `lib/instructor/openai/patch.rb`)
- Anthropic patch → Shared patch logic: uses shared methods (evidence: `lib/instructor/anthropic/patch.rb`)
- OpenAI patch → OpenAI API: posts request (evidence: `lib/instructor/openai/patch.rb`)
- OpenAI patch → OpenAI parser: parses response (evidence: `lib/instructor/openai/patch.rb`)
- Anthropic patch → Anthropic API: posts request (evidence: `lib/instructor/anthropic/patch.rb`)
- Anthropic patch → Anthropic parser: parses response (evidence: `lib/instructor/anthropic/patch.rb`)
- OpenAI patch → Validated model: returns validated model (evidence: `lib/instructor/openai/patch.rb`)
- Anthropic patch → Validated model: returns validated model (evidence: `lib/instructor/anthropic/patch.rb`)
