# openai/openai-dotnet architecture

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

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

## Overview

##### Main Components

a. Client Layer:
- Core OpenAIClient as the main entry point
- Feature-specific clients (ChatClient, AudioClient, ImageClient, etc.)
- Client configuration and options management

b. Protocol Layer:
- REST API communication handling
- Request/response pipeline
- Authentication and authorization
- Retry logic and error handling

c. Model Layer:
- Data models for different API features
- Serialization/deserialization
- Model factories for testing

d. Utility Components:
- Telemetry and observability
- Streaming support
- Binary content handling
- Mock testing infrastructure

##### Relationships and Interactions

**[Application Layer]**
- ↓
**[OpenAIClient (Main Entry Point)]**
- ↓
**[Feature-Specific Clients]**
- ↙  ↓  ↘
**[Chat] [Audio] [Images] etc.**
- ↓
**[Protocol Layer]**
- ↙     ↓    ↘
**[Auth] [Retry] [Pipeline]**
- ↓
**[OpenAI REST API]**

##### Specific Elements

a. Client Components:
- OpenAIClient as the central hub
- All feature-specific clients (Chat, Audio, Images, etc.)
- Client options and configuration

b. Protocol Handling:
- REST client implementation
- Authentication middleware
- Retry policy
- Error handling
- Request/response pipeline

c. Model Management:
- Request/response models
- Serialization helpers
- Model factories
- Type formatters

d. Support Infrastructure:
- OpenTelemetry integration
- Streaming support
- Binary content handling
- Mock testing framework

##### Important Details

- Relationship between OpenAIClient and feature-specific clients
- How authentication flows through the system
- Demonstrate the retry and error handling mechanisms
- Streaming capabilities for relevant features
- Integration points for telemetry and observability
- Mock testing infrastructure connections

##### Notes for Implementation

- Use clear, descriptive labels for all components
- Adding notes for experimental features
- Version compatibility information
- Error handling paths

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Application Layer"
        App[Client Application]
    end

    subgraph "Client Layer"
        OAC[OpenAI Client]:::main
        click OAC "https://github.com/openai/openai-dotnet/blob/main/src/Custom/OpenAIClient.cs"
        
        Options[Client Options]:::config
        click Options "https://github.com/openai/openai-dotnet/blob/main/src/Custom/OpenAIClientOptions.cs"
    end

    subgraph "Feature Clients"
        Chat[Chat Client]:::feature
        Audio[Audio Client]:::feature
        Image[Image Client]:::feature
        Embed[Embeddings Client]:::feature
        Assist[Assistants Client]:::experimental
        
        click Chat "https://github.com/openai/openai-dotnet/blob/main/src/Custom/Chat/ChatClient.cs"
        click Audio "https://github.com/openai/openai-dotnet/blob/main/src/Custom/Audio/AudioClient.cs"
        click Image "https://github.com/openai/openai-dotnet/blob/main/src/Custom/Images/ImageClient.cs"
        click Embed "https://github.com/openai/openai-dotnet/blob/main/src/Custom/Embeddings/EmbeddingClient.cs"
        click Assist "https://github.com/openai/openai-dotnet/blob/main/src/Custom/Assistants/AssistantClient.cs"
    end

    subgraph "Protocol Layer"
        Rest[REST Client]:::protocol
        Pipeline[Pipeline Policy]:::protocol
        Auth[Authentication]:::protocol
        
        click Rest "https://github.com/openai/openai-dotnet/blob/main/src/Generated/OpenAIClient.RestClient.cs"
        click Pipeline "https://github.com/openai/openai-dotnet/blob/main/src/Utility/GenericActionPipelinePolicy.cs"
        click Auth "https://github.com/openai/openai-dotnet/blob/main/src/Generated/OpenAIClientOptions.cs"
    end

    subgraph "Model Layer"
        BaseModels[Base Models]:::model
        ChatModels[Chat Models]:::model
        AudioModels[Audio Models]:::model
        ImageModels[Image Models]:::model
        ErrorModels[Error Models]:::error
        
        click BaseModels "https://github.com/openai/openai-dotnet/tree/main/src/Generated/Models"
        click ChatModels "https://github.com/openai/openai-dotnet/blob/main/src/Custom/Chat/ChatMessage.cs"
        click AudioModels "https://github.com/openai/openai-dotnet/blob/main/src/Custom/Audio/AudioTranscription.cs"
        click ImageModels "https://github.com/openai/openai-dotnet/blob/main/src/Custom/Images/GeneratedImage.cs"
        click ErrorModels "https://github.com/openai/openai-dotnet/blob/main/src/Custom/OpenAIError.cs"
    end

    subgraph "Utility Layer"
        Telemetry[OpenTelemetry]:::utility
        Streaming[Streaming Support]:::utility
        Binary[Binary Content]:::utility
        Mock[Mock Testing]:::testing
        
        click Telemetry "https://github.com/openai/openai-dotnet/blob/main/src/Utility/Telemetry/OpenTelemetrySource.cs"
        click Streaming "https://github.com/openai/openai-dotnet/blob/main/src/Utility/System.Net.ServerSentEvents.cs"
        click Binary "https://github.com/openai/openai-dotnet/blob/main/src/Utility/MultipartFormDataBinaryContent.cs"
        click Mock "https://github.com/openai/openai-dotnet/blob/main/tests/Utility/MockPipelineTransport.cs"
    end

    subgraph "External"
        API[OpenAI REST API]:::external
    end

    App --> OAC
    OAC --> Options
    OAC --> |Creates| Chat & Audio & Image & Embed & Assist
    Chat & Audio & Image & Embed & Assist --> Rest
    Rest --> Pipeline
    Pipeline --> Auth
    Auth --> API
    Rest --> BaseModels
    Chat --> ChatModels
    Audio --> AudioModels
    Image --> ImageModels
    Rest --> ErrorModels
    Rest --> Streaming
    Rest --> Binary
    Rest --> Telemetry
    Rest -.-> Mock

    classDef main fill:#2196F3,stroke:#1565C0,color:white
    classDef config fill:#90CAF9,stroke:#1565C0,color:black
    classDef feature fill:#4CAF50,stroke:#2E7D32,color:white
    classDef experimental fill:#FF9800,stroke:#EF6C00,color:white
    classDef protocol fill:#FDD835,stroke:#F9A825,color:black
    classDef model fill:#9C27B0,stroke:#6A1B9A,color:white
    classDef error fill:#F44336,stroke:#C62828,color:white
    classDef utility fill:#795548,stroke:#4E342E,color:white
    classDef testing fill:#9E9E9E,stroke:#424242,color:white
    classDef external fill:#607D8B,stroke:#37474F,color:white
```
