# relationalai/rai-sdk-python architecture

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

- Interactive diagram: https://gitdiagram.com/relationalai/rai-sdk-python
- Repository: https://github.com/relationalai/rai-sdk-python

## Overview

##### Project Type and Purpose
- This is an open-source Python SDK (“rai-sdk”) that exposes RelationalAI’s REST (and gRPC) APIs to Python developers. It provides both:
- Core “railib” modules for talking to RAI, handling configuration, credentials, protobuf messages, HTTP/gRPC transport, and convenience commands (“show”).
- A Snowflake-integration subpackage (relationalai-sdk-snowflake) to embed the SDK into Snowflake UDFs or pipelines.
- The SDK is packaged via setuptools/pyproject and published to PyPI. Top‐level folders include: examples (stand-alone usage), railib (core library), relationalai-sdk-snowflake (Snowflake extension), tests, CI/CD workflows, and config templates.

##### File Structure Analysis
- Top-level modules:
- railib/: core Python package
– api.py – high-level client calls
– rest.py – HTTP wrapper
– pb/ – protobuf definitions and generated code
– credentials.py – OAuth or API-key loader
– config.py – INI-style config parser
– show.py – CLI helper for printing results
- relationalai-sdk-snowflake/: separate package embedding railib in Snowflake UDF context
- examples/: ~60 stand-alone scripts demonstrating each API endpoint
- test/: unit and integration tests
- CI/CD (.github/), packaging (setup.py, pyproject.toml), and config.spec

##### README Insights
- Primary communication is over REST (HTTPS) by default, with optional gRPC via generated protobufs.
- SDK reads credentials/config from ~/.rai/config.
- Example scripts simply instantiate railib.API() and call methods like list_engines(), execute(), etc.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    subgraph "Client Application Layer"
        Examples["Examples (Client Code)"]:::client
    end

    %% SDK Core
    subgraph "rai-sdk-python"
        subgraph "Configuration & Credentials"
            Config["config.py"]:::config
            Credentials["credentials.py"]:::config
            ConfigSpec["config.spec"]:::config
        end
        subgraph "API Surface"
            API["api.py"]:::api
        end
        subgraph "Transport Layer"
            Rest["rest.py"]:::transport
            GRPCStub1["message_pb2.py"]:::transport
            GRPCStub2["schema_pb2.py"]:::transport
        end
        subgraph "Protobuf Definitions"
            MessageProto["message.proto"]:::proto
            SchemaProto["schema.proto"]:::proto
        end
        CLI["show.py"]:::cli
    end

    %% Snowflake Extension
    subgraph "Snowflake Extension"
        SnowflakeAPI["api.py"]:::snowflake
        SnowflakeInit["__init__.py"]:::snowflake
    end

    %% External Services
    subgraph "External Services"
        Backend["RelationalAI Backend"]:::external
        OAuth["OAuth Token Issuer"]:::external
        SnowflakeService["Snowflake"]:::external
    end

    %% Tests
    subgraph "Tests"
        T1["test_unit.py"]:::test
        T2["test_integration.py"]:::test
        T3["test_integration.py"]:::test
    end

    %% CI/CD & Packaging
    subgraph "CI/CD & Packaging"
        CI1["build.yaml"]:::ci
        CI2["publish.yaml"]:::ci
        CI3["publish-snowflake-sdk-pypi.yaml"]:::ci
        Setup["setup.py"]:::meta
        Pyproject["pyproject.toml"]:::meta
        Requirements["requirements.txt"]:::meta
    end

    %% Data Flows
    Examples -->|"calls"| API
    API -->|"loads"| Config
    API -->|"loads"| Credentials
    Credentials -->|"requests token"| OAuth
    API -->|"calls HTTP"| Rest
    API -->|"/grpc (optional)"| GRPCStub1
    Rest -->|"HTTPS"| Backend
    GRPCStub1 -->|"/gRPC"| Backend
    Backend -->|"response"| Rest
    Rest -->|"returns"| API
    API -->|"returns"| Examples

    Examples -->|"UDF calls"| SnowflakeAPI
    SnowflakeAPI -->|"uses"| API
    SnowflakeAPI -->|"runs in"| SnowflakeService

    CLI -->|"print results"| Examples

    %% Test and CI flows (dashed)
    T1 -.->|tests| API
    T2 -.->|tests| Rest
    T3 -.->|tests| SnowflakeAPI
    CI1 -.->|builds| Setup
    CI2 -.->|publishes| Setup
    CI3 -.->|publishes| SnowflakeInit

    %% Click Events
    click Config "https://github.com/relationalai/rai-sdk-python/blob/main/railib/config.py"
    click Credentials "https://github.com/relationalai/rai-sdk-python/blob/main/railib/credentials.py"
    click ConfigSpec "https://github.com/relationalai/rai-sdk-python/blob/main/config.spec"
    click API "https://github.com/relationalai/rai-sdk-python/blob/main/railib/api.py"
    click Rest "https://github.com/relationalai/rai-sdk-python/blob/main/railib/rest.py"
    click GRPCStub1 "https://github.com/relationalai/rai-sdk-python/blob/main/railib/pb/message_pb2.py"
    click GRPCStub2 "https://github.com/relationalai/rai-sdk-python/blob/main/railib/pb/schema_pb2.py"
    click MessageProto "https://github.com/relationalai/rai-sdk-python/blob/main/railib/pb/message.proto"
    click SchemaProto "https://github.com/relationalai/rai-sdk-python/blob/main/railib/pb/schema.proto"
    click CLI "https://github.com/relationalai/rai-sdk-python/blob/main/railib/show.py"
    click SnowflakeAPI "https://github.com/relationalai/rai-sdk-python/blob/main/relationalai-sdk-snowflake/relationalai/snowflake_sdk/api.py"
    click SnowflakeInit "https://github.com/relationalai/rai-sdk-python/blob/main/relationalai-sdk-snowflake/relationalai/snowflake_sdk/__init__.py"
    click Examples "https://github.com/relationalai/rai-sdk-python/tree/main/examples/"
    click T1 "https://github.com/relationalai/rai-sdk-python/blob/main/test/test_unit.py"
    click T2 "https://github.com/relationalai/rai-sdk-python/blob/main/test/test_integration.py"
    click T3 "https://github.com/relationalai/rai-sdk-python/blob/main/relationalai-sdk-snowflake/test/test_integration.py"
    click CI1 "https://github.com/relationalai/rai-sdk-python/blob/main/.github/workflows/build.yaml"
    click CI2 "https://github.com/relationalai/rai-sdk-python/blob/main/.github/workflows/publish.yaml"
    click CI3 "https://github.com/relationalai/rai-sdk-python/blob/main/.github/workflows/publish-snowflake-sdk-pypi.yaml"
    click Setup "https://github.com/relationalai/rai-sdk-python/blob/main/setup.py"
    click Pyproject "https://github.com/relationalai/rai-sdk-python/blob/main/relationalai-sdk-snowflake/pyproject.toml"
    click Requirements "https://github.com/relationalai/rai-sdk-python/blob/main/requirements.txt"

    %% Styles
    classDef config fill:#bbdefb,stroke:#333,stroke-width:1px
    classDef api fill:#c8e6c9,stroke:#333,stroke-width:1px
    classDef transport fill:#ffe0b2,stroke:#333,stroke-width:1px
    classDef proto fill:#e1bee7,stroke:#333,stroke-width:1px
    classDef cli fill:#d7ccc8,stroke:#333,stroke-width:1px
    classDef snowflake fill:#ffccbc,stroke:#333,stroke-width:1px
    classDef client fill:#b3e5fc,stroke:#333,stroke-width:1px
    classDef external fill:#ffcdd2,stroke:#333,stroke-width:1px
    classDef ci fill:#f0f4c3,stroke:#333,stroke-width:1px
    classDef meta fill:#cfd8dc,stroke:#333,stroke-width:1px
    classDef test fill:#f8bbd0,stroke:#333,stroke-width:1px
```
