# openai/openai-java architecture

> Architecture diagram of the GitHub repository openai/openai-java, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openai/openai-java
- Repository: https://github.com/openai/openai-java

## Overview

Below are the steps and guidelines to capture the architecture accurately:

##### Main components and boundaries
- Core Library Module (openai-java-core): Contains the low-level HTTP abstractions (e.g., HttpRequest, HttpResponse), JSON (de)serialization, error types, utility classes, and helper modules for streaming (e.g. SSE).
- Client Implementation(s) (openai-java-client-okhttp): Implements the actual HTTP client using the OkHttp library and handles both blocking and asynchronous networking. It exposes the OpenAIClient (and its async variant) which the user code instantiates.
- Service Layer: Within the core and client modules you have “services” packages (both blocking and async) covering various API endpoints (e.g., Chat, Completions, Embeddings, File Uploads). These services take in request parameter objects (like ChatCompletionCreateParams) and return deserialized response models (like ChatCompletion).
- Example Module (openai-java-example): Contains runnable sample programs illustrating how to configure and use the client, with sample flows for using the Chat API, handling streaming responses, and file uploads.
- Build & DevOps: The build system (Gradle with buildSrc), Docker configuration (in .devcontainer), GitHub workflows for CI/CD, and related scripts which enable releases, linting, testing, and formatting.
- Configuration & Environment: The SDK supports configuration via environment variables (for API keys, organization, project, etc.) and exposes various customization options (timeouts, proxy, retries).

##### Relationships and interactions
- The SDK user (application) instantiates an OpenAIClient. This client is constructed by either a builder (with fluent configuration to set API key, timeouts, streaming executors, etc.) or loaded using environment variables.
- The client internally leverages the underlying OkHttp HTTP client (provided by openai-java-client-okhttp) to execute REST API calls.
- Requests are constructed and passed to service-specific methods (for chat completions, file uploads, etc.) that first validate and then send the HTTP request.
- The core layer then handles deserializing responses (or streaming parts one-by-one) into model objects.
- Error handling is centralized via custom exceptions mapped to error codes.
- In asynchronous flows, the response uses futures (CompletableFuture) or a streaming handler that executes on a dedicated thread pool.

##### Architectural patterns and design principles
- Modular design: Separation between low-level HTTP core, client-specific wrapping, and usage examples.
- Immutability: Most API models are built using a builder pattern and are immutable.
- Separation of concerns: The client is responsible for managing networking while the models and services handle data (including streaming) and error processing.
- Asynchronous vs. synchronous processing: Architectural support for both blocking and non-blocking execution.
- Extensibility: Use of “additional properties” in model building and the ability to customize request headers/parameters so that undocumented API functionalities can be integrated flexibly.

## Diagram (Mermaid)

```mermaid
flowchart TD 
    subgraph "Execution Flow"
        A["Application Code"]:::external
        N["Environment Config & Builder Setup"]:::config
        B["OpenAIClient (sync/async)"]:::client
        C["HTTP Client (OkHttp integration)"]:::client
        D["Request Builders & Models"]:::core
        E1["Synchronous Service Methods"]:::service
        E2["Asynchronous Service Methods"]:::service
        F["HTTP Request sent to OpenAI REST API (External)"]:::external
        G["HTTP Response Received"]:::external
        H["Response Parsing"]:::core
        I["Deserialized Models"]:::core
        J["Streaming (SSE/Async)"]:::service
        K["Accumulation Handler"]:::service
        L["Error Mapping Component"]:::error
        M["Custom Exceptions"]:::error

        A --> B
        N --- B
        B --> C
        C --> D
        D --> E1
        D --> E2
        E1 --> F
        E2 --> F
        F --> G
        G --> H
        H --> I
        G --> J
        J --> K
        C --> L
        L --> M
    end

    subgraph "SDK Modules"
        CONF["Configuration & Environment"]:::config
        CL["Core Library Module"]:::core
        CI["Client Implementation (OkHttp)"]:::client
        SS["Synchronous Services"]:::service
        AS["Asynchronous Services"]:::service
        EX["Example Module"]:::supplement
        BS["Build Scripts"]:::build
        DC["DevOps Config"]:::build
    end

    %% Click Events
    click CL "https://github.com/openai/openai-java/tree/main/openai-java-core"
    click CI "https://github.com/openai/openai-java/tree/main/openai-java-client-okhttp"
    click SS "https://github.com/openai/openai-java/tree/main/openai-java-core/src/main/kotlin/com/openai/services/blocking"
    click AS "https://github.com/openai/openai-java/tree/main/openai-java-core/src/main/kotlin/com/openai/services/async"
    click EX "https://github.com/openai/openai-java/tree/main/openai-java-example"
    click BS "https://github.com/openai/openai-java/blob/main/build.gradle.kts"
    click DC "https://github.com/openai/openai-java/blob/main/.devcontainer"
    click CONF "https://github.com/openai/openai-java/blob/main/openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt"

    %% Styles
    classDef external fill:#A2D2FF,stroke:#000,stroke-width:2px;
    classDef client fill:#FFD6A5,stroke:#000,stroke-width:2px;
    classDef core fill:#C1E1C1,stroke:#000,stroke-width:2px;
    classDef service fill:#F9D5BB,stroke:#000,stroke-width:2px;
    classDef config fill:#E0BBE4,stroke:#000,stroke-width:2px;
    classDef error fill:#FFADAD,stroke:#000,stroke-width:2px;
    classDef supplement fill:#BDE0FE,stroke:#000,stroke-width:2px;
    classDef build fill:#CFF9D9,stroke:#000,stroke-width:2px;
```
