# ollama/ollama-python architecture

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

- Interactive diagram: https://gitdiagram.com/ollama/ollama-python
- Repository: https://github.com/ollama/ollama-python

## Overview

##### Main Components

a) Client Layer:
- Synchronous Client (`Client` class)
- Asynchronous Client (`AsyncClient` class)
- Type Definitions (from `_types.py`)

b) API Interface Layer:
- REST API endpoints mapping
- Request/Response handling
- Error handling

c) Core Features:
- Chat functionality
- Generation
- Model management (list, show, create, copy, delete)
- Model distribution (pull, push)
- Embedding generation
- Process management

d) External Components:
- Ollama Server
- LLM Models
- HTTP/HTTPS Communication

**[Client Applications]**
- ↓
**[Python Library Interface]**
- Sync Client
- Async Client
- ↓
**[Type System & Utils]**
- ↓
**[API Layer]**
- ↓
**[Ollama Server]**
- ↓
**[LLM Models]**

##### Important Elements to Highlight

- Blue: Client interfaces
- Green: Core functionality
- Yellow: External components
- Gray: Utility and type systems

- Connection Types:

##### Specific Implementation Details

- Bidirectional communication for streaming responses
- Error handling pathways
- Type checking and validation flows
- Async vs sync paths differently

##### Key Architectural Patterns

- Client-Server architecture
- Factory pattern for client creation
- Strategy pattern for sync/async operations
- Type-safe interface design

**Rectangle: Component**
**Diamond: Decision Point**

##### Top Layer: Client Interface
- Both sync and async entry points
- Example methods (chat, generate, embed)

##### Middle Layer: Core Library
- Type system
- Utility functions
- API mapping

##### Bottom Layer: External Integration
- Ollama server connection
- Model management
- Process handling

##### Technical Details

- HTTP/HTTPS communication
- Python version compatibility (3.8+)
- External dependencies (httpx)
- Type hints and validation

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client_Layer
        Client["Sync Client"]:::client
        AsyncClient["Async Client"]:::client
        Types["Type Definitions"]:::types
    end

    subgraph Core_Features
        Chat["Chat Interface"]:::core
        Gen["Generation"]:::core
        Embed["Embedding"]:::core
        ModelMgmt["Model Management"]:::core
        ProcMgmt["Process Management"]:::core
    end

    subgraph API_Layer
        REST["REST API Interface"]:::api
        ReqHandler["Request Handler"]:::api
        RespHandler["Response Handler"]:::api
        ErrorHandler["Error Handler"]:::api
    end

    subgraph External
        Server["Ollama Server"]:::external
        Models["LLM Models"]:::external
    end

    %% Relationships
    Client --> Types
    AsyncClient --> Types
    Client --> Core_Features
    AsyncClient --> Core_Features
    Core_Features --> API_Layer
    API_Layer -.->|HTTP/HTTPS| Server
    Server --> Models

    %% Click Events
    click Client "https://github.com/ollama/ollama-python/blob/main/ollama/_client.py"
    click AsyncClient "https://github.com/ollama/ollama-python/blob/main/ollama/_client.py"
    click Types "https://github.com/ollama/ollama-python/blob/main/ollama/_types.py"
    click Chat "https://github.com/ollama/ollama-python/blob/main/examples/chat.py"
    click Gen "https://github.com/ollama/ollama-python/blob/main/examples/generate.py"
    click Embed "https://github.com/ollama/ollama-python/blob/main/examples/embed.py"
    click ModelMgmt "https://github.com/ollama/ollama-python/blob/main/examples/list.py"
    click ProcMgmt "https://github.com/ollama/ollama-python/blob/main/examples/ps.py"

    %% Styling
    classDef client fill:#a8d1ff,stroke:#333,stroke-width:2px
    classDef types fill:#d3d3d3,stroke:#333,stroke-width:2px
    classDef core fill:#90EE90,stroke:#333,stroke-width:2px
    classDef api fill:#FFE5B4,stroke:#333,stroke-width:2px
    classDef external fill:#FFB6C1,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        L1["Client Components"]:::client
        L2["Type System"]:::types
        L3["Core Features"]:::core
        L4["API Layer"]:::api
        L5["External Systems"]:::external
    end
```
