# boundaryml/baml architecture

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

- Interactive diagram: https://gitdiagram.com/boundaryml/baml
- Repository: https://github.com/boundaryml/baml

## Overview

##### Major Components
- Core Engine & Libraries: Use the “engine” directory as the heart of the system. Within it, there are multiple submodules such as:
– baml-lib (which includes baml-core, baml-log, baml-types, parser-database, jinja-runtime, jsonish, etc.)
– baml-runtime (handles prompt execution, CLI commands, streaming, IR processing, retry policies, LLM client orchestration)
– baml-rpc (facilitates remote procedure calls/debugging, trace events)
– CLI (simple command line interface to trigger operations)
- Language Clients & Code Generation: The “language_client_*” directories (for Python, Ruby, TypeScript, etc.) show that the project supports auto-generated client libraries for different ecosystems. In addition, there is the “language_client_codegen” component and associated templates (for Python, Ruby, TypeScript) that drive this code generation.
- Frontend & Interactive Tools:
– The TypeScript “fiddle-frontend” and “fiddle-proxy” directories provide a web playground for testing prompts.
– VSCode integration exists in the “typescript/vscode-ext” folder to give developers interactive tools (such as viewing full prompt code, network request previews, etc.).
- Documentation & Developer Tools:
– “fern” – a large documentation and guide corpus primarily written in Markdown.
– “docs” folder and integration tests under “integ-tests” add test harnesses and example projects.
- External LLM Providers:

##### Relationships and Interactions
- The client libraries (language_client_*) rely on the core engine to generate the necessary bindings which are produced by the code generation tools. Represent them as consumers of engine APIs.
- The CLI tools in the engine and additional integration tests show how developers interact with the system to run prompts and observe streaming responses.
- Frontend components (Fiddle-frontend, VSCode Extension) serve as the user interface layer that communicates (via APIs or workspaces) with the underlying engine and runtime components.
- Documentation (“fern”, “docs”) is linked to all major components as an aid to both usage and developer onboarding.

##### Architectural Patterns and Design Principles
- Modularity: The engine is split into clear libraries (parsing, runtime, diagnostics, LLM client orchestration, codegen) that are largely independent.
- Layered Architecture: The design separates low-level processing (parsing, IR generation, validation) from high-level client and presentation layers (language clients, web interface, IDE extensions).
- Extensibility & Interoperability: The language client design and code generation indicate that BAML is intended to be integrated into many development environments in a plug-and-play fashion.
- Separation of Concern: Prompts (functions) are separated from language-specific bindings and from the logic needed to handle streaming, retries, and fallback strategies.
- Use of open standards and clear interfacing (e.g., via file-based prompts, JSON-friendly schemas, streaming APIs).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Engine & Libraries
    subgraph "Core Engine & Libraries"
        engine["Core Engine"]:::coreEngine
        bamlLib["baml-lib"]:::coreEngine
        bamlRuntime["baml-runtime"]:::coreEngine
        bamlRPC["baml-rpc"]:::coreEngine
        cli["CLI"]:::coreEngine
    end

    %% Language Clients & Code Generation
    subgraph "Language Clients & Code Generation"
        codeGen["Code Generation Tools"]:::client
        pythonClient["Python Client"]:::client
        rubyClient["Ruby Client"]:::client
        tsClient["TypeScript Client"]:::client
    end

    %% Frontend & Interactive Tools
    subgraph "Frontend & Interactive Tools"
        fiddleFrontend["Fiddle Frontend"]:::frontend
        fiddleProxy["Fiddle Proxy"]:::frontend
        vscode["VSCode Integration"]:::frontend
    end

    %% Documentation & Developer Tools
    subgraph "Documentation & Developer Tools"
        fernDoc["Fern Documentation"]:::docs
        docsDoc["Docs Folder"]:::docs
        integTests["Integration Tests"]:::docs
    end

    %% External LLM Providers
    llmProvider["External LLM Providers"]:::external

    %% Connections within Core Engine
    engine -->|"parses"| bamlLib
    engine -->|"executes"| bamlRuntime
    engine -->|"RPC/debug"| bamlRPC
    engine -->|"triggers"| cli

    %% Core Engine interfacing with External Services
    bamlRuntime -->|"LLM_API_calls"| llmProvider

    %% Core Engine & Code Generation Flow
    engine -->|"exposes_bindings"| codeGen
    codeGen -->|"generates_for"| pythonClient
    codeGen -->|"generates_for"| rubyClient
    codeGen -->|"generates_for"| tsClient

    %% Core Engine streaming to Frontend/IDE Tools
    bamlRuntime -->|"streams_output"| fiddleFrontend
    bamlRuntime -->|"streams_output"| fiddleProxy
    bamlRuntime -->|"streams_output"| vscode

    %% Integration Tests targeting Core Engine
    integTests -->|"tests"| engine

    %% Documentation links to major components
    fernDoc ---|"guides"| engine
    fernDoc ---|"guides"| codeGen
    fernDoc ---|"guides"| fiddleFrontend
    docsDoc ---|"provides_info"| engine
    docsDoc ---|"provides_info"| codeGen
    docsDoc ---|"provides_info"| vscode

    %% Styles
    classDef coreEngine fill:#AED6F1,stroke:#2E86C1,stroke-width:2px;
    classDef client fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef frontend fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px;
    classDef docs fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
    classDef external fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;

    %% Click Events
    click engine "https://github.com/boundaryml/baml/tree/canary/engine"
    click bamlLib "https://github.com/boundaryml/baml/tree/canary/engine/baml-lib"
    click bamlRuntime "https://github.com/boundaryml/baml/tree/canary/engine/baml-runtime"
    click bamlRPC "https://github.com/boundaryml/baml/tree/canary/engine/baml-rpc"
    click cli "https://github.com/boundaryml/baml/tree/canary/engine/cli"
    click codeGen "https://github.com/boundaryml/baml/tree/canary/engine/language_client_codegen"
    click pythonClient "https://github.com/boundaryml/baml/tree/canary/engine/language_client_python"
    click rubyClient "https://github.com/boundaryml/baml/tree/canary/engine/language_client_ruby"
    click tsClient "https://github.com/boundaryml/baml/tree/canary/engine/language_client_typescript"
    click fiddleFrontend "https://github.com/boundaryml/baml/tree/canary/typescript/fiddle-frontend"
    click fiddleProxy "https://github.com/boundaryml/baml/tree/canary/typescript/fiddle-proxy"
    click vscode "https://github.com/boundaryml/baml/tree/canary/typescript/vscode-ext"
    click fernDoc "https://github.com/boundaryml/baml/tree/canary/fern"
    click docsDoc "https://github.com/boundaryml/baml/tree/canary/docs"
    click integTests "https://github.com/boundaryml/baml/tree/canary/integ-tests"
```
